Download presentation
Presentation is loading. Please wait.
Published byThomas Summers Modified over 9 years ago
2
Binary Search Tree 황승원 Fall 2011 CSE, POSTECH
3
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
4
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
5
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
6
5 Complexity Of Dictionary Operations get(), put() and remove() n is number of elements in dictionary
7
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.
8
7 Example Binary Search Tree 20 10 6 28 15 40 30 25 Only keys are shown.
9
8 The Operation ascend() 20 10 6 28 15 40 30 25 Do an inorder traversal. O(n) time.
10
9 The Operation get() 20 10 6 28 15 40 30 25 Complexity is O(height) = O(n), where n is number of nodes/elements.
11
10 The Operation put() 20 10 6 28 15 40 30 25 Put a pair whose key is 35. 35
12
11 The Operation put() Put a pair whose key is 7. 20 10 6 28 15 40 30 2535 7
13
12 The Operation put() 20 10 6 28 15 40 30 25 Put a pair whose key is 18. 35 7 18
14
13 The Operation put() 20 10 6 28 15 40 30 25 Complexity of put() is O(height). 35 7 18
15
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.
16
15 Remove From A Leaf Remove a leaf element. key = 7 20 10 6 28 15 40 30 2535 7 18
17
16 Remove From A Leaf (contd.) Remove a leaf element. key = 35 20 10 6 28 15 40 30 2535 7 18
18
17 Remove From A Degree 1 Node Remove from a degree 1 node. key = 40 20 10 6 28 15 40 30 2535 7 18
19
Remove From A Degree 1 Node (contd.) Remove from a degree 1 node. key = 15 20 10 6 28 15 40 30 2535 7 18
20
19 Remove From A Degree 2 Node Remove from a degree 2 node. key = 10 20 10 6 28 15 40 30 2535 7 18
21
20 Remove From A Degree 2 Node 20 10 6 28 15 40 30 25 Replace with largest key in left subtree (or smallest in right subtree). 35 7 18
22
21 Remove From A Degree 2 Node 20 10 6 28 15 40 30 25 Replace with largest key in left subtree (or smallest in right subtree). 35 7 18
23
22 Remove From A Degree 2 Node 20 8 6 28 15 40 30 25 Replace with largest key in left subtree (or smallest in right subtree). 35 7 18
24
23 Remove From A Degree 2 Node 20 8 6 28 15 40 30 25 Largest key must be in a leaf or degree 1 node. 35 7 18
25
24 Another Remove From A Degree 2 Node Remove from a degree 2 node. key = 20 20 10 6 28 15 40 30 2535 7 18
26
25 Remove From A Degree 2 Node 20 10 6 28 15 40 30 25 Replace with largest in left subtree. 35 7 18
27
26 Remove From A Degree 2 Node 20 10 6 28 15 40 30 25 Replace with largest in left subtree. 35 7 18
28
27 Remove From A Degree 2 Node 18 10 6 28 15 40 30 25 Replace with largest in left subtree. 35 7 18
29
28 Remove From A Degree 2 Node 18 10 6 28 15 40 30 25 Complexity is O(height). 35 7
30
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
31
30 Indexed Binary Search Tree Binary search tree. Each node has an additional field. leftSize = number of nodes in its left subtree
32
31 Example Indexed Binary Search Tree 20 10 6 28 15 40 30 2535 7 18 0 0 1 1 4 0 0 7 0 0 1 3 leftSize values are in red
33
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
34
33 leftSize And Rank 20 10 6 28 15 40 30 2535 7 18 0 0 1 1 4 0 0 7 0 0 1 3 sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]
35
34 get(index) And remove(index) 7 20 10 6 28 15 40 30 2535 7 18 0 0 1 1 4 0 0 0 0 1 3 sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]
36
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
37
36 Challenge O(height)=O(n) In which case? How to avoid this?
38
37 Coming Up Next READING: Ch 15 NEXT: Balanced Binary Tree (Ch 16.1~2)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.