a binary search tree implementation chapter 17. 2 chapter contents getting started an interface for...

32
A Binary Search Tree Implementation Chapter 17

Post on 21-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

A Binary Search Tree Implementation

Chapter 17

Page 2: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

2

Chapter Contents

Getting Started 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

Page 3: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

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

Page 4: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

4

Getting Started

A binary search tree of names.

Page 5: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

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

Page 6: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

6

Duplicate Entries

A binary search tree with duplicate entries.

How about we add one more Jared to above tree?

If duplicates are allowed, place the

duplicate in the entry's right subtree.

If duplicates are allowed, place the

duplicate in the entry's right subtree.

Page 7: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

7

Beginning the Class Definition

import java.util.Iterator;public class BinarySearchTree extends BinaryTree

implements SearchTreeInterface{ public BinarySearchTree()

{ super();} // end default constructorpublic BinarySearchTree(Comparable rootEntry){ super();

setRootNode(new BinaryNode(rootEntry));} // end constructor. . .

Page 8: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

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

Page 9: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

9

Searching and Retrieving

The recursive search algorithmAlgorithm bstSearch(binarySearchTree, desiredObject)// Searches a binary search tree for a given object.// Returns true if the object is found.if (binarySearchTree is empty)

return falseelse if (desiredObject == object in the root of binarySearchTree)

return trueelse if (desiredObject < object in the root of binarySearchTree)

return bstSearch(left subtree of binarySearchTree, desiredObject)else

return bstSearch(right subtree of binarySearchTree, desiredObject)

Page 10: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

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

Page 11: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

11

Adding an EntryEssential 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) 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?

Page 12: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

12

Adding an Entry Recursively

Recursively adding Chad to smaller subtrees of a binary search tree … continued →

Page 13: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

13

Adding an Entry Recursively

(ctd.) Recursively adding Chad to

smaller subtrees of a binary search tree.

Page 14: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

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

Page 15: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

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

Page 16: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

16

Removing an Entry, Node Has One Child

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

Page 17: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

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.

Page 18: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

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.

Page 19: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

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.

Page 20: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

20

Removing an Entry, Node Has Two Children

(a) A binary search tree; (b) after removing Chad;

Page 21: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

21

Removing an Entry, Node Has Two Children

(c) after removing Sean; (d) after removing Kathy.

Page 22: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

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

Page 23: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

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?

Page 24: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

24

Remove Chad and Sean using the Second method

Page 25: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

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.

Page 26: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

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

Page 27: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

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

Page 28: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

28

Efficiency of Operations

Two binary search trees that contain the same data.

Shorter tree has efficiency O(log n)

Shorter tree has efficiency O(log n)

Page 29: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

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.

Page 30: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

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.

Page 31: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

31

Importance of Balance

Some binary trees that are height balanced.

Page 32: A Binary Search Tree Implementation Chapter 17. 2 Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning

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