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 17

2 Chapter 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 Recursive Implementation Removing an Entry Whose Node is a leaf Whose Node Has One Child Whose Node has Two Children In the Root Efficiency of Operations Importance of Balance Order in Which Nodes Are Added Implementation of the ADT Dictionary

3 Getting Started A binary search tree is a binary tree
Nodes contain Comparable objects For each node in the tree The data in a node is greater than the data in the node's left subtree The data in a node is less than the data in the node's right subtree

4 A binary search tree of names.
Getting Started A binary search tree of names.

5 An Interface for the Binary Search Tree
import java.util.Iterator; public interface SearchTreeInterface extends TreeInterface { public boolean contains(Comparable entry); public Comparable getEntry(Comparable entry); public Comparable add(Comparable newEntry); public Comparable remove(Comparable entry); public Iterator getInorderIterator(); } // end SearchTreeInterface

6 Duplicate Entries A binary search tree with duplicate entries.
If duplicates are allowed, place the duplicate in the entry's right subtree. A binary search tree with duplicate entries. How about we add one more Jared to above tree?

7 Beginning the Class Definition
import java.util.Iterator; public class BinarySearchTree extends BinaryTree implements SearchTreeInterface { public BinarySearchTree() { super(); } // end default constructor public BinarySearchTree(Comparable rootEntry) { super(); setRootNode(new BinaryNode(rootEntry)); } // end constructor

8 Searching and Retrieving
Like performing a binary search of an array For a binary array Search one of two halves of the array For the binary search tree You search one of two subtrees of the binary search tree

9 Searching and Retrieving
The recursive search algorithm Algorithm bstSearch(binarySearchTree, desiredObject) // Searches a binary search tree for a given object. // Returns true if the object is found. if (binarySearchTree is empty) return false else if (desiredObject == object in the root of binarySearchTree) return true else if (desiredObject < object in the root of binarySearchTree) return bstSearch(left subtree of binarySearchTree, desiredObject) else return bstSearch(right subtree of binarySearchTree, desiredObject)

10 Traversing The SearchTreeInterface provides the method getInorderIterator Returns an inorder iterator Our class is a subclass of BinaryTree It inherits getInorderIterator This iterator traverses entries in ascending order Uses the entries' method compareTo

11 A binary search tree; (b) the same tree after adding Chad.
Adding an Entry Essential Operation to build a tree. Need to retain the relationship among tree nodes. Every addition to binary search tree adds a new leaf to the tree A binary search tree; (b) the same tree after adding Chad. Q: Try to add Miguel and then Nancy to (a). What about add Nancy and Miguel? Trees are the same or not?

12 Adding an Entry Recursively
Recursively adding Chad to smaller subtrees of a binary search tree … continued →

13 Adding an Entry Recursively
(ctd.) Recursively adding Chad to smaller subtrees of a binary search tree.

14 Removing an Entry The remove method must receive an entry to be matched in the tree If found, it is removed Otherwise the method returns null Three cases The node has no children, it is a leaf (simplest case) The node has one child The node has two children

15 Removing an Entry, Node a Leaf
(a) Two possible configurations of leaf node N; (b) the resulting two possible configurations after removing node N by setting the child reference of P to null

16 Removing an Entry, Node Has One Child
Four possible configurations of node N with one child; (b) resulting two possible configurations after removing node N. We make C a child of P instead of N

17 Removing an Entry, Node Has Two Children
Two possible configurations of node N that has two children. If we remove N, left with two orphans, P can only accept one of them, no room for both. Thus, removing N is not a option. We do not want to remove node N to remove its entry. Find a node A that is easy to remove and replace N’s entry, then remove node A.

18 Removing an Entry, Node Has Two Children
Node N and its subtrees; (a) entry a is immediately before e, b is immediately after e; (b) after deleting the node that contained a and replacing e with a.

19 Removing an Entry, Node Has Two Children
The largest entry a in node N's left subtree occurs in the subtree's rightmost node R.

20 Removing an Entry, Node Has Two Children
(a) A binary search tree; (b) after removing Chad;

21 Removing an Entry, Node Has Two Children
(c) after removing Sean; (d) after removing Kathy.

22 Removing an Entry, Node Has Two Children
Algorithm Delete the entry e from a node N that has two children Find the rightmost node R in N’s left subtree Replace the entry in node N with the entry that is in node R Delete node R

23 Removing an Entry, Node Has Two Children
Can you think of another way of finding a node R to replace the entry in node N?

24 Remove Chad and Sean using the Second method

25 Removing an Entry in the Root
It will be a special case only if we actually remove the root node. It occurs when the root has at most one child. (a) Two possible configurations of a root that has one child; (b) after removing the root.

26 Algorithm Remove(binarySearchTree, entry)
To locate the desired entry at a root, then call removeFromRoot(rootNode) //removes the entry in a given root node of a subtree If(rootNode has two children) { largestNode = node with the largest entry in the left subtree of rootNode Replace the entry in rootNode with the entry in largestNode Remove largestNode from the tree } else if (rootNode has a right child) rootNode = rootNode’s right child else rootNode = rootNode’s left child // assertion: if rootNode was a leaf, it is now null Return rootNode

27 Efficiency of Operations
Operations add, remove, getEntry require a search that begins at the root. In the worse case, searches begins at root and examine each node on a path and ends at a leaf. Maximum number of comparisons is directly proportional to the height, h of the tree These operations are O(h) Thus we desire the shortest binary search tree we can create from the data

28 Efficiency of Operations
Two binary search trees that contain the same data. Shorter tree has efficiency O(log n)

29 Efficiency of Operations
The tallest tree has height n if it contains n nodes. The tree looks like a linked chain, and the searching is like searching a linked chain. O(n). The shortest tree is full. The height of a full tree containing n nodes is log2(n+1). Thus, in the worst case, searching a full binary search tree is an O(logn). Both full and complete binary search tree can give us O(logn) performance.

30 Importance of Balance Completely balanced
Subtrees of each node have exactly same height, full tree Other trees are height balanced Subtrees of each node in the tree differ in height by no more than 1 Completely balanced or height balanced trees are balanced Concept of balance applies to general trees not just binary tree.

31 Some binary trees that are height balanced.
Importance of Balance Some binary trees that are height balanced.

32 Importance of Balance The order in which entries are added affect the shape of the tree If entries are added to an empty binary tree Best not to have them sorted first Tree is more balanced if entries are in random order


Download ppt "A Binary Search Tree Implementation"

Similar presentations


Ads by Google