Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.

Similar presentations


Presentation on theme: "A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions."— Presentation transcript:

1 A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano

2 Binary Search Tree For each node in a binary search tree  Node’s data is greater than all data in node’s left subtree  Node’s data is less than all data in node’s right subtree Every node in a binary search tree is the root of a binary search tree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

3 Binary Search Tree FIGURE 25-1 A binary search tree of names © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

4 Interface for the Binary Search Tree LISTING 25-1 An interface for a search tree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

5 Interface for the Binary Search Tree LISTING 25-1 An interface for a search tree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

6 Understanding the Specifications Methods will use return values instead of exceptions to indicate whether an operation has failed  getEntry, returns same entry it is given to find  getEntry returns an object in tree and matches given entry according to the entry’s compareTo method © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

7 Understanding the Specifications FIGURE 25-2 Adding an entry that matches an entry already in a binary search tree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

8 Understanding the Specifications FIGURE 25-2 Adding an entry that matches an entry already in a binary search tree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

9 Duplicate Entries If any entry e has a duplicate entry d, we arbitrarily require that d occur in the right subtree of e’s node For each node in a binary search tree:  Data in a node is greater than data in node’s left subtree  Data in a node is less than or equal to data in node’s right subtree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

10 Duplicate Entries FIGURE 25-3 A binary search tree with duplicate entries © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

11 Beginning the Class Definition LISTING 25-2 An outline of the class BinarySearchTree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

12 Beginning the Class Definition LISTING 25-2 An outline of the class BinarySearchTree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

13 Searching and Retrieving Recursive algorithm to search a binary search tree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

14 Searching and Retrieving Algorithm that describes actual implementation more closely © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

15 Searching and Retrieving The method getEntry uses findEntry © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

16 Searching and Retrieving The method getEntry uses findEntry © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

17 Searching and Retrieving Method contains can simply call getEntry to see whether a given entry is in the tree: © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

18 Adding an Entry FIGURE 25-4 (a) A binary search tree; © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

19 Adding an Entry FIGURE 25-4 (b) the same tree after adding Chad © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

20 Recursive Implementation FIGURE 25-5 Recursively adding Chad to smaller subtrees of a binary search tree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

21 Recursive Implementation FIGURE 25-5 Recursively adding Chad to smaller subtrees of a binary search tree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

22 Recursive Implementation Recursive algorithm for adding a new entry © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

23 Recursive Implementation Recursive algorithm for adding a new entry © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

24 Recursive Implementation Handle the addition to an empty binary search tree as a special case © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

25 Recursive Implementation Private recursive method addEntry © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

26 Recursive Implementation The public method add © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

27 Iterative Implementation Iterative algorithm for adding a new entry © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

28 Iterative Implementation Iterative algorithm for adding a new entry © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

29 Iterative Implementation Iterative algorithm for adding a new entry © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

30 Iterative Implementation Iterative implementation of the method addEntry © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

31 Iterative Implementation Iterative implementation of the method addEntry © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

32 Removing an Entry Whose Node Is a Leaf FIGURE 25-6 (a) Two possible configurations of a leaf node N; (b) the resulting two possible configurations after removing node © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

33 Removing an Entry Whose Node Has One Child FIGURE 25-7 Removing a node N from its parent node P when N has one child and is itself (a) a left child © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

34 Removing an Entry Whose Node Has One Child FIGURE 25-7 Removing a node N from its parent node P when N has one child and is itself (b) a right child © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

35 Removing an Entry Whose Node Has Two Children FIGURE 25-8 Two possible configurations of a node N that has two children © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

36 Removing an Entry Whose Node Has Two Children FIGURE 25-9 Node N and its subtrees: (a) the entry a is immediately before the entry e, and b is immediately after e; (b) after deleting the node that contained a and replacing e with a © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

37 Removing an Entry Whose Node Has Two Children FIGURE 25-10 The largest entry a in node N’s left subtree occurs in the subtree’s rightmost node R © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

38 Removing an Entry Whose Node Has Two Children FIGURE 25-11 (a) A binary search tree; (b) after removing Chad © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

39 Removing an Entry Whose Node Has Two Children FIGURE 25-11 (c) after removing Sean; (d) after removing Kathy © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

40 Removing an Entry in the Root FIGURE 25-12 (a) Two possible configurations of a root that has one child; (b) after removing the root © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

41 Recursive Implementation Recursive algorithm describes the method’s logic at a high level © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

42 Recursive Implementation The private method removeEntry © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

43 Recursive Implementation The private method removeEntry © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

44 Recursive Implementation The algorithm removeFromRoot © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

45 Recursive Implementation The private method removeFromRoot © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

46 Recursive Implementation The private method removeFromRoot © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

47 Recursive Implementation The private method findLargest © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

48 Recursive Implementation The private method removeLargest © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

49 Iterative Implementation Pseudocode that describes what remove must do: © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

50 Iterative Implementation The public method remove © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

51 Iterative Implementation The public method remove © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

52 Iterative Implementation The private method findNode © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

53 Iterative Implementation Pseudocode for the private method getNodeToRemove © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

54 Iterative Implementation Implementation of the private method getNodeToRemove © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

55 Iterative Implementation The private method removeNode © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

56 Efficiency of Operations For tree of height h  The operations add, remove, and getEntry are O(h) If tree of n nodes has height h = n  These operations are O(n) Shortest tree is full  Results in these operations being O(log n) © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

57 Efficiency of Operations FIGURE 25-13 Two binary search trees that contain the same data © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. O(n) O(log n)

58 The Importance of Balance Do not need a full binary search tree to get O(log n) performance Complete tree will also give us O(log n) performance. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

59 Order in Which Nodes Are Added Order in which you add entries to a binary search tree affects the shape of the tree If you add entries into an initially empty binary search tree, do not add them in sorted order. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

60 Implementation of the ADT Dictionary Interface for a dictionary © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

61 Implementation of the ADT Dictionary LISTING 25-3 An outline of an implementation of the ADT dictionary that uses a binary search tree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

62 Implementation of the ADT Dictionary LISTING 25-3 An outline of an implementation of the ADT dictionary that uses a binary search tree © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

63 Implementation of the ADT Dictionary Dictionary’s add method © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

64 Implementation of the ADT Dictionary Beginning of Dictionary’s remove method © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

65 Implementation of the ADT Dictionary Iterators © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

66 Implementation of the ADT Dictionary Iterators © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

67 Implementation of the ADT Dictionary Iterators © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

68 End Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.


Download ppt "A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions."

Similar presentations


Ads by Google