//二叉树节点类 classBinaryNode { //Friendly data;accessible by other package toutines Object element;//The data in the node BinaryNode left;//Left child BinaryNode right;//right child }
查找树ADT——二叉查找树
使二叉树成为查找树的性质是,对于树中的每个节点 X ,它的左子树中所有项的值小于 X 中的项,而它的右子树中所有项的值大于 X 中的项。
/** * Internal method to find an item in a subtree * @param x is item to search for. * @param t the node that roots the subtree. * @return true if the item is found; false otherwise. */
//用递归编写findMin,用非递归编写findMax /** * Internal method to find the smallest item in a subtree * @param t the node that roots the subtree. * @return node containing the smallest item */ private BinaryNode<AnyType> findMin(BinaryNode<AnyType> t) { if(t == null) returnnull; elseif(t.left == null) return t; return findMin(t.left); } /** * Internal method to find the largest item in a subtree * @param t the node that roots the subtree. * @return node containing the largest item. */ private BinaryNode<AnyType> finMax(BinaryNode<AnyType> t) { if(t != null) while(t.right != null) t = t.right; return t; }
/** * Internal method to insert into a subtree * @param x the item to insert * @param t the node that roots the subtree * @return the new root of the subtree */ private BinaryNode<AnyType> insert(AnyType x, BinaryNode<AnyType> t) { if(t == null) returnnewBinaryNode<>(x, null, null);
/** * Internal method to remove from a subtree * @param x the item to remove. * @param t the node that roots the subtree. * @return the new root of the subtree */ private BinaryNode<AnyType> remove(AnyType x, BinaryNode<AnyType> t) { if(t == null) return t;//Item not found; do nothing
intcompareResult= x.compareTo(t.element);
if(compareResult < 0) t.left = remove(x, t.left); elseif(compareResult > 0) t.right = remove(x, t.right); elseif(t.left != null && t.right != null)//Node that has two children { t.element = findMin(t.right).element;//Find the minimum item of right subtree t.right = remove(t.element, t.right);//Remove the node of minimum item recursively } else t = (t.left != null) ? t.left : t.right;//Node that has one children; parent of the node roots subtree of the node return t; }
AvlNode(AnyType theElement, AvlNode<AnyType> lt, AvlNode<AnyType> rt) {element = theElement; left = lt; right = rt; height = 0;}
AnyType element;//The data in the code AvlNode<AnyType> left;//Left child AvlNode<AnyType> right;//Right child int height;//Height }
然后需要一个返回节点高度的方法:
1 2 3 4 5 6 7 8
//返回AVL树的节点高度 /** * return the height of node t, or -1, if null. */ privateintheight(AvlNode<AnyType> t) { return t == null ? -1 : t.height; }
单旋转
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** * Rotate binary tree node with left child. * For AVL trees, this is a single rotation for case 1. * Update heights, then return new root. */ private AvlNode<AnyType> RotationWithLeftChild(AvlNode<AnyType> k2) { AVLTreeNode<AnyType> k1 = k2.left; k2.left = k1.right; k1.right = k2; k2.height = Math.max( height(k2.left), height(k2.right)) + 1; k1.height = Math.max( height(k1.left), k2.height) + 1; return k1; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** * Rotate binary tree node with right child. * For AVL trees, this is a single rotation for case 4. * Update heights, then return new root. */ private AvlNode<AnyType> RotationWithRightChild(AvlNode<AnyType> k1) { AVLTreeNode<AnyType> k2 = k1.right; k1.right = k2.left; k2.left = k1; k1.height = Math.max( height(k1.left), height(k1.right)) + 1; k1.height = Math.max( height(k2.right), k1.height) + 1; return k2; }
双旋转
1 2 3 4 5 6 7 8 9 10 11
/** * Double rotate binary tree node: first left child * with its right child; then node k3 with new left child. * For AVL trees, this is a double rotation for case 2. * Update heights, then return new root. */ private AvlNode<AnyType> doubleWithLeftChild(AvlNode<AnyType> k3) { k3.left = RotationWithRightChild(k3.left); return RotationWithLeftChild(k3); }
1 2 3 4 5 6 7 8 9 10 11
/** * Double rotate binary tree node: first right child * with its left child; then node k1 with new right child. * For AVL trees, this is a double rotation for case 3. * Update heights, then return new root. */ private AvlNode<AnyType> doubleWithRightChild(AvlNode<AnyType> k1) { k1.right = RotationWithRightChild(k1.right); return RotationWithLeftChild(k1); }
/** * Internal method to insert into a subtree. * @param x the item to insert. * @param t the node that roots the subtree. * @return the new root of the subtree. */ private AvlNode<AnyType> insert(AnyType x, AvlNode<AnyType> t) { if(t == null) returnnewAvlNode<>(x, null, null);
private AvlNode<AnyType> remove(AnyType x, AvlNode<AnyType> t) { if(t == null) return t;//Item not found; do nothing
int compareResult = x.compareTo(t.element);
if(compareResult < 0) t.left = remove(x, t.left); else if(compareResult > 0) t.right = remove(x, t.right); else if(t.left != null && t.right != null)//Node that has two children { t.element = findMin(t.right).element;//Find the minimum item of right subtree t.right = remove(t.element, t.right);//Remove the node of minimum item recursively } else t = (t.left != null) ? t.left : t.right;//Node that has one children; parent of the node roots subtree of the node return balance(t); }