binary search trees
play

Binary Search Trees ! A data structures with efficient support for - PDF document

Binary Search Trees ! A data structures with efficient support for many dynamic set operations: Search/Find Minimum/findMin Maximum/findMax Binary Search Trees Predecessor Successor Insert Delete/Remove ! All


  1. Binary Search Trees ! A data structures with efficient support for many dynamic set operations: – Search/Find – Minimum/findMin – Maximum/findMax Binary Search Trees – Predecessor – Successor – Insert – Delete/Remove ! All these operations take time proportional to the height of the tree. 1 2 Binary Tree Definition Implementation ! A binary-tree that satisfies the binary- class BinaryNode { search-tree property : Comparable item; BinaryNode left; ! Let x be a node in a binary search BinaryNode right; tree. Any node y in the left subtree of x satisfies: y.key <= x.key BinaryNode(Comparable x) { Any node y in the right subtree of x this(x, null, null); satisfies: x.key <= y.key } } public class BinarySearchTree { BinaryNode root = null; public Comparable find(Comparable x); public void insert(Comparable x); public void remove(Comparable x); ! Inorder traversal of a binary search … tree visits the node according to the } sorted order of their keys. 3 4

  2. Operations: Examples Implementation: Find Protected BinaryNode find(Comparable x, BinaryNode t) { while (t != null) if (x.compareTo(t.item) < 0) t = t.left; else if (x.compareTo(t.item) > 0) t = t.right; else return t; // item found error(); // item not found } Public Comparable find(Comparable x) { return find(x, root).item; } 5 6 Implementation: FindMin Implementation: Insert Protected BinaryNode Protected BinaryNode insert(Comparable x, BinaryNode t) findMin(BinaryNode t) { { if (t == null) if (t == null) t = new BinaryNode(x); error(); else if (x.compareTo(t.item) < 0) while (t.left != null) t.left = insert(x, t.left); t = t.left; else if (x.compareTo(t.item) > 0) return t; t.right = insert(x, t.right); } else // item already exists in tree return t; Public Comparable findMin() } { return findMin(root).item; } Public void insert(Comparable x) { root = insert(x, root); } 7 8

  3. Implementation: Remove: 3 cases RemoveMin I Protected BinaryNode removeMin(BinaryNode t) { if (t == null) error(); II if (t.left != null) t.left = removeMin(t.left); else t = t.right; return t; III } 9 10 Implemenation: Remove K-th Element Selection Protected BinaryNode remove(Comparable x, BinaryNode t) ! Augmenting each node with the size { of it’s subtree allows efficient location if (t == null) of the k-th element in sorted order. error(); BinaryNode findKth(int k, BinaryNode t) if (x.compareTo(t.item) < 0) { t.left = remove(x, t.left); if (t == null) else if (x.compareTo(t.item) > 0) error(); t.right = remove(x, t.right); int leftSize = (t.left != null) ? else if (t.left != null && t.right != null) t.left.size : 0; { if (k <= leftSize) t.item = findMin(t.right).item; return findKth(k, t.left); t.right = removeMin(t.right); else if (k == leftSize+1) } return t; else // Reroot tree else t = (t.left != null) ? t.left : t.right; return findKth(k - leftSize - 1, t.right); return t; } } 11 12

  4. Randomly-Built Binary Finding a Successor Search Trees ! Augmenting each node with a pointer to its parent allows efficient location of ! Definition: A randomly-built binary search the successor of a node in sorted tree over n distinct keys is a binary search order. tree that results from inserting the n keys in random order (where each permutation of the ! The successor of t is either the min of keys is equally likely) into an initially empty the right child, or the lowest ancestor tree. of t, whose left child is also an ancestor of t. ! Theorem: The average height of a randomly- built binary search tree of n distinct keys is BinaryNode successor(BinaryNode t) ( ) O lg n { if (t.right != null) return findMin(t.right); ! Corollary: The operations find, findMin, ( ) findMax, findKth, successor, have O lg n BinaryNode p = t.parent; average complexity on randomly-built binary while (p != null && t == p.right) { search trees. t = p; p = t.parent; } return p; } 13 14

Recommend


More recommend