二叉树java实现
目标
实现二叉树的增加、查找、删除、遍历
实现
结点类
public class Node{ int key; int data; Node lchild=null; Node rchild=null; public Node(int key,int data) { this.key=key; this.data=data; } }
二叉树类:
public class BinaryTree { Node root=null; public void add(int key,int data) { Node node=new Node(key,data); if (root==null) root=node; else { Node current=root; Node pre=null; while (true) { if (key<current.key) { pre=current; current=current.lchild; if (current==null) { pre.lchild=node; break; } } else { pre=current; current=current.rchild; if (current==null) { pre.rchild=node; break; } } } } } public Node findNode(int key) { Node current=root; while (current!=null) { if (current.key==key) return current; else if (key<current.key) current=current.lchild; else current=current.rchild; } return null; } public int findElement(int key) { Node current=root; while (current!=null) { if (current.key==key) return current.data; else if (key<current.key) current=current.lchild; else current=current.rchild; } return 0; } public boolean delete (int key) { Node current=root; boolean isleftchild=false; Node parent=root; Node currentdouble=null; while (current!=null) { if (root.key==key) { root=null; return true; } if (current.key==key) { //当前结点没有孩子,直接删除 if (current.lchild==null && current.rchild==null) { if (isleftchild=true) parent.lchild=null; else parent.rchild=null; return true; } //当前结点没有左孩子,有右孩子 if (current.lchild==null && current.rchild!=null) { if (isleftchild=true) parent.lchild=current.rchild; else parent.rchild=current.lchild; return true; } //当前结点没有右孩子,有左孩子 if (current.lchild!=null && current.rchild==null) { if (isleftchild=true) parent.lchild=current.lchild; else parent.rchild=current.lchild; return true; } //右孩子,左孩子都有情况,将左孩子结点替换当前结点,当前结点的右孩子作为替换操作后子树中最右的一个结点 if (current.lchild!=null && current.rchild!=null) { if (isleftchild=true) parent.lchild=current.lchild; else parent.rchild=current.lchild; currentdouble=current.lchild; Node rparent=null; while (currentdouble!=null) { rparent=currentdouble; currentdouble=currentdouble.rchild; } rparent.rchild=current.rchild; return true; } } else if (key<current.key) { parent=current; current=current.lchild; isleftchild=true; } else { parent=current; current=current.rchild; isleftchild=false; } } return false; } //先序遍历 public void preorder(Node node) { if (node!=null) { System.out.println(node.data); preorder(node.lchild); preorder(node.rchild); } } //中序遍历 public void midorder(Node node) { if (node!=null) { midorder(node.lchild); System.out.println(node.data); midorder(node.rchild); } } //后序遍历 public void lastorder(Node node) { if (node!=null) { lastorder(node.lchild); lastorder(node.rchild); System.out.println(node.data); } } }
测试类
public class Test { public static void main(String[] args) { BinaryTree bt=new BinaryTree(); bt.add(100, 100); bt.add(50, 50); bt.add(150, 150); bt.add(10, 10); bt.add(60, 60); bt.add(120, 120); bt.add(180, 180); System.out.println(bt.findElement(50)); System.out.println(bt.root.data); bt.preorder(bt.root); System.out.println(bt.root.key); System.out.println(bt.delete(50)); bt.preorder(bt.root); } }
参考资料
http://blog.csdn.net/aaaaaaaa0705/article/details/6713845
http://www.ibm.com/developerworks/cn/java/j-lo-tree/