Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Binary Search Tree Implementation

Similar presentations


Presentation on theme: "A Binary Search Tree Implementation"— Presentation transcript:

1 A Binary Search Tree Implementation
Chapter 25 Adapted from Pearson Education, Inc.

2 Contents Getting Started Searching and Retrieving Traversing
An Interface for the Binary Search Tree Duplicate Entries Beginning the Class Definition Searching and Retrieving Traversing Adding an Entry A Recursive Implementation An Iterative Implementation Adapted from Pearson Education, Inc.

3 Contents Removing an Entry The Efficiency of Operations
Removing an Entry Whose Node Is a Leaf Removing an Entry Whose Node Has One Child Removing an Entry Whose Node Has Two Children Removing an Entry in the Root A Recursive Implementation An Iterative Implementation The Efficiency of Operations The Importance of Balance The Order in Which Nodes Are Added An Implementation of the ADT Dictionary Adapted from Pearson Education, Inc.

4 Objectives Decide whether a binary tree is a binary search tree
Locate a given entry in a binary search tree using fewest comparisons Traverse entries in a binary search tree in sorted order Add a new entry to a binary search tree Remove entry from a binary search tree Describe efficiency of operations on a binary search tree Use a binary search tree to implement ADT dictionary Adapted from Pearson Education, Inc.

5 Getting Started Characteristics of a binary search tree For each node
A binary tree Nodes contain Comparable objects For each node Data in a node is greater than data in node’s left subtree Data in a node is less than data in node’s right subtree Do an in-order Traversal. What do you notice? Adapted from Pearson Education, Inc.

6 Adapted from Pearson Education, Inc.
Interfaces… package TreePackage; public interface TreeInterface < T > { public T getRootData (); public int getHeight (); public int getNumberOfNodes (); public boolean isEmpty (); public void clear (); } // end TreeInterface package TreePackage; import java.util.Iterator; public interface TreeIteratorInterface < T > { public Iterator < T > getPreorderIterator (); public Iterator < T > getPostorderIterator (); public Iterator < T > getInorderIterator (); public Iterator < T > getLevelOrderIterator (); } // end TreeIteratorInterface package TreePackage; public interface BinaryTreeInterface < T > extends TreeInterface < T > , TreeIteratorInterface < T > { public void setTree (T rootData); public void setTree (T rootData, BinaryTreeInterface < T > leftTree, BinaryTreeInterface < T > rightTree); } // end BinaryTreeInterface Additional operations needed beyond basic tree operations Search Retrieve Remove Traverse Note interface, Listing 25-1 Adapted from Pearson Education, Inc.

7 Adapted from Pearson Education, Inc.
Interfaces… package TreePackage; public interface TreeInterface < T > { public T getRootData (); public int getHeight (); public int getNumberOfNodes (); public boolean isEmpty (); public void clear (); } // end TreeInterface package TreePackage; import java.util.Iterator; public interface TreeIteratorInterface < T > { public Iterator < T > getPreorderIterator (); public Iterator < T > getPostorderIterator (); public Iterator < T > getInorderIterator (); public Iterator < T > getLevelOrderIterator (); } // end TreeIteratorInterface package TreePackage; public interface BinaryTreeInterface < T > extends TreeInterface < T > , TreeIteratorInterface < T > { public void setTree (T rootData); public void setTree (T rootData, BinaryTreeInterface < T > leftTree, BinaryTreeInterface < T > rightTree); } // end BinaryTreeInterface package TreePackage; import java.util.Iterator; public interface SearchTreeInterface < T extends Comparable < ? super T >> extends TreeInterface < T > { public boolean contains (T entry); public T getEntry (T entry); public T add (T newEntry); public T remove (T entry); public Iterator < T > getInorderIterator (); } // end SearchTreeInterface Adapted from Pearson Education, Inc.

8 Adapted from Pearson Education, Inc.
Interfaces… package TreePackage; public interface TreeInterface < T > { public T getRootData (); public int getHeight (); public int getNumberOfNodes (); public boolean isEmpty (); public void clear (); } // end TreeInterface package TreePackage; import java.util.Iterator; public interface TreeIteratorInterface < T > { public Iterator < T > getPreorderIterator (); public Iterator < T > getPostorderIterator (); public Iterator < T > getInorderIterator (); public Iterator < T > getLevelOrderIterator (); } // end TreeIteratorInterface package TreePackage; import java.util.Iterator; public class BinarySearchTree < T extends Comparable < ? super T >> extends BinaryTree < T > implements SearchTreeInterface < T > { …. } package TreePackage; import java.util.Iterator; public interface SearchTreeInterface < T extends Comparable < ? super T >> extends TreeInterface < T > { public boolean contains (T entry); public T getEntry (T entry); public T add (T newEntry); public T remove (T entry); public Iterator < T > getInorderIterator (); } // end SearchTreeInterface Adapted from Pearson Education, Inc.

