java实现简单二叉树排序
2018-01-04 09:41
399 查看
public class Test01{public static void main(String args[]){BinaryTree bt = new BinaryTree();bt.add(3);bt.add(7);bt.add(9);bt.add(2);bt.add(4);bt.add(12);bt.add(8);bt.add(6);bt.add(21);bt.add(0);bt.add(4);bt.print();}};class BinaryTree{class Node{private Comparable data;//自动装箱,Integer转为Comparableprivate Node left;private Node right;public Node(Comparable data){this.data = data;}public void addNode(Node newNode){//添加新节点if(newNode.data.compareTo(this.data)<0){if(this.left == null){this.left = newNode;}else{this.left.addNode(newNode);}}if(newNode.data.compareTo(this.data)>=0){if(this.right == null){this.right = newNode;}else{this.right.addNode(newNode);}}}public void printNode(){//中序输出if(this.left!=null){this.left.printNode();}System.out.println(this.data+" ");if(this.right!=null){this.right.printNode();}}};private Node root;//根public void add(Comparable data){//添加元素Node newNode = new Node(data);if(root == null){//如果没有根节点root = newNode;}else{root.addNode(newNode);}}public void print(){root.printNode();}};简单实现
相关文章推荐
- java实现简单二叉树排序
- java实现简单二叉树排序
- java实现简单二叉树排序
- 二叉树排序在Java代码的简单实现
- java实现简单二叉树排序
- java实现简单二叉树排序
- java实现简单二叉树排序
- java实现简单二叉树排序
- 数据结构(JAVA)---二叉树的简单实现及排序
- java实现简单二叉树排序
- java实现简单二叉树排序
- Java中实现简单的三大排序——冒泡、选择、插入
- 排序与查找简单算法 java代码实现
- java实现简单的二叉树
- JAVA实现简单的冒泡排序与选择排序
- JAVA之冒泡排序与简单选择排序的实现
- Java简单实现几种常见排序方法
- 程序员必知的8大排序(三)-------简单选择排序,堆排序(java实现)
- Java 实现二叉树排序和查找
- java实现的二叉树排序