許老師結合數學與電腦程式的教學網 課程包含 程式設計 數值分析 -------------------------------------- Prof. Hsu teaches computer programming and numerical analysis to help those who wants to be a computer programming master and focus on controlling computers to benefit people more than only on making money.
關於我自己
- 許志宇(Chih-Yu Hsu)
- Welcome to discuss about : Chinese Traditional Medicine and Acupuncture Please send me the email: tccnchsu@gmail.com Chih-Yu Hsu
2009年5月14日 星期四
fig
=================================
http://www.powercam.cc/slide/1561
=================================================================================
class Mytree{
TreeNode root;
Mytree()
{
root=null;
}
void insertNode(int insertValue )
{
if ( root == null )
root = new TreeNode( insertValue );
else
root.insert( insertValue );
}
void preorderTraversal()
{
preorderHelper( root);
}
void preorderHelper( TreeNode node )
{
if(node==null)
{
System.out.println("空的");
return;
}
else
{
System.out.println("不空的");
// output node data
System.out.println( node.data + " " );
// traverse left subtree
preorderHelper( node.leftNode );
// traverse right subtree
preorderHelper( node.rightNode );
}
}
public static void main(String args[]){
//TreeNode root = new TreeNode(47);
Mytree tree = new Mytree();
tree.insertNode(47);
tree.insertNode(25);
tree.insertNode(77);
tree.preorderTraversal();
}
}
==========================================================
// class TreeNode definition
class TreeNode {
// package access members
TreeNode leftNode;
int data;
TreeNode rightNode;
// initialize data and make this a leaf node
public TreeNode( int nodeData )
{
data = nodeData;
leftNode = rightNode = null; // node has no children
}
// insert TreeNode into Tree that contains nodes;
// ignore duplicate values
public synchronized void insert( int insertValue )
{
// insert in left subtree
if ( insertValue < data ) {
// insert new TreeNode
if ( leftNode == null )
leftNode = new TreeNode( insertValue );
// continue traversing left subtree
else
leftNode.insert( insertValue );
}
// insert in right subtree
else if ( insertValue > data ) {
// insert new TreeNode
if ( rightNode == null )
rightNode = new TreeNode( insertValue );
// continue traversing right subtree
else
rightNode.insert( insertValue );
}
} // end method insert
} // end class TreeNode
==========================================================
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言