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.

Slides:



Advertisements
Similar presentations
CSC 205 – Java Programming II Lecture 35 April 17, 2002.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Course: Programming II - Abstract Data Types AVL TreesSlide Number 1 AVL Trees Performance of Binary Search Trees (BST) depends on how well balanced the.
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) 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
Deletion algorithm – Phase 2: Remove node or replace its with successor TreeNode deleteNode(TreeNode n) { // Returns a reference to a node which replaced.
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.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Course: Programming II - Abstract Data Types Slide Number 1 The ADT Binary Tree Definition The ADT Binary Tree is a finite set of nodes which is either.
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.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Course: Programming II - Abstract Data Types Red-Black TreesSlide Number 1 Balanced Search Trees Binary Search Tree data structures can allow insertion,
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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.
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,
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
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.
Chapter 20 Binary Search Trees
10 Binary-Search-Tree Data Structure  Binary-trees and binary-search-trees  Searching  Insertion  Deletion  Traversal  Implementation of sets using.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
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.
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.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
CSC 205 Java Programming II Lecture 28 BST Implementation.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
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.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
1 Binary Search Trees What are the disadvantages of using a linked list? What are the disadvantages of using an array- based list? Binary search trees.
Binary Tree Data Structures Binary trees and binary search trees. Searching. Insertion. Deletion. Traversal. Implementation of sets using BSTs.
Trees Chapter 15.
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
A Binary Search Tree Implementation
BST Trees
Binary Search Tree Chapter 10.
Trees.
Binary Search Trees Why this is a useful data structure. Terminology
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Binary Search Trees.
Ch. 11 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?)
Search Sorted Array: Binary Search Linked List: Linear Search
Lecture 12 CS203 1.
CSC 143 Binary Search Trees.
B.Ramamurthy Chapter 9 CSE116A,B
Trees.
CSC 205 – Java Programming II
CSC 205 – Java Programming II
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
COSC2007 Data Structures II
Presentation transcript:

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 binary tree with an ordering on the nodes, such that for each node: 1) all values in the left sub-tree are less than the value in the node; 2) all values in the right sub-tree are greater than the value in the node. Advantage Efficient procedures for insertion and retrieval of data. Wendy Jane Tom AlanEllen Bob Nancy Can you think of other binary search trees with these same items?

Course: Programming II - Abstract Data Types ADT Binary Search TreeSlide Number 2 Search Key Node in a Binary Search Tree include Comparable Objects: have a special attribute used as search key. The search key’s value uniquely identifies a node in the tree. For each node n of a binary search tree, n’s search key is greater than all the search keys in n’s left subtree; n’s search key is less than all the search keys in n’s right subtree, both the left and right subtrees of n are binary search trees. Additional operations are insertion, deletion and retrieval of items by the search key’s value, and not by position in the tree. Traversal operations for binary trees are also applicable to binary search trees.

Course: Programming II - Abstract Data Types ADT Binary Search TreeSlide Number 3 Access Procedures for Binary Search Trees The Access procedures createEmptyTree ( ), isEmpty( ), getRootElem( ), getLeftTree( ), getRightTree( ) for binary trees are unchanged. Additional access procedures are needed to add elements to and delete elements from a binary search tree according to their search key’s value: insert(key, newElem) // post: insert newElem with search key equal to key in a binary search tree // post: whose nodes have search keys that differ from the given key. delete(key) // post: delete from a binary search tree the element whose search key equals // post: key. If no such element exists, the operation fails and throws exception. retrieve(key) // post: returns the element in a binary search tree with search key equal to key. // post: returns null if no such element exists.

Course: Programming II - Abstract Data Types ADT Binary Search TreeSlide Number 4 Theorem In the worst case, the number of comparisons needed to find a particular value in a binary search tree with N nodes is proportional to log 2 (N+1) if the tree is balanced. (NB A tree is said to be balanced if the number of nodes in the left and right subtrees of any non-terminal node are approximately equal). (a) (b)(c) Wendy Jane Tom AlanEllen Bob Nancy Jane Nancy AlanEllen Bob Wendy Tom Alan Bob Ellen Jane Nancy Tom Wendy

