Topic 15 The Binary Search Tree ADT. 11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
© 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, Binary Trees, and Binary Search Trees COMP171.
BST Data Structure A BST node contains: A BST contains
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Fundamentals of Python: From First Programs Through Data Structures
TCSS 342 BST v1.01 BST addElement To addElement(), we essentially do a find( ). When we reach a null pointer, we create a new node there. void addElement(Object.
Binary Search Trees Chapter 7 Objectives
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.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Lauren Milne Summer
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 10: Binary Search Trees Java Software Structures: Designing.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Trees CSCI Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Analyze.
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 ),
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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.
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)
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.
1 Trees and Binary Search Trees Using binary trees Definition of a binary search tree Implementing binary search trees –Add Element –Remove Element Using.
Chapter 20 Binary Search Trees
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
1 Trees and Binary Search Trees Using binary trees Definition of a binary search tree Implementing binary search trees –Add Element –Remove Element Using.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Linda Shapiro Winter 2015.
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 Search Trees Chapter 7 Objectives
The Tree ADT.
Trees Chapter 15.
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
BST Trees
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
Binary Search Tree (BST)
Trees and Binary Search Trees
Trees Tree nomenclature Implementation strategies Traversals
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
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.
Find in a linked list? first last 7  4  3  8 NULL
Lecture 12 CS203 1.
Binary Trees, Binary Search Trees
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
Trees.
Binary Trees, Binary Search Trees
Chapter 20 Binary Search Trees
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Topic 15 The Binary Search Tree ADT

11-2 Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such that the data in any internal node is Greater than the data in any node in its left subtree Less than the data in any node in its right subtree Note: this definition does not allow duplicates; some definitions do, in which case we could say “less than or equal to”

11-3 Examples: are these Binary Search Trees?

11-4 Observations: What is in the leftmost node? What is in the rightmost node? Discussion

11-5 BST Operations A binary search tree is a special case of a binary tree So, it has all the operations of a binary tree It also has operations specific to a BST : add an element (requires that the BST property be maintained) remove an element (requires that the BST property be maintained) remove the maximum element remove the minimum element

11-6 Searching in a BST Why is it called a binary search tree? Data is stored in such a way, that it can be more efficiently found than in an ordinary binary tree

11-7 Algorithm to search for an item in a BST Compare data item to the root of the (sub)tree If data item = data at root, found If data item < data at root, go to the left; if there is no left child, data item is not in tree If data item > data at root, go to the right; if there is no right child, data item is not in tree Searching in a BST

11-8 Search Operation – a Recursive Algorithm To search for a value k; returns true if found or false if not found If the tree is empty, return false. If k == value at root return true: we’re done. If k < value at root return result from search for k in the left subtree Else return result from search for k in the right subtree.

11-9 Search Operation Search for 13: visited nodes are coloured yellow; return false when node containing 12 has no right child Search for 22: return false when node containing 23 has no left child

11-10 BST Operations: add To add an item to a BST: Follow the algorithm for searching, until there is no child Insert at that point So, new node will be added as a leaf (We are assuming no duplicates allowed)

11-11 Add Operation To insert 13: Same nodes are visited as when searching for 13. Instead of returning false when the node containing 12 has no right child, build the new node, attach it as the right child of the node containing 12, and return true. 13

11-12 Add Operation – an Algorithm To insert a value k into a tree, returning true if successful and false if not Build a new node for k. If tree is empty add new node as root node, return true. If k == value at root return false (no duplicates allowed). If k < value at root If root has no left child add new node as left child of root, return true Else insert k into left subtree of root. If k > value at root If root has no right child add new node as right child of root, return true Else insert k into the right subtree of root.

11-13 Example: Adding Elements to a BST 26 1: Add : Add : Add : Add : Add : Add

11-14 BST Operations: Remove Case 1: value to be removed is in a leaf node Node can be removed and the tree needs no further rearrangement Case 2: value to be removed is in an interior node Why can’t we just change the link from its parent node to a successor node? We can replace the node with its inorder predecessor (or successor) Complex, and we will not implement this

11-15 Example: Removing BST Elements 1: Initial tree : Remove

: Remove : Remove

: Remove : Remove

11-18 BST Operations: Remove Minimum Recall that leftmost node contains the minimum element Three cases: 1)root has no left child (so, root is minimum) its right child becomes the root 2)leftmost node is a leaf set its parent’s left child to null 3)leftmost node is internal the right child of the node to be removed becomes the parent’s left child

11-19 Example: Removing Minimum BST Element 1: Initial tree : Remove minimum Case 3: internal node removed

: Remove minimum : Remove minimum Case 3: internal node removed Case 2: leaf node removed

: Remove minimum : Remove minimum Case 1: root node removed Case 2: leaf node removed

