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

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Course: Programming II - Abstract Data Types ADT Binary Search TreeSlide Number 1 The ADT Binary Search Tree Definition The ADT Binary Search Tree is a.
Dictionaries Chapter Chapter Contents Specifications for the ADT Dictionary Entries and methods Using the ADT Dictionary English Dictionary Telephone.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
Binary Search Trees1 Part-F1 Binary Search Trees   
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
1 Binary Search Trees (BSTs). 2 Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Marc Smith and Jim Ten Eyck
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
1 Joe Meehean.  Important and common problem  Given a collection, determine whether value v is a member  Common variation given a collection of unique.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Binary search trees. What’s on the menu? ADT DictionaryBST & Algorithms SimulatorComplexity.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
CSC 213 – Large Scale Programming Lecture 17: Binary Search Trees.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Topic 19 Binary Search Trees "Yes. Shrubberies are my trade. I am a shrubber. My name is 'Roger the Shrubber'. I arrange, design, and sell shrubberies."
1 Trees, Trees, and More Trees. 2 By looking at forests of terms, awesome animations, and complete examples, we hope to get at the root of trees. Hopefully,
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees.
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
Chapter 8 Binary Search Trees. Chapter 8: Binary Search Trees 8.1 – Trees 8.2 – The Logical Level 8.3 – The Application Level 8.4 – The Implementation.
Binary Search Trees CS340. Overview of a Binary Search Tree A set of nodes T is a binary search tree if either of the following is true T is empty If.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Trees Chapter 15.
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
A Binary Search Tree Implementation
BST Trees
Efficiency of in Binary Trees
Binary Search Tree (BST)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Trees.
Binary Search Tree Chapter 10.
Trees.
A Binary Search Tree Implementation
Binary Tree Applications
Binary Search Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Binary Search Trees.
CSC 143 Binary Search Trees.
Trees.
A Binary Search Tree Implementation
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

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 Recursive Implementation Removing an Entry Whose Node is a leaf 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 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 Getting Started Fig 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 An Interface for the Binary Search Tree Fig Adding an entry that matches an entry already in a binary tree … continued →

7 An Interface for the Binary Search Tree Fig (ctd.) Adding an entry that matches an entry already in a binary tree.

8 Duplicate Entries Fig A binary search tree with duplicate entries. 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.

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 (a) A binary search tree; (b) the same tree after adding Chad.

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

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

16 Adding an Entry Recursively Fig (a) The method addNode copies its argument (null) to its local parameter rootNode ; (b) rootNode references a new node that contains Chad

17 Adding an Entry Recursively Fig 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 The node has two children

19 Removing an Entry, Node a Leaf Fig (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 Fig (a) Four possible configurations of node N with one child; (b) resulting two possible configurations after removing node N.

21 Removing an Entry, Node Has Two Children Fig Two possible configurations of node N that has two children.

22 Removing an Entry, Node Has Two Children Fig 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.

23 Removing an Entry, Node Has Two Children Fig 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 Fig (a) A binary search tree; (b) after removing Chad;

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

26 Removing an Entry in the Root Fig (a) Two possible configurations of a root that has one child; (b) after removing 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 remove needs

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 Two binary search trees that contain the same data. Shorter tree has efficiency O(log n)

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 Fig Some binary trees that are height balanced.

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(); public void clear(); } // end DictionaryInterface

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... } // 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... } // end Dictionary