9 Adding an entry that matches an entry already in a binary search tree
Adapted from Pearson Education, Inc.

10 Duplicate Entries For simplicity we do not allow duplicate entries
Add method prevents this Modify definition to allow duplicates (IF wanted) Data in node is greater than data in node’s left subtree Data in node is less than or equal to data in node’s right subtree Adapted from Pearson Education, Inc.

11 A binary search tree with duplicate entries
Inorder traversal of the tree visits duplicate entry Jared immediately after visiting the original Jared. Adapted from Pearson Education, Inc.

12 Beginning the Class Definition
Source code of class BinarySearchTree, Listing 25-2 Note methods which disable setTree from BinaryTree Check out add(“Chad”) (recursive) Check out add(“Chad”) (iterative) Adapted from Pearson Education, Inc.

13 Recursively adding Chad to smaller subtrees of a binary search tree
Adapted from Pearson Education, Inc.

14 Recursively adding Chad to smaller subtrees of a binary search tree
Adapted from Pearson Education, Inc.

15 Removing a node First step is to find the node to be removed
Start searching from root Once you found it, identify the case: Is it a leaf node? Is it a node with one child? Is it a node with two children? Adapted from Pearson Education, Inc.

16 Removing a leaf node Adapted from Pearson Education, Inc.

17 Deleting a node that has one child
50 65 40 20 57 10 22 52 60 Adapted from Pearson Education, Inc.

18 Deleting a node that has one child
50 20 65 10 57 22 52 60 Adapted from Pearson Education, Inc.

19 Deleting a node that has one child
50 65 40 20 57 10 22 52 60 Adapted from Pearson Education, Inc.

20 Deleting a node that has one child
50 57 40 20 52 60 10 22 Adapted from Pearson Education, Inc.

21 Deleting a node that has two children
Two possible configurations Cant remove it directly But can substitute a value from either: A leaf node A node with one child What can you tell me about the value of a relative to e, and relative to all other nodes in that subtree? How is that helpful? Adapted from Pearson Education, Inc.

22 Deleting Chad Step 1- identify the case
A node with 2 children Step 2 – get a replacement value Use right-most child in left subtree In this case Brittany It’s a leaf node Step 3- delete original node containing Brittany Brittany Adapted from Pearson Education, Inc.

23 More removes After removing Chad Now removing Sean After removing Sean
Now removing Kathy Adapted from Pearson Education, Inc.

24 Details of removing Kathy
Identify replacement Doug has one child Remove original node containing Doug Doug Adapted from Pearson Education, Inc.

25 Removing the root e What if the root had two children?
And what if the root is a leaf node? Adapted from Pearson Education, Inc.

26 Efficiency of Operations
Operations add, remove, and getEntry require search that begins at root Worst case: Searches begin at root and examine each node on path that ends at leaf Number of comparisons each operation requires, directly proportional to height h of tree Operations add, remove, and getEntry are O(h) What is the height of a tree with n nodes? Adapted from Pearson Education, Inc.

27 Order in Which Nodes Added
Add data to binary search tree in random order Expect tree whose operations are O(log n). Shortest tree with n nodes has height log(n) Adding nodes in sorted order results in tall tree, low efficiency operations Tallest tree with n nodes has height n What is the height of a tree with n nodes? Adapted from Pearson Education, Inc.

28 Importance of Balance Full binary search tree not necessary to get O(log n) performance Complete tree will also give O(log n) performance Completely balanced tree Subtrees of each node have exactly same height Height balanced tree Subtrees of each node in tree differ in height by no more than 1 Adapted from Pearson Education, Inc.

29 Implementation of the ADT Dictionary
Recall interface for a dictionary from Ch 19, section 4 Adapted from Pearson Education, Inc.

30 Implementation of the ADT Dictionary
Consider a dictionary implementation that uses balanced search tree to store its entries A class of data objects that will contain both search key and associated value Note listing of such a class, Listing 25-3 Adapted from Pearson Education, Inc.

31 End Chapter 25 Adapted from Pearson Education, Inc.


Download ppt "A Binary Search Tree Implementation"

Similar presentations


Ads by Google