11-22 Binary Search Tree Traversals Consider the traversals of a binary search tree: preorder, inorder, postorder, level- order Try the traversals on the example on the next page Is there anything special about the order of the data in the BST, for each traversal? Question: what if we wanted to visit the nodes in descending order?

11-23 Binary Search Tree Traversals Try these traversals: preorder inorder postorder level-order

11-24 Binary Search Tree ADT A BST is just a binary tree with the ordering property imposed on all nodes in the tree So, we can define the BinarySearchTreeADT interface as an extension of the BinaryTreeADT interface

11-25 public interface BinarySearchTreeADT extends BinaryTreeADT { public void addElement (T element); public T removeElement (T targetElement); public void removeAllOccurrences (T targetElement); public T removeMin( ); public T removeMax( ); public T findMin( ); public T findMax( ); } The BinarySearchTreeADT interface

11-26 UML Description of BinarySearchTreeADT > BinaryTreeADT getRoot() toString() isEmpty( ) size( ) contains( ) find( ) iteratorInOrder( ) iteratorPreOrder( ) iteratorPostOrder( ) iteratorLevelOrder( ) > BinarySearchTreeADT addElement( ) removeElement( ) removeAllOccurrences( ) removeMin( ) removeMax( ) findMin( ) findMax( )

11-27 Implementing BSTs using Links See LinkedBinarySearchTree.java Constructors: use super() addElement method (does not implement our recursive algorithm of p.12; also, allows duplicates) note the use of Comparable: so that we can use compareTo method to know where to add the new node removeMin method essentially implements our algorithm of p. 18

11-28 Implementing BSTs using Links The special thing about a Binary Search Tree is that finding a specific element is efficient! So, LinkedBinarySearchTree has a find method that overrides the find method of the parent class LinkedBinaryTree It only has to search the appropriate side of the tree It uses a recursive helper method findAgain Note that it does not have a contains method that overrides the contains of LinkedBinaryTree – why not? It doesn’t need to, because contains just calls find

11-29 Using Binary Search Trees: Implementing Ordered Lists A BST can be used to provide efficient implementations of other collections! We will examine an implementation of an Ordered List ADT as a binary search tree Our implementation is called BinarySearchTreeList.java (naming convention same as before: this is a BST implementation of a List)

11-30 BinarySearchTreeList implements OrderedListADT Which extends ListADT So it also implements ListADT So, what operations do we need to implement? add removeFirst, removeLast, remove, first, last, contains, isEmpty,size, iterator, toString But, for which operations do we actually need to write code? … Using BST to Implement Ordered List

11-31 Using BST to Implement Ordered List BinarySearchTreeList extends our binary search tree class LinkedBinarySearchTree Which extends LinkedBinaryTree So, what operations have we inherited ? addElement, removeElement, removeMin, removeMax, findMin, findMax, find getRoot, isEmpty, size, contains, find, toString, iteratorInOrder, iteratorPreOrder, iteratorPostOrder, iteratorLevelOrder

11-32 Discussion First, let’s consider some of the methods of the List ADT that we do not need to write code for: contains method: we can just use the one from the LinkedBinaryTree class What about the methods isEmpty size toString

11-33 Discussion To implement the following methods of the OrderedListADT, we can call the appropriate methods of the LinkedBinarySearchTree class (fill in the missing ones) addcall addElement removeFirstremoveMin removeLast remove first last iterator

11-34 Balanced Trees Our definition: a balanced tree has the property that, for any node in the tree, the height of its left and right subtrees can differ by at most 1 Note that conventionally the height of an empty subtree is -1

11-35 Balanced Trees Which of these trees is a balanced tree?

11-36 Analysis of BST Implementation We will now compare the linked list implementation of an ordered list with its BST implementation, making the following important assumptions: The BST is a balanced tree The maximum level of any node is log 2 (n), where n is the number of elements stored in the tree

11-37 Analysis of Ordered List Implementations: Linked List vs. Balanced BST OperationLinkedListBinarySearchTreeList removeFirstO(1)O(log 2 n) removeLastO(n)O(log 2 n) removeO(n)O(log 2 n) *but may cause tree to become unbalanced firstO(1)O(log 2 n) lastO(n)O(log 2 n) containsO(n)O(log 2 n) isEmptyO(1) sizeO(1) addO(n)O(log 2 n) *

11-38 Discussion Why is our balance assumption so important? Look at what happens if we insert the following numbers in this order without rebalancing the tree:

11-40 Degenerate Binary Trees The resulting tree is called a degenerate binary tree Note that it looks more like a linked list than a tree! But it is actually less efficient than a linked list (Why?)

11-41 Degenerate BSTs are far less efficient than balanced BSTs Consider the worst case time complexity for the add operation: O(n) for degenerate tree O(log 2 n) for balanced tree Degenerate Binary Trees

11-42 Balancing Binary Trees There are many approaches to balancing binary trees But they will not be discussed in this course …