• No results found

A Binary Search Tree Implementation

N/A
N/A
Protected

Academic year: 2021

Share "A Binary Search Tree Implementation"

Copied!
36
0
0

Loading.... (view fulltext now)

Full text

(1)

A Binary Search Tree Implementation

Chapter 26

(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

Iterative Implementation

Whose Node Has One Child

Whose Node has Two Children

In the Root

Iterative Implementation

Recursive Implementation

Efficiency of Operations

Importance of Balance

Order in Which Nodes Are Added

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

Getting Started

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

An Interface for the

Binary Search Tree

(7)

An Interface for the Binary Search Tree

Fig. 26-2 (ctd.) Adding an entry that matches an entry already in a binary tree.

(8)

Duplicate Entries

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.

(9)

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

. . . Note that it is serializable because its base class BinaryTree is serializable.

Note that it is serializable because its base class BinaryTree is serializable.

(10)

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

(11)

Searching and Retrieving

The 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)

(12)

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

(13)

Adding an Entry

Fig. 26-4 (a) A binary search tree;

(b) the same tree after adding Chad.

(14)

Adding an Entry Recursively

(15)

Adding an Entry Recursively

Fig. 26-5 (ctd.) Recursively adding

Chad to smaller subtrees of a binary

search tree.

(16)

Adding an Entry Recursively

Fig. 26-6 (a) The method addNode copies its argument

(17)

Adding an Entry Recursively

Fig. 26-7 After adding a node to the subtree passed to it,

addNode returns a reference to the subtree so it can be attached to the rest of the original tree.

(18)

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

(19)

19

Removing an Entry, Node a Leaf

Fig. 26-8 (a) Two possible configurations of leaf node N; (b) the resulting two possible

configurations after removing node N.

(20)

Removing an Entry,

Node Has One Child

(21)

Removing an Entry, Node Has Two Children

Fig. 26-10 Two possible configurations of node N that has two children.

(22)

Removing an Entry, Node Has Two Children

Fig. 26-11 Node N and its subtrees; (a) entry a is

(23)

Removing an Entry, Node Has Two Children

Fig. 26-12 The largest entry a in node N's left subtree occurs in the subtree's rightmost node R.

(24)

Removing an Entry,

Node Has Two Children

(25)

25

Removing an Entry, Node Has Two Children

Fig. 26-13 (c) after removing Sean;

(d) after removing Kathy.

(26)

Removing an Entry in the Root

(27)

Iterative Implementation

To locate the desired entry

The remove method given entry to be matched

If remove finds the entry, it returns the entry

If not, it returns null

The compareTo method used to make comparisons with the entries in the tree

(28)

Recursive Implementation

Details similar to adding an entry

The public remove method calls a private recursive remove method

The public remove returns removed entry

Private remove must return root of revised tree

Thus use parameter that is an instance of

ReturnObject to return the value the public

(29)

Efficiency of Operations

Operations add, remove, getEntry require a search that begins at the root

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

(30)

Efficiency of Operations

Fig. 26-15 Two binary search trees

that contain the same data.

(31)

Importance of Balance

Completely balanced

Subtrees of each node have exactly same height

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

(32)

Importance of Balance

(33)

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

(34)

Implementation of the ADT Dictionary

Interface for a dictionary

import java.util.Iterator;

public interface DictionaryInterface

{ public Object add(Object key, Object value);

public Object remove(Object key);

public Object getValue(Object key);

public boolean contains(Object key);

public Iterator getKeyIterator();

public Iterator getValueIterator();

public boolean isEmpty();

public int getSize();

(35)

35

Implementation of the ADT Dictionary

Class of data entries (can be private, internal to class Dictionary)

private class Entry implements Comparable, java.io.Serializable { private Object key;

private Object value;

private Entry(Object searchKey, Object dataValue) { key = searchKey;

value = dataValue; } // end constructor public int compareTo(Object other)

{ Comparable cKey = (Comparable)key;

return cKey.compareTo(((Entry)other).key); } // end compareTo

< The class also defines the methods getKey, getValue, and setValue;

no setKey method is provided. >

. . .

} // end Entry

(36)

Implementation of the ADT Dictionary

Beginning of class Dictionary

import java.util.Iterator;

public class Dictionary implements DictionaryInterface, java.io.Serializable

{ private SearchTreeInterface bst;

public Dictionary()

{ bst = new BinarySearchTree();|

} // end default constructor . . .

References

Related documents

Creation is binary search tree algorithm with ppt presentations and the middle element to later functions are performed on word or to search example: check the stack as far?. Deleting

This essay describes the views of Philippines livestock sector stakeholders concerning the events and issues associated with the rapid rise in hog and poultry production, based

Classic problems: Binary Tree Preorder Traversal , Binary Tree Inorder Traversal, Binary Tree Pos- torder Traversal, Word Ladder, Validate Binary Search Tree, Flatten Binary Tree

Given a non-empty binary search tree (an ordered binary tree), return the minimum data value found in that tree.. Note that it is not necessary to search the

This implementation uses a binary node to represent the root of the binary search tree and implements the methods for insertion, deletion and search recursively.. You find part of

Write a function that is given a pointer to the root of a valid binary search tree with unique elements and prints out a list of all the odd numbers stored in the binary search

evolution phenotypes The link to Artificial insemination and pollination. Cloning Punctuated Equilibrium Effects on genetic diversity Genetic Engineering and “Transgenic

Given a binary search tree, write a C function called isTreeSkewed(), whose prototype is shown below, that takes a binary search tree as its first parameter, the number of top