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

binary search trees
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

1

Binary Search Trees

2

Binary Search Trees

! A data structures with efficient support

for many dynamic set operations:

– Search/Find – Minimum/findMin – Maximum/findMax – Predecessor – Successor – Insert – Delete/Remove

! All these operations take time

proportional to the height of the tree.

3

Binary Tree Definition

! A binary-tree that satisfies the binary-

search-tree property:

! Let x be a node in a binary search

  • tree. Any node y in the left subtree of

x satisfies: y.key <= x.key Any node y in the right subtree of x satisfies: x.key <= y.key

! Inorder traversal of a binary search

tree visits the node according to the sorted order of their keys.

4

Implementation

class BinaryNode { Comparable item; BinaryNode left; BinaryNode right; BinaryNode(Comparable x) { this(x, null, null); } } public class BinarySearchTree { BinaryNode root = null; public Comparable find(Comparable x); public void insert(Comparable x); public void remove(Comparable x); … }

slide-2
SLIDE 2

5

Operations: Examples

6

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; }

7

Implementation: FindMin

Protected BinaryNode findMin(BinaryNode t) { if (t == null) error(); while (t.left != null) t = t.left; return t; } Public Comparable findMin() { return findMin(root).item; }

8

Implementation: Insert

Protected BinaryNode insert(Comparable x, BinaryNode t) { if (t == null) t = new BinaryNode(x); else if (x.compareTo(t.item) < 0) t.left = insert(x, t.left); else if (x.compareTo(t.item) > 0) t.right = insert(x, t.right); else // item already exists in tree return t; } Public void insert(Comparable x) { root = insert(x, root); }

slide-3
SLIDE 3

9

Remove: 3 cases

I II III

10

Implementation: RemoveMin

Protected BinaryNode removeMin(BinaryNode t) { if (t == null) error(); if (t.left != null) t.left = removeMin(t.left); else t = t.right; return t; }

11

Implemenation: Remove

Protected BinaryNode remove(Comparable x, BinaryNode t) { if (t == null) error(); if (x.compareTo(t.item) < 0) t.left = remove(x, t.left); else if (x.compareTo(t.item) > 0) t.right = remove(x, t.right); else if (t.left != null && t.right != null) { t.item = findMin(t.right).item; t.right = removeMin(t.right); } else // Reroot tree t = (t.left != null) ? t.left : t.right; return t; }

12

K-th Element Selection

! Augmenting each node with the size

  • f it’s subtree allows efficient location
  • f the k-th element in sorted order.

BinaryNode findKth(int k, BinaryNode t) { if (t == null) error(); int leftSize = (t.left != null) ? t.left.size : 0; if (k <= leftSize) return findKth(k, t.left); else if (k == leftSize+1) return t; else return findKth(k - leftSize - 1, t.right); }

slide-4
SLIDE 4

13

Finding a Successor

! Augmenting each node with a pointer

to its parent allows efficient location of the successor of a node in sorted

  • rder.

! The successor of t is either the min of

the right child, or the lowest ancestor

  • f t, whose left child is also an

ancestor of t. BinaryNode successor(BinaryNode t) { if (t.right != null) return findMin(t.right); BinaryNode p = t.parent; while (p != null && t == p.right) { t = p; p = t.parent; } return p; }

14

Randomly-Built Binary Search Trees

! Definition: A randomly-built binary search

tree over n distinct keys is a binary search tree that results from inserting the n keys in random order (where each permutation of the keys is equally likely) into an initially empty tree.

! Theorem: The average height of a randomly-

built binary search tree of n distinct keys is

! Corollary: The operations find, findMin,

findMax, findKth, successor, have average complexity on randomly-built binary search trees.

( )

n O lg

( )

n O lg