Java 二叉查找(排序)树 创建 以及中序和层序遍历

首先我们需要知道二叉排序树是个什么东西。它或者是一棵空树或者是具有下列性质的二叉树:1.如果左子树不空,那么左子树上所有节点的值均小于它的根节点的值2.如果右子树不空,那么右子树上所有节点的值均小于它的根节点的值3.左右子树也分别为二叉排序树

[java]  view plain  copy
  1. import java.util.LinkedList;  
  2. import java.util.Queue;  
  3.   
  4. class biNode{  
  5.     private int data;  
  6.     private biNode left;  
  7.     private biNode right;  
  8.     //构造函数  
  9.     public biNode(int data){  
  10.         this.data=data;  
  11.         this.left=null;  
  12.         this.right=null;  
  13.     }  
  14.     public void setData(int data){  
  15.         this.data=data;  
  16.     }  
  17.     public int getData(){  
  18.         return this.data;  
  19.     }  
  20.     public void setLeft(biNode left){  
  21.         this.left=left;  
  22.     }  
  23.     public biNode getLeft(){  
  24.         return this.left;  
  25.     }  
  26.     public void setRight(biNode right){  
  27.         this.right=right;  
  28.     }  
  29.     public biNode getRight(){  
  30.         return this.right;  
  31.     }  
  32. }  
  33. public class binarySearchTree {  
  34.     private biNode root;  
  35.     //构造函数  
  36.     public void binarySearchTree(){  
  37.         this.root=null;  
  38.     }  
  39.     //把结点插入二叉树中构造线索二叉树  
  40.     public void insert(int data){  
  41.         biNode newNode = new biNode(data);  
  42.         if(root==null){  
  43.             root=newNode;  
  44.         }  
  45.         else{  
  46.             biNode currentNode=root;  
  47.             biNode parent;  
  48.             //寻找插入位置  
  49.             while(true){  
  50.                 parent=currentNode;  
  51.                 if(data<currentNode.getData()){  
  52.                     currentNode=currentNode.getLeft();  
  53.                     if(currentNode==null){  
  54.                         parent.setLeft(newNode);  
  55.                         return;  
  56.                     }  
  57.                 }  
  58.                 else{  
  59.                     currentNode=currentNode.getRight();  
  60.                     if(currentNode==null){  
  61.                         parent.setRight(newNode);  
  62.                         return;  
  63.                     }  
  64.                 }  
  65.             }  
  66.         }  
  67.     }  
  68.       
  69.     //通过一个整数数组构建线索二叉树  
  70.     public void buildTree(int[] d){  
  71.         for(int i=0;i<d.length;i++){  
  72.             insert(d[i]);  
  73.         }  
  74.     }  
  75.     //中序递归遍历线索二叉树  
  76.     public void inorder(biNode localRoot){  
  77.         if(localRoot!=null){  
  78.             inorder(localRoot.getLeft());  
  79.             System.out.print(localRoot.getData()+" ");  
  80.             inorder(localRoot.getRight());  
  81.         }  
  82.     }  
  83.     //用队列实现层序遍历二叉树  
  84.     public void layerTranverse(biNode root){  
  85.         //为什么不能创建queue类型的对象 直接  
  86.         //Queue<biNode> myq=new Queue<biNode>();  
  87.         //因为Queue只是一个接口 Linkedlist才是实现了它的一个具体的类  
  88.         Queue<biNode> myq=new LinkedList<biNode>();  
  89.         biNode tmp=root;  
  90.         if(tmp==null){  
  91.             return;  
  92.         }  
  93.         else{  
  94.             myq.add(tmp);  
  95.             while(!myq.isEmpty()){  
  96.                 biNode n=myq.poll();  
  97.                 System.out.print(n.getData()+" ");  
  98.                 if(n.getLeft()!=null){  
  99.                     myq.add(n.getLeft());  
  100.                 }  
  101.                 if(n.getRight()!=null){  
  102.                     myq.add(n.getRight());  
  103.                 }  
  104.             }  
  105.         }  
  106.     }  
  107.     public static void main(String[] args){  
  108.         binarySearchTree mytree = new binarySearchTree();  
  109.         int[] data={2,8,7,4,9,3,1,6,7,5};  
  110.         mytree.buildTree(data);  
  111.         mytree.inorder(mytree.root);  
  112.         System.out.println();  
  113.         mytree.layerTranverse(mytree.root);  
  114.     }  
  115. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值