Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees CSE, POSTECH. Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than skip lists and.

Similar presentations


Presentation on theme: "Binary Search Trees CSE, POSTECH. Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than skip lists and."— Presentation transcript:

1 Binary Search Trees CSE, POSTECH

2 Search Trees Search trees are ideal for implementing dictionaries – 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

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 (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

4 Examples of Binary Trees Which of the above trees are binary search trees?  (b) and (c) Why isn’t (a) a binary search tree?  It violates the property #3 Figure 14.1 Binary Trees

5 Indexed Binary Search Trees Definition Binary search tree. Each node has an additional field ‘LeftSize’. Support search and delete operations by rank as well as all the binary search tree operations. LeftSize – 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)

6 Indexed Binary Search Tree Example What is the Leftsize for each node? LeftSize values are in red. 20 4010 6 28 1530 2535 7 18 7 4 1 0 0 1 0 3 1 000

7 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 the subtree rooted at x See Figure 14.2 for more examples

8 Exercise Do Exercise 14.1

9 The bsTree ADT See ADT 14.1 for bsTree ADT See ADT 14.2 for IndexedBSTree ADT See Programs 14.1 and 14.2 for C++ abstract class definitions for bsTree and IndexedBSTree

10 The Class binarySearchTree 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 binarySearchTree as a derived class of linkedBinaryTree (Section 11.8)

11 The Operation Ascend() 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 20 4010 6 28 1530 25

12 The Operation Search(key, e) 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

13 The Operation Insert(key, e) 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 – 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

14 Insert Example We wish to insert an element with the key 35. Where should it be inserted? 20 4010 6 28 1530 25 35

15 Insert Example Insert an element with the key 7. 20 4010 6 28 1530 2535 7

16 Insert Example Insert an element with the key 18. 20 4010 6 28 1530 2535 7 18

17 The Operation Delete(key, e) For deletion, there are three cases for the element to be deleted: 1. Element is in a leaf. 2. Element is in a degree 1 node (i.e., has exactly one nonempty subtree). 3. Element is in a degree 2 node (i.e., has exactly two nonempty subtrees).

18 Case 1: Delete from a Leaf For case 1, we can simply discard the leaf node. Example, delete a leaf element. key=7 20 4010 6 28 1530 2535 7 18

19 Case 1: Delete from a Leaf Delete a leaf element. key=35 20 4010 6 28 1530 253518

20 Case 2: Delete from a Degree 1 Node Which nodes have a degree 1? Example: Delete key=40 20 4010 6 28 1530 2518

21 Case 2: Delete from a Degree 1 Node 20 10 6 28 1530 2518 Delete from a degree 1 node. key=15

22 Case 3: Delete from a Degree 2 Node 20 4010 6 28 1530 2535 7 18 Which nodes have a degree 2? Example: Delete key=10

23 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? 20 4010 6 28 1530 2535 7 18 Case 3: Delete from a Degree 2 Node

24 20 408 6 28 1530 2535 7 18 The largest key must be in a leaf or degree 1 node. Case 3: Delete from a Degree 2 Node

25 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 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

26 Another Delete from a Degree 2 Node Delete from a degree 2 node. key=20 Replace with the largest in the left subtree. 20 4010 6 28 1530 2535 7 18

27 Another Delete from a Degree 2 Node 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 18 4010 6 28 1530 2535 7 The result is

28 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

29 The Class dBSTree The binary search tree with duplicates (dBSTree) We can implement this by changing the while loop of binarySearchTree::Insert (Program 14.5) See Program 14.7 for the new while loop for dBSTree

30 Indexed Binary Search Tree – Search & Delete IndexSearch(rank) returns the rank th element IndexDelete(rank) deletes the rank th element If rank = x.LeftSize, desired element is x.element. If rank < x.LeftSize, desired element is rank th element in left subtree of x. If rank > x.LeftSize, desired element is (rank-(x.LeftSize+1)) th element in right subtree of x. Read Section 14.5

31 Indexed Binary Search Example What is the 4 th element in this IndexedBST? 20 4010 6 28 1530 2535 7 18 7 4 1 0 0 1 0 3 1 000 What is the 10 th element in this IndexedBST?

32 READING Do Exercise 14.7 Read Sections 14.1-14.5


Download ppt "Binary Search Trees CSE, POSTECH. Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than skip lists and."

Similar presentations


Ads by Google