首页 > 代码库 > 二叉搜索树
二叉搜索树
Java
Node.class
1 class Node { 2 int iData; 3 double fData; 4 Node leftChild; 5 Node rightChild; 6 7 public void displayNode(){ 8 System.out.println(this.toString()); 9 } 10 11 @Override 12 public String toString() { 13 return "<Node>:[iData="http://www.mamicode.com/+this.iData+";fData="http://www.mamicode.com/+this.fData+"]"; 14 } 15 16 public Node(int iData, double fData) { 17 this.iData =http://www.mamicode.com/ iData; 18 this.fData =http://www.mamicode.com/ fData; 19 } 20 }
Tree.class
public class Tree { private Node root; public Tree(){ root =null; } public Node find(int key){ Node current = root; if(root==null){ return root; } while(current.iData!=key){ if(current.iData>key){ current = current.leftChild; }else{ current = current.rightChild; } if(current==null){ return null; } } return current; } public void insert(int iData, double fData){ Node newNode = new Node(iData, fData); if(root==null){ root = newNode; }else{ Node current = root; while(true){ Node parent = current; if(iData<current.iData){ current = current.leftChild; if(current==null){ parent.leftChild = newNode; return; } }else{ current = current.rightChild; if(current==null){ parent.rightChild = newNode; return; } } } } } public boolean delete(int key){ Node current = root; Node parent = root; boolean isLeftChild = true; while(current.iData!=key){ parent =current; if(current.iData>key){ current = current.leftChild; isLeftChild = true; }else{ current = current.rightChild; isLeftChild = false; } if(current==null){ return false; } } if(current.leftChild==null && current.rightChild==null){ if(current==root){ root = null; }else if(isLeftChild){ parent.leftChild = null; }else{ parent.rightChild = null; } }else if(current.leftChild==null){ if(current==root){ root = root.rightChild; }else if(isLeftChild){ parent.leftChild = current.rightChild; }else{ parent.rightChild = current.rightChild; } }else if(current.rightChild==null){ if(current==root){ root = root.leftChild; }else if(isLeftChild){ parent.leftChild = current.leftChild; }else{ parent.rightChild = current.leftChild; } }else{ Node suceesstor = getSuceesstor(current); if(current==root){ root = suceesstor; }else if(isLeftChild){ parent.leftChild = suceesstor; }else{ parent.rightChild = suceesstor; } suceesstor.leftChild = current.leftChild; } return true; } private Node getSuceesstor(Node delNode){ Node successerParent = delNode; Node successer = delNode; Node current = delNode.rightChild; while(current!=null){ successerParent = successer; successer = current; current = current.leftChild; } if(successer!=delNode.rightChild){ successerParent.leftChild = successer.rightChild; successer.rightChild = delNode.rightChild; } return successer; } /** * 前序遍历 * @param localRoot */ public void preOrder(Node localRoot){ if(localRoot!=null){ System.out.println(localRoot.toString()); preOrder(localRoot.leftChild); preOrder(localRoot.rightChild); } } public void inOrder(Node localRoot){ if(localRoot!=null){ inOrder(localRoot.leftChild); System.out.println(localRoot.toString()); inOrder(localRoot.rightChild); } } public void outOrder(Node localRoot){ if(localRoot!=null){ outOrder(localRoot.leftChild); outOrder(localRoot.rightChild); System.out.println(localRoot.toString()); } } public Node getRoot() { return root; } public void setRoot(Node root) { this.root = root; } }
二叉搜索树
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。