数据结构与算法分析(Java语言描述)简记 --- 第4章 树 ---
4.2 二叉树
二叉树(binary tree)的平均深度为 O(√N),
二叉查找树(binary search tree)的平均深度为 O(logN)。
4.2.1 实现
1 class BinaryNode { // 节点类 2 Object element; // 节点的值 3 BinaryNode left; // 左子树 4 BinaryNode right; // 右子树 5 }
4.3 二叉查找树
BinaryNode类
1 private static class BinaryNode<AnyType> { 2 BinaryNode(AnyType theElement) { 3 this(theElement, null, null); 4 } 5 6 BinaryNode(AnyType theElement, BinaryNode<AnyType> lt, BinaryNode<AnyType> rt) { 7 element = theElement; 8 left = lt; 9 right = rt; 10 } 11 12 AnyType element; // 节点的值 13 BinaryNode<AnyType> left; // 左子树 14 BinaryNode<AnyType> right; // 右子树 15 }
二叉查找树架构
1 public class BinarySearchTree<T extends Comparable<? super T>> { 2 3 private static class BinaryNode<T> { 4 /* 5 Figure 4.16 6 */ 7 } 8 9 private BinaryNode<T> root; 10 11 public BinarySearchTree() { 12 root = null; 13 } 14 15 public void makeEmpty() { 16 root = null; 17 } 18 19 public boolean isEmpty() { 20 return root == null; 21 } 22 23 public boolean contains(T x) { 24 return contains(x, root); 25 } 26 27 public T findMin() { 28 if (isEmpty()) 29 throw new UnderflowException(); 30 return findMin(root).element; 31 } 32 33 public T findMax() { 34 if (isEmpty()) 35 throw new UnderflowException(); 36 return findMax(root).element; 37 } 38 39 public void insert(T x) { 40 root = insert(x, root); 41 } 42 43 public void remove(T x) { 44 root = remove(x, root); 45 } 46 47 public void printTree() { 48 /* 49 Figure 4.56 50 */ 51 } 52 53 private boolean contains(T x, BinaryNode<T> t) { 54 /* 55 Figure 4.18 56 */ 57 } 58 59 private BinaryNode<T> findMin(BinaryNode<T> t) { 60 /* 61 Figure 4.20 62 */ 63 } 64 65 private BinaryNode<T> findMax(BinaryNode<T> t) { 66 /* 67 Figure 4.20 68 */ 69 } 70 71 private BinaryNode<T> insert(T x, BinaryNode<T> t) { 72 /* 73 Figure 4.22 74 */ 75 } 76 77 private BinaryNode<T> remove(T x, BinaryNode<T> t) { 78 /* 79 Figure 4.25 80 */ 81 } 82 83 private void printTree(BinaryNode<T> t) { 84 /* 85 Figure 4.56 86 */ 87 } 88 }
二叉查找树的contains操作
1 private boolean contains(T x, BinaryNode<T> t) { 2 if (t == null) { 3 return false; 4 } 5 6 int compareResult = x.compareTo(t.element); 7 8 if (compareResult < 0) { 9 return contains(x, t.left); 10 } else if (compareResult > 0) { 11 return contains(x, t.right); 12 } else { 13 return true; 14 } 15 }
对使用函数对象实现二叉查找树的注释
1 public class BinarySearchTree<T> { 2 3 private BinaryNode<T> root; 4 private Comparator<? super T> cmp; 5 6 public BinarySearchTree() { 7 root = null; 8 } 9 10 public BinarySearchTree(Comparator<? super T> c) { 11 root = null; 12 cmp = c; 13 } 14 15 private int myCompare(T lhs, T rhs) { 16 if (cmp != null) { 17 return cmp.compare(lhs, rhs); 18 } else { 19 return ((Comparable)lhs).compareTo(rhs); 20 } 21 } 22 23 private boolean contains(T x, BinaryNode<T> t) { 24 if (t == null) { 25 return false; 26 } 27 int compareResult = myCompare(x, t.element); 28 29 if (compareResult < 0) { 30 return contains(x, t.left); 31 } else if (compareResult > 0) { 32 return contains(x, t.right); 33 } else { 34 return true; 35 } 36 } 37 38 }

浙公网安备 33010602011771号