1 / 32

Binary Search Trees

Binary Search Trees. CSE, POSTECH. 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

base
Download Presentation

Binary Search Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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: • Each node has a key (or value), and no two nodes have the same key (i.e., all keys are distinct). • 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 larger than that in x. • The left and right subtrees of the root are also binary search trees

  4. Figure 14.1 Binary Trees 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

  5. Indexed Binary Search Trees Definition • Binary search tree. • Each node has an additional field ‘LeftSize’. • Support search and delete operations by rankas 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. 7 20 4 3 10 40 1 0 1 6 15 30 0 1 0 0 0 2 8 18 25 35 0 7 Indexed Binary Search Tree Example • What is the Leftsize for each node? • LeftSize values are in red.

  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 representationof Section 11.4.2 • We can define binarySearchTree as a derived class of linkedBinaryTree (Section 11.8)

  11. 20 10 40 6 15 30 2 8 25 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

  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. 20 10 40 6 15 30 2 8 25 35 Insert Example We wish to insert an element with the key 35. Where should it be inserted?

  15. 20 10 40 6 15 30 2 8 25 35 7 Insert Example Insert an element with the key 7.

  16. 20 10 40 6 15 30 2 8 18 25 35 7 Insert Example Insert an element with the key 18.

  17. The Operation Delete(key, e) • For deletion, there are three cases for the element to be deleted: • 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).

  18. 20 10 40 6 15 30 2 8 18 25 35 7 Case 1: Delete from a Leaf • For case 1, we can simply discard the leaf node. • Example, delete a leaf element. key=7

  19. 20 10 40 6 15 30 2 8 18 25 35 Case 1: Delete from a Leaf Delete a leaf element. key=35

  20. 20 10 40 6 15 30 2 8 18 25 Case 2: Delete from a Degree 1 Node • Which nodes have a degree 1? • Example: Delete key=40

  21. 20 10 6 15 30 2 8 18 25 Case 2: Delete from a Degree 1 Node Delete from a degree 1 node. key=15

  22. 20 10 40 6 15 30 2 8 18 25 35 7 Case 3: Delete from a Degree 2 Node • Which nodes have a degree 2? • Example: Delete key=10

  23. 20 10 40 6 15 30 2 8 18 25 35 7 Case 3: Delete from a Degree 2 Node • 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?

  24. 20 8 40 6 15 30 2 8 18 25 35 7 Case 3: Delete from a Degree 2 Node The largest key must be in a leaf or degree 1 node.

  25. Case 3: Delete from a Degree 2 Node • 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. 20 10 40 6 15 30 2 8 18 25 35 7 Another Delete from a Degree 2 Node • Delete from a degree 2 node. key=20 • Replace with the largest in the left subtree.

  27. 18 10 40 6 15 30 2 8 25 35 7 Another Delete from a Degree 2 Node The result is • 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

  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 rankth element • IndexDelete(rank) deletes the rankth element • If rank = x.LeftSize, desired element is x.element. • If rank < x.LeftSize, desired element is rankth 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. 7 20 4 3 10 40 1 0 1 6 15 30 0 1 0 0 0 2 8 18 25 35 0 7 Indexed Binary Search Example • What is the 4th element in this IndexedBST? • What is the 10th element in this IndexedBST?

  32. READING • Do Exercise 14.7 • Read Sections 14.1-14.5

More Related