Binary Search Tree 황승원 Fall 2011 CSE, POSTECH
2 2 Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than skip lists and hashing – Particularly ideal for accessing data sequentially or by rank Types of search trees – Binary search trees – AVL trees – Red-black trees – B-trees
3 3 Binary Search Tree Definition A binary tree that may be empty. A nonempty binary search tree satisfies the following properties 1. Each node has a (key, value) pair and no two nodes have the same key (i.e., all keys are distinct). 2. For every node x, all keys in the left subtree of x are smaller than that in x. 3. For every node x, all keys in the right subtree of x are larger than that in x. 4. The left and right subtrees of the root are also binary search trees
4 Binary Search Trees Dictionary Operations: get(key) put(key, value) remove(key) Additional operations: ascend() Elements in the ascending order get(rank) (indexed binary search tree) Get the element in the rank th order remove(rank) (indexed binary search tree) Remove the element in the rank th order
5 Complexity Of Dictionary Operations get(), put() and remove() n is number of elements in dictionary
6 Definition Of Binary Search Tree A binary tree. Each node has a (key, value) pair. For every node x, all keys in the left subtree of x are smaller than that in x. For every node x, all keys in the right subtree of x are greater than that in x.
7 Example Binary Search Tree Only keys are shown.
8 The Operation ascend() Do an inorder traversal. O(n) time.
9 The Operation get() Complexity is O(height) = O(n), where n is number of nodes/elements.
10 The Operation put() Put a pair whose key is
11 The Operation put() Put a pair whose key is
12 The Operation put() Put a pair whose key is
13 The Operation put() Complexity of put() is O(height)
14 The Operation remove() Three cases: Element is in a leaf. Element is in a degree 1 node. Element is in a degree 2 node.
15 Remove From A Leaf Remove a leaf element. key =
16 Remove From A Leaf (contd.) Remove a leaf element. key =
17 Remove From A Degree 1 Node Remove from a degree 1 node. key =
Remove From A Degree 1 Node (contd.) Remove from a degree 1 node. key =
19 Remove From A Degree 2 Node Remove from a degree 2 node. key =
20 Remove From A Degree 2 Node Replace with largest key in left subtree (or smallest in right subtree)
21 Remove From A Degree 2 Node Replace with largest key in left subtree (or smallest in right subtree)
22 Remove From A Degree 2 Node Replace with largest key in left subtree (or smallest in right subtree)
23 Remove From A Degree 2 Node Largest key must be in a leaf or degree 1 node
24 Another Remove From A Degree 2 Node Remove from a degree 2 node. key =
25 Remove From A Degree 2 Node Replace with largest in left subtree
26 Remove From A Degree 2 Node Replace with largest in left subtree
27 Remove From A Degree 2 Node Replace with largest in left subtree
28 Remove From A Degree 2 Node Complexity is O(height). 35 7
29 Binary Search Trees with Duplicates We can remove the requirement that all elements in a binary search tree need distinct keys How? – Replace “smaller” in property 2 by “smaller or equal to” – Replace “larger” in property 3 by “larger or equal to” Then binary search trees can have duplicate keys
30 Indexed Binary Search Tree Binary search tree. Each node has an additional field. leftSize = number of nodes in its left subtree
31 Example Indexed Binary Search Tree leftSize values are in red
32 leftSize And Rank Rank of an element is its position in inorder (inorder = ascending key order). [2,6,7,8,10,15,18,20,25,30,35,40] rank(2) = 0 rank(15) = 5 rank(20) = 7 leftSize(x) = rank(x) with respect to elements in subtree rooted at x
33 leftSize And Rank sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]
34 get(index) And remove(index) sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]
35 get(index) And remove(index) if index = x.leftSize desired element is x.element if index < x.leftSize desired element is index ’ th element in left subtree of x if index > x.leftSize desired element has index=(index - x.leftSize-1) in the right subtree of x
36 Challenge O(height)=O(n) In which case? How to avoid this?
37 Coming Up Next READING: Ch 15 NEXT: Balanced Binary Tree (Ch 16.1~2)