import java.util.*;
class Tree
{
Tree left,right;
int item;
public Tree(Tree left,Tree right,int item)
{
this.left=left;
this.right=right;
this.item=item;
}
public void insert(int item)
{
Tree root=this;
Tree node=new Tree(null,null,item);
while(true)
{
if(root.item==item)return;
if(root.item>item)
{
if(root.left==null)
{
root.left=node;
return;
}
else
root=root.left;
}
else
{
if(root.right==null)
{
root.right=node;
return;
}
root=root.right;
}
}
}
public void inorderTraversal()
{
if(left!=null)left.inorderTraversal();
System.out.println(this.item+" ");
if(right!=null)right.inorderTraversal();
}
};
class TreeDictionary
{
public static void main(String[] args)
{
System.out.println("Hello World!");
Tree mapleTree=new Tree(null,null,10);
mapleTree.insert(5);
mapleTree.insert(20);
mapleTree.insert(30);
mapleTree.insert(7);
mapleTree.insert(15);
mapleTree.insert(19);
mapleTree.inorderTraversal();
}
}
二叉排序树java实现
最新推荐文章于 2022-11-14 20:58:43 发布