中序遍历
INORDER-TREE-WALK(x) if x != NIL INORDER-TREE-WALK(x.left) print x.key INORDER-TREE-WALK(x.right)
查找
TREE-SEARCH(x,k) if x == NIL or k == x.key return x if k < x.key return TREE-SEARCH(x.left, k) else return TREE-SEARCH(x.right, k)
最大关键字
TREE-MAXIMUM(x) while(x.right != NIL) x = x.right return x
最小关键字
TREE-MINIMUM(x) while(x.left != NIL) x = x.left return x
后继
TREE-SUCCESSOR(x) if x.right != NIL return TREE-MINIMUM(x.right) y = x.p while y != NIL and x == y.right x = y y = y.p return y
插入
TREE-INSERT(T, z) y = NIL x = T.root while (x != NIL) y = x if(z.key < x.key) x = x.left else(x = x.right) z.p = y if(y == NIL) T.root = z // tree T was empty else if(z.key < y.key) y.left = z else y.right = z
删除
TREE-DELETE(T, z) if left[z] = NIL or right[z] = NIL then y = z; else y = TREE-SUCCESSOR(z) if left[y] != NIL then x = left[y] else x = right[y] if x!=NIL then p[x] = p[y] if p[y] == NIL then root[T] = x else if y = left[p[y]] then left[p[y]] = x else right[p[y]] = x if y != z then key[z] = key[y] return y
实现
构造二叉树结构
public class BinarySearchTree<E> { private TreeNode<E> root = new TreeNode<E>(null, null, null, null); public BinarySearchTree(E root) { this.root = new TreeNode<E>(root); } } public class TreeNode<T> { TreeNode<T> parent; TreeNode<T> leftChild; T value; TreeNode<T> rightChild; public TreeNode(T value) { this.value = value; } public TreeNode(TreeNode<T> parent, TreeNode<T> leftChild, T element, TreeNode<T> rightChild) { this.parent = parent; this.leftChild = leftChild; this.value = element; this.rightChild = rightChild; } }
方法
public void insert(BinarySearchTree<Integer> bt, Integer value) { TreeNode<Integer> element = new TreeNode<Integer>(value); element.leftChild = element.rightChild = null; TreeNode<Integer> x = bt.root; TreeNode<Integer> y = null; while (x != null) { y = x; if (element.value < x.value) { x = x.leftChild; } else { x = x.rightChild; } } element.parent = y; if (y == null) { bt.root = element; } else if (element.value < y.value) { y.leftChild = element; } else { y.rightChild = element; } } public void delete(BinarySearchTree<Integer> bt, TreeNode<Integer> node) { TreeNode<Integer> x = null; TreeNode<Integer> y = null; if (node.leftChild == null || node.rightChild == null) { y = node; } else { y = getSuccessor(node); } if (y.leftChild == null) { x = y.leftChild; } else { x = y.rightChild; } if (x != null) { x.parent = y.parent; } if (y.parent == null) { bt.root = x; } else if (y == y.parent.leftChild) { y.parent.leftChild = x; } else { y.parent.rightChild = x; } if (y != node) { node.value = y.value; } } //后继 public TreeNode<Integer> getSuccessor(TreeNode<Integer> node) { if (node.rightChild != null) { return getMinNode(node.rightChild); } TreeNode<Integer> y = node.parent; while (y != null && node == y.rightChild) { node = y; y = y.parent; } return y; } //前驱 public TreeNode<Integer> getPredecessor(TreeNode<Integer> node) { if (node.leftChild != null) { return getMaxNode(node.leftChild); } TreeNode<Integer> y = node.parent; while (y != null && node == y.leftChild) { node = y; y = y.parent; } return y; } public TreeNode<Integer> getMinNode(TreeNode<Integer> root) { TreeNode<Integer> node = root; while (node.leftChild != null) { node = node.leftChild; } return node; } public TreeNode<Integer> getMaxNode(TreeNode<Integer> root) { TreeNode<Integer> node = root; while (node.rightChild != null) { node = node.rightChild; } return node; } //中序遍历 public void inOrderTreeWalk(TreeNode<Integer> root) { if (root != null) { inOrderTreeWalk(root.leftChild); System.out.print(root.value + " "); inOrderTreeWalk(root.rightChild); } } //前序遍历 public void preOrderTreeWalk(TreeNode<Integer> root) { if (root != null) { System.out.print(root.value + " "); preOrderTreeWalk(root.leftChild); preOrderTreeWalk(root.rightChild); } } //后续遍历 public void postOrderTreeWalk(TreeNode<Integer> root) { if (root != null) { postOrderTreeWalk(root.leftChild); postOrderTreeWalk(root.rightChild); System.out.print(root.value + " "); } } public TreeNode<Integer> search(TreeNode<Integer> root, Integer k) { if (root == null || k.intValue() == root.value) { return root; } if (k < root.value) { return search(root.leftChild, k); } else { return search(root.rightChild, k); } } public BinarySearchTree<Integer> createBinarySearchTree(int[] a) { BinarySearchTree<Integer> bt = null; for (int i = 0; i < a.length; i++) { if (i == 0) { bt = new BinarySearchTree<Integer>(a[0]); } else { insert(bt, a[i]); } } return bt; }