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.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
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   
Data Structures Topic #9. Today’s Agenda Continue Discussing Trees Examine the algorithm to insert Examine the algorithm to remove Begin discussing efficiency.
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
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.
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.
1 Chapter 25 Trees Iterators Heaps Priority Queues.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
© 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.
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."
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
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.
Binary Search Trees (BST)
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
Trees Chapter 15.
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
BST Trees
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
COMP 103 Binary Search Trees.
Chapter 16 Tree Implementations
Topic 18 Binary Search Trees
A Binary Search Tree Implementation
Binary Tree and General Tree
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.
Topic 19 Binary Search Trees
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
(2,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) Trees
Chapter 16 Tree Implementations
A Robust Data Structure
Binary Search Trees.
Binary Search Trees A special case of a Binary Tree
CSC 143 Binary Search Trees.
B.Ramamurthy Chapter 9 CSE116A,B
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 17

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

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

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

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

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?

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 . . .

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

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)

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

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?

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

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

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

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

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

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.

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.

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.

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

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

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

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?

Remove Chad and Sean using the Second method

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.

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

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

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

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.

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.

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

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