二叉排序树 の 实现&遍历

来源:互联网 发布:昌泰茶业。淘宝 编辑:程序博客网 时间:2024/05/17 22:58

看了书上关于二叉树的知识,决定用java来实现一下二叉树。

实现的是二叉排序树,就是小的在左边,大的在右边那个。

然后实现了,中序遍历,先序遍历,后序遍历,层序遍历

然后实现了一下按行打印二叉树。

感觉萌萌哒。


首先,我想说,某些书上的代码是错的,编译都通不过,也出了书。 这算不算误人子弟微笑


然后我想说,为什么学校给买的教材永远都是那么难?! 看都看不懂,一点儿都不接地气骂人


最后我想说,代码还是要自己敲一遍才理解的深刻啊。

以前备考的时候准备这几个遍历,头都大了,每次都算不对,

其实代码一写,真的是一目了然。

以前觉得很难的东西,自己做过了,突然觉得没啥难的,都不知道要讲什么。

就这样吧,贴代码:


/** *  */package dianer;import java.util.ArrayList;import java.util.LinkedList;import java.util.Queue;/** * * @author LilyLee * @date  2017年3月19日 * @Version  * */public class BinaryTree {public Node root;public BinaryTree(){root=null;}public static void main(String[] args) {BinaryTree biTree=new BinaryTree();int [] data={2,8,7,4,9,3,1,6,7,5};biTree.root=biTree.buildTree(data);System.out.println("中序遍历结果:");biTree.inOrder();System.out.println("\n先序遍历结果:");biTree.preOrder();System.out.println("\n后序遍历结果:");biTree.postOrder();System.out.println("\n层序遍历结果:");biTree.layerTranverse();System.out.println("\n 按行打印结果:");biTree.layerPrint();}public void inOrder(){this.inOrder(this.root);}public void preOrder(){this.preOrder(this.root);}public void postOrder(){this.postOrder(this.root);}public void layerTranverse(){this.layerTranverse(this.root);}/*//将data插入到二叉树中public void insert(int data){Node newNode=new Node(data);if(root==null)root=newNode;else{Node current=root;Node parent=null;while(true){//寻找插入位置parent=current;if(data>current.data){ current=current.left;if(current==null){parent.left=newNode;return;}else{current=current.right;if(current==null){parent.right=newNode;return;}}}}}}*///将数值输入构建二叉树public Node buildTree(int[] data){Node root=new Node(data[0]);for(int i=1;i<data.length;i++){root.insert(data[i]);}return root;}//中序遍历方法的递归实现public void inOrder(Node localRoot){if(localRoot!=null){inOrder(localRoot.left);System.out.print(localRoot.data+" ");inOrder(localRoot.right);}}//先序遍历方法的递归实现public void preOrder(Node localRoot){if(localRoot!=null){System.out.print(localRoot.data+" ");preOrder(localRoot.left);preOrder(localRoot.right);}}//后序遍历的递归实现public void postOrder(Node localRoot){if(localRoot!=null){postOrder(localRoot.left);postOrder(localRoot.right);System.out.print(localRoot.data+" ");}}//层序遍历二叉树 /*这个代码是面试宝典上的,我觉得不太好,打印出来的顺序确实是对的  * 但是不能反应出“层序”的感觉*/public void layerTranverse(Node localRoot){if(root==null) { System.out.println("Null tree!"); return;}Queue<Node>q=new LinkedList<Node>();q.add(this.root);while(!q.isEmpty()){Node n=q.poll();System.out.print(n.data+" ");if(n.left!=null)q.add(n.left);if(n.right!=null)q.add(n.right);}}//层序遍历二叉树,又名, 把二叉树打印成多行/*《解法来自剑指offer》* 这里使用队列来实现二叉树的层序遍历,* 先将根节点放入队列,然后每次都从队列中取出一个节点来打印该数值。* 如果这个节点有子节点,则把它的子节点放到队列尾,* 直到队列为空* */public void layerPrint(){LinkedList <Node> queue =new LinkedList<Node>();if(root==null){System.out.println("Null tree"); return;}Node current=root;queue.offer(current); // offer就是队列的add,不过对于队列长度未知的情况更适用int count;//用于记录 本层 已经打印的个数int last;//记录 本层 一共有多少个while(!queue.isEmpty()){count=0;last=queue.size();ArrayList<Integer>list=new ArrayList<Integer>();while(count<last){current=queue.pop();// 根节点出队list.add(current.data);//准备打印根节点count++;if(current.left!=null)queue.offer(current.left);if(current.right!=null)queue.offer(current.right);}for(int i=0;i<list.size();i++)System.out.print(list.get(i)+" ");System.out.println(" ");}}class Node{public int data;public Node left;public Node right;public Node(int data){this.data=data;this.left=null;this.right=null;}public void insert(int data){if(data<this.data){if(this.left==null){this.left=new Node(data);}else {this.left.insert(data);}}else if(data>=this.data){if(this.right==null){this.right=new Node(data);}else{this.right.insert(data);}}}}}


0 0
原创粉丝点击