關於我自己

我的相片
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
==========================================================

沒有留言: