1 import java.util.*; 2 3 //docs about generic version of class: https://docs.oracle.com/javase/tutorial/java/generics/types.html 4 public class myBST <T extends Comparable<T>>{ 5 6 //define tree node using a inner class 7 private class Node<T> { 8 private T data; 9 private Node<T> left, right; 10 11 public Node (T data) { 12 this.data = data; 13 } 14 15 public Node (T data, Node<T> left, Node<T> right) { 16 this.data = data; 17 this.left = left; 18 this.right = right; 19 } 20 } 21 22 //Fields 23 private Node<T> root; 24 private Comparator<T> comparator; 25 26 //Two constructer 27 public myBST() { 28 root = null; 29 comparator = null; 30 } 31 32 public myBST(Comparator<T> comparator) { 33 root = null; 34 this.comparator = comparator; 35 } 36 37 private int compare(T x, T y) { 38 if (comparator == null) { 39 return x.compareTo(y); 40 }else{ 41 return comparator.compare(x, y); 42 } 43 } 44 45 //************************************************************************** 46 // insert 47 //************************************************************************** 48 public void insert(T data) { 49 root = insert(root, data); 50 } 51 52 private Node<T> insert(Node<T> root, T data) { 53 if (root == null) { 54 return new Node<T>(data); 55 } 56 if (compare(root.data, data) == 0) { 57 return root; 58 }else if (compare(root.data, data) > 0) { 59 root.right = insert(root.right, data); 60 }else{ 61 root.left = insert(root.left, data); 62 } 63 return root; 64 } 65 66 //************************************************************************** 67 // search 68 //************************************************************************** 69 public boolean search (T data) { 70 return search(root, data); 71 } 72 private boolean search (Node<T> root, T data) { 73 if (root == null) { 74 return false; 75 } 76 if (compare(root.data, data) == 0) { 77 return true; 78 }else if (compare(root.data, data) < 0) { 79 return search(root.right, data); 80 }else{ 81 return search(root.left, data); 82 } 83 } 84 85 //************************************************************************** 86 // delete 87 //************************************************************************** 88 public void delete(T data) { 89 root = delete(root, data); 90 } 91 private Node<T> delete(Node<T> root, T data) { 92 if (root == null) { 93 throw new RuntimeException("Cannot delete"); 94 } 95 if (compare(root.data, data) == 0) { 96 if (root.left == null) { 97 return root.right; 98 }else if (root.right == null) { 99 return root.left; 100 }else{ 101 Node<T> cur = root; 102 while (cur.left != null) { 103 cur = cur.left; 104 } 105 root.data = cur.data; 106 delete(root.left, root.data); 107 } 108 }else if (compare(root.data, data) < 0) { 109 delete(root.right, data); 110 }else{ 111 delete(root.left, data); 112 } 113 return root; 114 } 115 116 117 public void preOrderTraversal() 118 { 119 preOrderHelper(root); 120 } 121 private void preOrderHelper(Node<T> r) 122 { 123 if (r != null) 124 { 125 System.out.print(r+" "); 126 preOrderHelper(r.left); 127 preOrderHelper(r.right); 128 } 129 } 130 131 132 133 134 public static void main(String[] args) { 135 Integer[] a = {1,5,2,7,4}; 136 BST<Integer> bst = new BST<Integer>(new MyComp1()); 137 //bst.delete(1); 138 for(Integer n : a) bst.insert(n); 139 bst.preOrderTraversal(); 140 System.out.println(); 141 } 142 } 143 144 class MyComp1 implements Comparator<Integer> 145 { 146 public int compare(Integer x, Integer y) 147 { 148 return y-x; 149 } 150 }
时间: 2024-10-11 21:49:20