java實現二叉排序樹

來源:互聯網
上載者:User

在電腦科學中,樹是一種非常重要的資料結構,而且有非常廣泛的應用,例如linux下的目錄結構就可以看成是一棵樹,另外樹也是儲存大量的資料一種解決方案,二叉排序樹是樹的一種特殊情形,它的每個節點之多隻能有兩個子節點,同時左子樹的節點都小於它的父節點,右子樹中的節點都大於它的父節點,二叉排序樹在搜尋中的應用非常廣泛,同時二叉排序樹的一個變種(紅/黑樹狀結構)是java中TreeMap和TreeSet的實現基礎。下邊是二叉排序樹的定義,其中用到了兩個類,一個是Node類,代表樹中的節點,另外一個是Name類,表示節點的資料,Name類實現Comparable介面,這樣才可以比較節點的大小。

public class BinarySearchTree{private Node root;private int size;public BinarySearchTree(Node root){this.root=root;size++;}public int getSize(){return this.size;}public boolean contains(Name name){return contains(name,this.root);//return false;}private boolean contains(Name n,Node root){if(root==null){return false;}int compare=n.compareTo(root.element);if(compare>0){if(root.right!=null){return contains(n,root.right);}else{return false;}}else if(compare<0){if(root.left!=null){return contains(n,root.left);}else{return false;}}else{return true;}}public boolean insert(Name n){boolean flag = insert(n,this.root);if(flag) size++;return flag;}private boolean insert(Name n,Node root){if(root==null){this.root=new Node(n);return true;}else if(root.element.compareTo(n)>0){if(root.left!=null){return insert(n,root.left);}else{root.left=new Node(n);return true;}}else if(root.element.compareTo(n)<0){if(root.right!=null){return insert(n,root.right);}else{root.right=new Node(n);return true;}}else{root.frequency++;return true;}}public boolean remove(Name name){root = remove(name,this.root);if(root != null){size--;return true;}return false;}private Node remove(Name name,Node root){int compare = root.element.compareTo(name);if(compare == 0){if(root.frequency>1){root.frequency--;}else{/**根據刪除節點的類型,分成以下幾種情況**①如果被刪除的節點是葉子節點,直接刪除**②如果被刪除的節點含有一個子節點,讓指向該節點的指標指向他的兒子節點**③如果被刪除的節點含有兩個子節點,找到左字數的最大節點,並替換該節點**/if(root.left == null && root.right == null){root = null;}else if(root.left !=null && root.right == null){root = root.left;}else if(root.left == null && root.right != null){root = root.right;}else{//被刪除的節點含有兩個子節點Node newRoot = root.left;while (newRoot.left != null){newRoot = newRoot.left;//找到左子樹的最大節點}root.element = newRoot.element;root.left = remove(root.element,root.left);}}}else if(compare > 0){if(root.left != null){root.left = remove(name,root.left);}else{return null;}}else{if(root.right != null){root.right = remove(name,root.right);}else{return null;}}return root;}public String toString(){//中序遍曆就可以輸出樹中節點的順序return toString(root);}private String toString(Node n){String result = "";if(n != null){if(n.left != null){result += toString(n.left);}result += n.element + " ";if(n.right != null){result += toString(n.right);}}return result;}}

在二叉排序樹的操作中,節點的刪除時最難處理的,要分成很多種情況分別進行處理,下邊是Node類和Name類的定義:

class Node{public Name element;public Node left;public Node right; public int frequency = 1;public Node(Name n){this.element=n;}}class Name implements Comparable<Name>{private String firstName;private String lastName;public Name(String firstName,String lastName){this.firstName=firstName;this.lastName=lastName;}public int compareTo(Name n) {int result = this.firstName.compareTo(n.firstName);return result==0?this.lastName.compareTo(n.lastName):result;}public String toString(){return firstName + "-" +lastName;}}

最後是二叉排序樹的測試:

public static void main(String[] args){//System.out.println("sunzhenxing");Node root = new Node(new Name("sun","zhenxing5"));BinarySearchTree bst =new BinarySearchTree(root);bst.insert(new Name("sun","zhenxing3"));bst.insert(new Name("sun","zhenxing7"));bst.insert(new Name("sun","zhenxing2"));bst.insert(new Name("sun","zhenxing4"));bst.insert(new Name("sun","zhenxing6"));bst.insert(new Name("sun","zhenxing8"));System.out.println(bst);bst.remove(new Name("sun","zhenxing2"));System.out.println(bst);bst.remove(new Name("sun","zhenxing7"));System.out.println(bst);}

測試輸出是:

sun-zhenxing2 sun-zhenxing3 sun-zhenxing4 sun-zhenxing5 sun-zhenxing6 sun-zhenxing7 sun-zhenxing8
sun-zhenxing3 sun-zhenxing4 sun-zhenxing5 sun-zhenxing6 sun-zhenxing7 sun-zhenxing8
sun-zhenxing3 sun-zhenxing4 sun-zhenxing5 sun-zhenxing6 sun-zhenxing8

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.