A Binary Search Tree Implementation

Slides:



Advertisements
Similar presentations
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
Advertisements

© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
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.
Tree Implementations Chapter Nodes in a Binary Tree The most common way uses a linked structure with each node represent each element in a binary.
Trees. 2 Tree Concepts Previous data organizations place data in linear order Some data organizations require categorizing data into groups, subgroups.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Tree Implementations Chapter 24 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.
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.
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
CSC 213 – Large Scale Programming Lecture 17: Binary Search Trees.
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.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Tree Implementations Chapter Chapter Contents The Nodes in a Binary Tree An Interface for a Node An implementation of BinaryNode An Implementation.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Trees Chapter 15.
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
A Binary Search Tree Implementation
Binary search tree. Removing a node
Binary Search Tree (BST)
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Section 8.1 Trees.
Binary Search Trees < > = Binary Search Trees
Chapter 16 Tree Implementations
Binary Search Trees Why this is a useful data structure. Terminology
Binary Tree and General Tree
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.
Tree Implementations (plus briefing about Iterators)
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Binary Search Trees < > =
Binary Search Trees < > = Binary Search Trees
Slides by Steve Armstrong LeTourneau University Longview, TX
Chapter 16 Tree Implementations
Binary Search Trees.
Binary Search Trees A special case of a Binary Tree
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Trees.
A Binary Search Tree Implementation
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

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

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.

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.

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.

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.

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.

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.

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.

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

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.

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.

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.

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

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

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.

Removing a leaf node Adapted from Pearson Education, Inc.

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

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

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

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

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.

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.

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

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

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.

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.

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.

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.

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

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.

End Chapter 25 Adapted from Pearson Education, Inc.