Binary Search Trees CSE POSTECH Search Trees l
Binary Search Trees CSE, POSTECH
Search Trees l Search trees are ideal for implementing dictionaries – – l Similar or better performance than skip lists and hash tables Particularly ideal for accessing data sequentially or by rank In this chapter, we will learn – – Binary search trees Indexed binary search trees
Binary Search Tree Definition l A binary tree that may be empty. A nonempty binary search tree satisfies the following properties: 1. Each node has a key (or value), 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
Examples of Binary Trees Figure 14. 1 Binary Trees Which of the above trees are binary search trees? (b) and (c) l Why isn’t (a) a binary search tree? It violates the property #3 l
Indexed Binary Search Trees Definition l Binary search tree. l Each node has an additional field ‘Left. Size’. l Support search and delete operations by rank as well as all the binary search tree operations. l Left. Size – – the number of elements in its left subtree the rank of an element with respect to the elements in its subtree (e. g. , the fourth element in sorted order)
Indexed Binary Search Tree Example 7 20 4 10 1 6 0 0 15 1 2 0 3 40 8 1 30 0 18 0 25 0 35 7 • What is the Leftsize for each node? • Left. Size values are in red.
Left. Size and Rank l l l 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 Left. Size(x) = rank(x) with respect to elements in the subtree rooted at x See Figure 14. 2 for more examples
Exercise l Do Exercise 14. 1
The bs. Tree ADT l l l See ADT 14. 1 for bs. Tree ADT See ADT 14. 2 for Indexed. BSTree ADT See Programs 14. 1 and 14. 2 for C++ abstract class definitions for bs. Tree and Indexed. BSTree
The Class binary. Search. Tree l l Since the number of elements in a binary search tree as well as its shape changes as operations are performed, a binary search tree is usually represented using the linked representation of Section 11. 4. 2 We can define binary. Search. Tree as a derived class of linked. Binary. Tree (Section 11. 8)
The Operation Ascend() 20 10 6 2 40 15 8 30 25 • How can we output all elements in ascending order of keys? • Do an inorder traversal (left, root, right). • What would be the output? • 2, 6, 8, 10, 15, 20, 25, 30, 40
The Operation Search(key, e) l l l l Search begins at the root If the root is NULL, the search tree is empty and the search fails. If key is less than the root, then left subtree is searched If key is greater than the root, then right subtree is searched If key equals the root, then the search terminates successfully The time complexity for search is O(height) See Program 11. 4 for the search operation code
The Operation Insert(key, e) l l l To insert a new element e into a binary search tree, we must first verify that its key does not already exist by performing a search in the tree If the search is successful, we do not insert If the search is unsuccessful, then the element is inserted at the point the search terminated – l l l Why insert it at that point? The time complexity for insert is O(height) See Figure 14. 3 for examples See Program 14. 5 for the insert operation code
Insert Example We wish to insert an element with the key 35. Where should it be inserted? 20 10 6 2 40 15 8 30 25 35
Insert Example Insert an element with the key 7. 20 10 6 2 15 8 7 40 30 25 35
Insert Example Insert an element with the key 18. 20 10 6 2 15 8 7 40 30 18 25 35
The Operation Delete(key, e) l For deletion, there are three cases for the element to be deleted: 1. 2. 3. Element is in a leaf. Element is in a degree 1 node (i. e. , has exactly one nonempty subtree). Element is in a degree 2 node (i. e. , has exactly two nonempty subtrees).
Case 1: Delete from a Leaf • For case 1, we can simply discard the leaf node. • Example, delete a leaf element. key=7 20 10 6 2 15 8 7 40 30 18 25 35
Case 1: Delete from a Leaf Delete a leaf element. key=35 20 10 6 2 40 15 8 30 18 25 35
Case 2: Delete from a Degree 1 Node 20 10 6 2 40 15 8 30 18 25 • Which nodes have a degree 1? • Example: Delete key=40
Case 2: Delete from a Degree 1 Node Delete from a degree 1 node. key=15 20 10 6 2 15 8 30 18 25
Case 3: Delete from a Degree 2 Node 20 10 6 2 40 15 8 30 18 7 • Which nodes have a degree 2? • Example: Delete key=10 25 35
Case 3: Delete from a Degree 2 Node 20 10 6 2 40 15 8 30 18 25 35 7 • Replace with the largest key in the left subtree (or the smallest in the right subtree) • Which node is the largest key in the left subtree?
Case 3: Delete from a Degree 2 Node 20 8 6 2 40 15 8 30 18 25 35 7 The largest key must be in a leaf or degree 1 node.
Case 3: Delete from a Degree 2 Node l l Note that the node with largest key in the left subtree (as well as that with smallest in the right subtree) is guaranteed to be in a node with either zero or one nonempty subtree How can we find the node with largest key in the left subtree of a node? by moving to the root of that subtree and then following a sequence of right-child pointers until we reach a node whose right-child pointer is NULL l How can we find the node with smallest key in the right subtree of a node? by moving to the root of that subtree and then following a sequence of left-child pointers until we reach a node whose left-child pointer is NULL
Another Delete from a Degree 2 Node • Delete from a degree 2 node. key=20 • Replace with the largest in the left subtree. 20 10 6 2 15 8 7 40 30 18 25 35
Another Delete from a Degree 2 Node 18 The result is 10 6 2 40 15 8 30 25 35 7 • The time complexity of delete is O(height). • See more delete examples in Figure 14. 4 • See Program 14. 6 for the delete operation code
Binary Search Trees with Duplicates l l We can remove the requirement that all elements in a binary search tree need distinct keys How? – – l 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
The Class d. BSTree l l l The binary search tree with duplicates (d. BSTree) We can implement this by changing the while loop of binary. Search. Tree: : Insert (Program 14. 5) See Program 14. 7 for the new while loop for d. BSTree
Indexed Binary Search Tree – Search & Delete l l l Index. Search(rank) returns the rankth element Index. Delete(rank) deletes the rankth element If rank = x. Left. Size, desired element is x. element. If rank < x. Left. Size, desired element is rankth element in left subtree of x. If rank > x. Left. Size, desired element is (rank-(x. Left. Size+1))th element in right subtree of x. Read Section 14. 5
Indexed Binary Search Example 7 20 4 10 1 6 0 0 15 1 2 0 3 40 8 1 30 0 18 0 25 0 35 7 • What is the 4 th element in this Indexed. BST? • What is the 10 th element in this Indexed. BST?
READING l l Do Exercise 14. 7 Read Sections 14. 1 -14. 5
- Slides: 32