二叉搜索树的接口类
public interface BinarySearchTree extends BinaryTree { Object find(Object obj); Object update(Object obj); boolean insert(Object obj); boolean delete(Object obj); void ascend(); }
二叉搜索树的链接存储类的实现
public class LinkBinarySearchTree extends LinkBinaryTree { public LinkBinarySearchTree(){ super(); } public LinkBinarySearchTree(BTreeNode st){ super(st); } public Object find(Object obj){ if(root==null) return null; BTreeNode st=root; while(st!=null){ if(((Comparable)obj).compareTo(st.element)==0) return st.element; else if(((Comparable)obj).compareTo(st.element)<0) st=st.left; else st=st.right; } return null; } public Object update(Object obj){ if(root==null) return null; BTreeNode st=root; while(st!=null){ if(((Comparable)obj).compareTo(st.element)==0){ Object x=st.element; st.element=obj; return x; } else if(((Comparable)obj).compareTo(st.element)<0) st=st.left; else st=st.right; } return null; } public boolean insert(Object obj){ BTreeNode st=root,pt=null; while(st!=null){ pt=st; if(((Comparable)obj).compareTo(st.element)==0) return false; else if(((Comparable)obj).compareTo(st.element)<0) st=st.left; else st=st.right; } BTreeNode s=new BTreeNode(obj); if(pt==null) root=s; else if(((Comparable)obj).compareTo(pt.element)<0) pt.left=s; else pt.right=s; return true; } public boolean delete(Object obj){ if(root==null) return false; BTreeNode st=root,pt=null; while(st!=null){ if(((Comparable)obj).compareTo(st.element)==0) break; else if(((Comparable)obj).compareTo(st.element)<0){ pt=st;st=st.left; } else{ pt=st;st=st.right; } } if(st==null) return false; if(st.left==null&&st.right==null){ if(st==root) root=null; else if(pt.left==st) pt.left=null; else pt.right=null; } else if(st.left==null||st.right==null){ if(st==root) if(st.left==null) root=st.right;else root=st.left; else if(pt.left==st&&st.left==null) pt.left=st.right; else if(pt.left==st&&st.right==null) pt.left=st.left; else if(pt.right==st&&st.left==null) pt.right=st.right; else if(pt.right==st&&st.right==null) pt.right==st.left; } else if(st.left!=null&&st.right!=null){ BTreeNode s1=st,s2=st.left; while(s2.right!=null){ s1=s2;s2=s2.right; } st.element=s2.element; if(s1==st) st.left=s2.left; else s1.right=s2.left; } return true; } public void ascend(){traverseBTree("inOrder");} }
客户端调试运行代码
public class Example{ public static void main(String[] args) { BinarySearchTree bst=new LinkBinarySearchTree(); Integer []a={23,45,89,40,73,12,49,72,20,44}; for(int i=0;i<a.length;i++) bst.insert(a[i]); System.out.print("二叉搜索树bst的广义表形式:");bst.printBTree(); System.out.print("前序:");bst.traverseBTree("preOrder"); System.out.print("中序:");bst.traverseBTree("inOrder"); System.out.print("后序:");bst.traverseBTree("postOrder"); System.out.print("按层:");bst.traverseBTree("levelOrder"); System.out.print("升序:");bst.ascend(); System.out.println("深度:"+bst.depthBTree()); System.out.println("结点数:"+bst.countBTree()); System.out.println("查找38的结果:"+bst.find(38)); System.out.println("删除73的结果:"+bst.delete(73)); System.out.println("删除40的结果:"+bst.delete(40)); System.out.println("删除23的结果:"+bst.delete(23)); System.out.print("运算后的二叉搜索树bst的广义表形式:");bst.printBTree(); } }
本文出自 “仙路千叠惊尘梦” 博客,请务必保留此出处http://secondscript.blog.51cto.com/9370042/1617238
原文地址:http://secondscript.blog.51cto.com/9370042/1617238