Course: Programming II - Abstract Data Types ADT Binary Search TreeSlide Number 5 An Interface for the Binary Search Trees public interface BST,V> extends Iterable >{ public boolean contains(K searchKey); //Post: Searches for an element with key equal to searchKey in the tree. //Post: Returns false if no element is found. public V retrieve(K searchKey); //Post: Retrieves an element with key equal to searchKey from the tree. Returns either the value of //Post: the element if found, or null if no element with given searchKey exists. public void insert(K key, V newValue); //Post: Adds a new element to the tree with given key and newValue. If the tree includes already an //Post: element with the given key, it replaces its value with newValue. public void delete(K searchKey) throws TreeException; //Post: Removes an element with key equal to searchKey from the tree. Throws an //Post: exception if such element is not found. public Iterator > iterator( ); //Post: Creates an iterator that traverses all entries in the tree. }

Course: Programming II - Abstract Data Types ADT Binary Search TreeSlide Number 6 Dynamic implementation of BST root leftright key value leftright key value leftright key value class TreeNode { private K key; private V value; private TreeNode left; private TreeNode right; ……… } class LinkedBasedBST,V>{ private TreeNode root; ……… }

Course: Programming II - Abstract Data Types ADT Binary Search TreeSlide Number 7 Search Strategy in Binary Search Trees search (TreeNode node, K searchKey) // Search the binary search tree with root node for element with searchKey. // Return true if the object is found. if (node is empty) return false; //desired element not found else if (searchKey == node’s key) return true; //desired element is found else if (searchKey < node’s key) return search ( node.getLeft( ), searchKey) else return search ( Node.getRight( ), searchkey)

Course: Programming II - Abstract Data Types Jane Nancy AlanEllen Bob Wendy Tom Daniel The ADT Binary Search Tree Slide Number 8 search (TreeNode node, K searchKey) if (node is empty) return false; else if (searchKey == node’s key) return true; else if (searchKey < node’s key) return search( node.getLeft( ), searchKey ) else return search( Node.getRight( ), searchkey) Search Strategy in Binary Search Trees Animation: successful search Search for Daniel Jane Nancy AlanEllen Bob Wendy Tom Daniel node search (TreeNode node, K searchKey) if (node is empty) return false; else if (searchKey == node’s key) return true; else if (searchKey < node’s key) return search( node.getLeft( ), searchKey ) else return search( Node.getRight( ), searchKey) Jane Nancy AlanEllen Bob Wendy Tom Daniel node search (TreeNode node, K searchKey) if (node is empty) return false; else if (searchKey == node’s key) return true; else if (searchKey < node’s key) return search( node.getLeft( ), searchKey ) else return search( Node.getRight( ), searchKey) search (TreeNode node, K searchKey) if (node is empty) return false; else if (searchKey == node’s key) return true; else if (searchKey < node’s key) return search( node.getLeft( ), searchKey ) else return search( Node.getRight( ), searchKey) Jane Nancy AlanEllen Bob Wendy Tom Daniel node search (TreeNode node, K searchKey) if (node is empty) return false; else if (searchKey == node’s key) return true; else if (searchKey < node’s key) return search( node.getLeft( ), searchKey ) else return search( Node.getRight( ), searchKey) Jane Nancy AlanEllen Bob Wendy Tom Daniel node search (TreeNode node, K searchKey) if (node is empty) return false; else if (searchKey == node’s key) return true; else if (searchKey < node’s key) return search( node.getLeft( ), searchKey ) else return search( Node.getRight( ), searchKey)

Course: Programming II - Abstract Data Types ADT Binary TreeSlide Number 9 Analysis of the Search Strategy Counting comparisons : Let the BST’s size be n. If the BST has height d, the number of comparisons is at most d.  If the BST is well-balanced, its height is (log 2 (n+1)): Max. no. of comparisons = (log 2 (n+1)): Best-case time complexity is O(log n).  If the BST is ill-balanced, its depth is at most n: Max. no. of comparisons = n Worst-case time complexity is O(n).

Course: Programming II - Abstract Data Types ADT Binary Search TreeSlide Number 10 Dynamic Implementation of BST Assume the implementation of a class TreeNode public class LinkedBasedBST, V> implements BST { private TreeNode root; public boolean contains(K searchKey){ return search(root, searchKey); } public V retrieve(K key){ return retrieveElem(root, key).getValue(); } public void insert(K key, V newValue){ root = insertElem(root, key, newValue); } public void delete(K searchKey){ root = deleteElem(root, searchKey); } }

Course: Programming II - Abstract Data Types ADT Binary Search TreeSlide Number 11 TreeNode retrieveElem(TreeNode node, K searchKey){ //post: Retrieves the node with key equal to searchKey from the tree. Returns null if no //post: such node exists if ( node != null) K key = node.getKey( ); if (key == searchKey) return node; else if (searchKey < key) return retrieveElem(node.getLeft(), searchKey); else return retrieveElem(node.getRight(), searchKey); } else return null; } Implementing retrieveElem

Course: Programming II - Abstract Data Types ADT Binary Search TreeSlide Number 12 Implementing insertElem How do we identify the correct place in a binary search tree to insert the new element? We can use the search strategy: i.Imagine the new element already exists in the tree ii.Use the search strategy to search for it. iii.The search strategy will stop at an empty sub-tree. iv.This is the correct position to add the new element. v.The new element is added as a new leaf: i.e. by changing null reference of existing leaf to refer to the new node. Example: insert Frank Wendy Jane Tom AlanEllen Bob Nancy Wendy Jane Tom AlanEllen Bob NancyWendy Jane Tom AlanEllen Bob NancyWendy Jane Tom AlanEllen Bob Nancy Wendy Jane Tom AlanEllen Bob Nancy Wendy Jane Tom AlanEllen Bob Nancy Frank

Course: Programming II - Abstract Data Types ADT Binary Search TreeSlide Number 13 Implementing insertElem TreeNode insertElem(TreeNode node, K key, V newValue) // post: creates a new Node with given key and value, inserts it as leaf of the BST with // post: root given by node and returns node (i.e. the root of the modified tree). if (node is null) { { Create a new node and let node reference it; Set the value in the new node to be equal to newValue; Set the references in the new node to null; } else if (key == node.getKey( )){ replace the value in node with newValue;} else if (key < node.getKey( )) node.setLeft(insertElem(node.getLeft( ), key, newValue)) } else node.setRight(insertElem(node.getRight( ),key, newValue))} return node;

Course: Programming II - Abstract Data Types ADT Binary Search TreeSlide Number 14 Counting comparisons : No. of comparisons is the same as for BST search  Whether a BST is well-balanced or ill-balance, depends on the order of insertion. If the inserted elements are randomly ordered, the BST will probably be reasonably well-balanced. If the inserted elements happen to be in ascending (or descending) order, the BST will be extremely ill- balanced. Analysis of insertion in BST

Course: Programming II - Abstract Data Types ADT Binary TreeSlide Number 15 Main problem is how to delete a node from a binary search tree so that the new tree is still a binary search tree! i e. so to preserve the ordering property. We can use the search strategy to locate the element to delete. Suppose the element is located in a node N. How do we delete N? There are three cases: 1.N is a leaf. 2.N has only one child 3.N has two children. 1.Set the reference in N’s parent to null. P N L P N L P L P L or 2. P N L R = the largest value in the left sub-tree of N = the smallest value in the right sub-tree of N P L R 3. Implementing deleteElem

Course: Programming II - Abstract Data Types ADT Binary TreeSlide Number 16 TreeNode deleteElem(TreeNode node, K key) // post: deletes the node from the BST, whose key is equal key and returns the // post: root node of the resulting tree if (node is null) throw TreeException; else if (node.getKey( ) == key) node = deleteNode(node); else if (node.getKey( ) > key) node.setLeft(deleteElem(node.getLeft( ),key)); else node.setRight(deleteElem(node.getRight( ),key)); return node;} Implementing deleteElem

Course: Programming II - Abstract Data Types ADT Binary TreeSlide Number 17 Implementing “ deleteNode ” TreeNode deleteNode(TreeNode node) // post: delete the given node and returns the modified tree if (node is a leaf) return null; else{ if (node has only left child) return node.getLeft( ); else if (node has only right child) return node.getRight( ); else{ replacementNode = findLeftMost(node.getRight( )); newRight = deleteLeftMost(node.getRight( )); replacementNode.setRight(newRight); replacementNode.setLeft(node.getLeft( )); return replacementNode; }

Course: Programming II - Abstract Data Types ADT Binary TreeSlide Number 18 Implementing “findLeftMost” & “deleteLeftMost” TreeNode findLeftMost(TreeNode node) { // post: returns the left most node in the tree rooted at node. if (node.getLeft( ) is null) return node; else return findLeftMost(node.getLeft( )); } TreeNode deleteLeftMost(TreeNode node) { // post: deletes the left most node in the tree rooted at node. // post: returns the root of the modified sub-tree if (node.getLeft( ) is null) return node.getRight( ); else { newChild = deleteLeftMost(node.getLeft( )); node.setleft(newChild); return node; } }

Course: Programming II - Abstract Data Types ADT Binary TreeSlide Number 19 Jane Nancy AlanEllen Bob Wendy Tom Daniel node delete Bob deleteElem(TreeNode node, K key) if (node is null) throw TreeException; else if (node.getKey( ) == key) node = deleteNode(node); else if (node.getKey( ) > key) node.setLeft(deleteElem(node.getLeft( ),key)); else node.setRight(deleteElem(node.getRight( ),key)); return node;

Course: Programming II - Abstract Data Types ADT Binary TreeSlide Number 20 Jane Nancy AlanEllen Bob Wendy Tom Daniel node delete Bob deleteElem(TreeNode node, K key) if (node is null) throw TreeException; else if (node.getKey( ) == key) node = deleteNode(node); else if (node.getKey( ) > key) node.setLeft(deleteElem(node.getLeft( ),key)); else node.setRight(deleteElem(node.getRight( ),key)); return node; node =

Course: Programming II - Abstract Data Types ADT Binary TreeSlide Number 21 Jane Nancy AlanEllen Bob Wendy Tom Daniel node delete Bob node = deleteNode(node) if (node is a leaf) return null; else {if (node has only left child) return node.getLeft( ); else if (node has only right child) return node.getRight( ); else{ replacementNode = findLeftMost(node.getRight( )); newRight = deleteLeftMost(node.getRight( )); replacementNode.setRight(newRight); replacementNode.setLeft(node.getLeft( )); return replacementNode; } replacementNode Daniel

Course: Programming II - Abstract Data Types ADT Binary TreeSlide Number 22 Jane Nancy AlanEllen Bob Wendy Tom Daniel node delete Bob node = deleteNode(node) if (node is a leaf) return null; else {if (node has only left child) return node.getLeft( ); else if (node has only right child) return node.getRight( ); else{ replacementNode = findLeftMost(node.getRight( )); newRight = deleteLeftMost(node.getRight( )); replacementNode.setRight(newRight); replacementNode.setLeft(node.getLeft( )); return replacementNode;} replacementNode Daniel newRight Ellen

Course: Programming II - Abstract Data Types ADT Binary TreeSlide Number 23 Jane Nancy AlanEllen Bob Wendy Tom Daniel node delete Bob node = deleteNode(node) if (node is a leaf) return null; else {if (node has only left child) return node.getLeft( ); else if (node has only right child) return node.getRight( ); else{ replacementNode = findLeftMost(node.getRight( )); newRight = deleteLeftMost(node.getRight( )); replacementNode.setRight(newRight); replacementNode.setLeft(node.getLeft( )); return replacementNode; } replacementNode Daniel newRight Ellen

Course: Programming II - Abstract Data Types ADT Binary TreeSlide Number 24 Jane Nancy AlanEllen Bob Wendy Tom Daniel node delete Bob node = deleteNode(node) if (node is a leaf) return null; else {if (node has only left child) return node.getLeft( ); else if (node has only right child) return node.getRight( ); else{ replacementNode = findLeftMost(node.getRight( )); newRight = deleteLeftMost(node.getRight( )); replacementNode.setRight(newRight); replacementNode.setLeft(node.getLeft( )); return replacementNode;} replacementNode Daniel newRight Ellen Alan

Course: Programming II - Abstract Data Types ADT Binary TreeSlide Number 25 Jane Nancy AlanEllen Daniel Wendy Tom Daniel node delete Bob node = replacementNode deleteNode(node) if (node is a leaf) return null; else {if (node has only left child) return node.getLeft( ); else if (node has only right child) return node.getRight( ); else{ replacementNode = findLeftMost(node.getRight( )); newRight = deleteLeftMost(node.getRight( )); replacementNode.setRight(newRight); replacementNode.setLeft(node.getLeft( )); return replacementNode; } replacementNode Daniel newRight Ellen Alan

Course: Programming II - Abstract Data Types ADT Binary TreeSlide Number 26 Summary The binary search tree allows the use of a binary search-like strategy to search for an element with a specified key. An in-order traversal of a binary search tree visits the tree’s nodes in sorted order. Binary search trees come in many shapes. The height of a binary search tree with n nodes can range from a minimum of log 2 (n+1), to a maximum of n. The shape of a binary search tree determines the efficiency of its operations. The closer a binary search tree is to a balanced tree, the closer to log 2 (n) would be the maximum number of comparisons needed in order to find a particular element.