Binary Search Tree. Revised based on textbook author s notes.

Similar documents
Trees. Tree Structure Binary Tree Tree Traversals

Binary Tree Application Expression Tree. Revised based on textbook author s notes.

Recursion. Presentation Subtitle. Brad Miller David Ranum 1 12/19/2005. Luther College. Binary Search Trees. 1 Department of Computer Science

Outline. An Application: A Binary Search Tree. 1 Chapter 7: Trees. favicon. CSI33 Data Structures

n 1 i = n + i = n + f(n 1)

Data Structures CSCI C343, Fall 2014

Last week. Last week. Last week. Today. Binary Trees. CSC148 Intro. to Computer Science. Lecture 8: Binary Trees, BST

Associate Professor Dr. Raed Ibraheem Hamed

CSI33 Data Structures

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

TREES. Trees - Introduction

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

Data Structures and Algorithms

BINARY SEARCH TREES. Problem Solving with Computers-II

Binary Search Tree (3A) Young Won Lim 6/2/18

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

A set of nodes (or vertices) with a single starting point

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS350: Data Structures Binary Search Trees

Binary Trees, Binary Search Trees

Binary Search Trees. See Section 11.1 of the text.

CS 350 : Data Structures Binary Search Trees

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

Lecture 34. Wednesday, April 6 CS 215 Fundamentals of Programming II - Lecture 34 1

Lecture 16: Binary Search Trees

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Binary Search Tree (2A) Young Won Lim 5/17/18

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

CS F-11 B-Trees 1

CSC148-Section:L0301

CmpSci 187: Programming with Data Structures Spring 2015

CS 171: Introduction to Computer Science II. Binary Search Trees

Search Trees: BSTs and B-Trees

BST Deletion. First, we need to find the value which is easy because we can just use the method we developed for BST_Search.

Heaps. Heaps Priority Queue Revisit HeapSort

Binary Search Trees > = 2014 Goodrich, Tamassia, Goldwasser. Binary Search Trees 1

Binary Search Tree (3A) Young Won Lim 6/6/18

Binary Search Tree (3A) Young Won Lim 6/4/18

CSC148-Section:L0301

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Linked Lists. Linked List vs Array vs Python List Singly Linked List Bag ADT Revisited Building Linked Lists

Figure 1: A complete binary tree.

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

(2,4) Trees. 2/22/2006 (2,4) Trees 1

CS350: Data Structures Heaps and Priority Queues

CSI33 Data Structures

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Data Structures (CS 1520) Lecture 23 Name:

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Balanced Binary Search Trees. Victor Gao

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

Chapter 20: Binary Trees

Binary Trees and Binary Search Trees

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

TREES AND ORDERS OF GROWTH 7

9. Heap : Priority Queue

Trees. (Trees) Data Structures and Programming Spring / 28

CSC148 Week 7. Larry Zhang

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Binary search trees. We can define a node in a search tree using a Python class definition as follows: class SearchTree:

Announcements. Problem Set 2 is out today! Due Tuesday (Oct 13) More challenging so start early!

Search Trees. Data and File Structures Laboratory. DFS Lab (ISI) Search Trees 1 / 17

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

CS102 Binary Search Trees

CSE 502 Class 16. Jeremy Buhler Steve Cole. March A while back, we introduced the idea of collections to put hash tables in context.

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Data Structures in Java

8. Binary Search Tree

Data Structures and Algorithms

Binary Trees. Recursive definition. Is this a binary tree?

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

CSI33 Data Structures

Linked Data Structures. Linked lists. Readings: CP:AMA The primary goal of this section is to be able to use linked lists and trees.

Recall: Properties of B-Trees

Linked Data Structures. Linked lists. Readings: CP:AMA The primary goal of this section is to be able to use linked lists and trees.

Data Structures And Algorithms

Trees. Truong Tuan Anh CSE-HCMUT

STREAMS, ITERATORS, AND BINARY TREES 10

2. We ll add new nodes to the AVL as leaves just like we did for Binary Search Trees (BSTs). a) Add the key 90 to the tree?

CS350: Data Structures Red-Black Trees

In addition to the correct answer, you MUST show all your work in order to receive full credit.

CSCI Trees. Mark Redekopp David Kempe

Binary Search Trees. Motivation. Binary search tree. Tirgul 7

CSC148H Week 8. Sadia Sharmin. July 12, /29

Binary search trees (BST) Binary search trees (BST)

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

Basic Data Structures (Version 7) Name:

BINARY SEARCH TREES (CONTD) Problem Solving with Computers-II

Programming II (CS300)

Self-Balancing Search Trees. Chapter 11

Binary Search Trees. What is a Binary Search Tree?

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key and satisfies the conditions:

Transcription:

Binary Search Tree Revised based on textbook author s notes.

Search Trees The tree structure can be used for searching. Each node contains a search key as part of its data or payload. Nodes are organized based on the relationship between the keys. Search trees can be used to implement various types of containers. Most common use is with the Map ADT. 2

Binary Search Tree (BST) A binary tree in which each node contains a search key and the tree is structured such that for each interior node V: All keys less than the key in node V are stored in the left subtree of V. All keys greater than the key in node V are stored in the right subtree of V. 3

BST Example Consider the example tree 4

BST ADT bst.py # We use an unique name to distinguish this version # from others in the chapter. class BST : def init ( self ): self._root = None self._size = 0 def len ( self ): return self._size def iter ( self ): return _BSTreeIterator( self._root ) #... 5 # Storage class for the binary search tree nodes. class _BSTNode : def init ( self, key, data ): self.key = key self.data = data self.left = None self.right = None

BST Searching A search begins at the root node. The target is compared to the key at each node. The path depends on the relationship between the target and the key in the node. 6

BST Search Example Suppose we want to search for 29 in our BST. 7

BST Search Example What if the key is not in the tree? Search for key 68 in our BST. 8

BST Search Implementation class BST : #... def contains ( self, key ): return self._bstsearch( self._root, key ) is not None def valueof( self, key ): node = self._bstsearch( self._root, key ) assert node is not None, "Invalid map key." return node.value def _bstsearch( self, subtree, target ): if subtree is None : return None elif target < subtree.key : return self._bstsearch( subtree.left ) elif target > subtree.key : return self._bstsearch( subtree.right ) else : return subtree bst.py 9

BST Min or Max Key Finding the minimum or maximum key within a BST is similar to the general search. Where might the smallest key be located? Where might the largest key be located? 10

BST Min or Max Key The helper method below finds the node containing the minimum key. class BST : #... def _bstminumum( self, subtree ): if subtree is None : return None elif subtree.left is None : return subtree else : return self._bstminimum( subtree.left ) 11

BST Insertions When a BST is constructed, the keys are added one at a time. As keys are inserted A new node is created for each key. The node is linked into its proper position within the tree. The search tree property must be maintained. 12

Building a BST Suppose we want to build a BST from the key list 60 25 100 35 17 80 13

BST Insertion Building a BST by hand is easy. How do we insert an entry in program code? What happens if we use the search method from earlier to search for key 30? 14

BST Insertion We can insert the new node where the search fell off the tree. 15

BST Insert Implementation bst.py class BST : #... def add( self, key, value ): node = self._bstsearch( key ) if node is not None : node.value = value return False else : self._root = self._bstinsert( self._root, key, value ) self._size += 1 return True def _bstinsert( self, subtree, key, value ): if subtree is None : subtree = _BSTMapNode( key, value ) elif key < subtree.key : subtree.left = self._bstinsert(subtree.left, key, value) elif key > subtree.key : subtree.right = self._bstinsert(subtree.right, key, value) return subtree 16

BST Insert Steps Add 30 to our sample BST. 17

18 BST Insert Steps

BST Deletions Deleting a node from a BST is a bit more complicated. Locate the node containing the node. Delete the node. When a node is removed, the remaining nodes must preserve the search tree property. 19

BST Deletions There are three cases to consider: the node is a leaf. the node has a single child the node has two children. 20

BST Delete Leaf Node Removing a leaf node is the easiest case. Suppose we want to remove 23. 21

BST Delete Interior Node Removing an interior node with one child. Suppose we want to remove 41. We can not simply unlink the node. 22

BST Delete Interior Node After locating the node to be removed, it's child must be linked to it's parent. 23

BST Delete Interior Node The most difficult case is deleting a node with two children. Suppose we want to delete node 12. Which child should be linked to the parent? 24

BST Delete Interior Node Based on the search tree property, each node has a logical predecessor and successor. For node 12, those are 4 and 23. 25

BST Delete Interior Node We can replace to be deleted with either its logical successor or predecessor. Both will either be a leaf or an interior node with one child. We already know how to remove those nodes. 26

BST Delete Interior Node Removing an interior node with two children requires 4 steps: (1) Find the node to be deleted, N. 27

BST Delete Interior Node (2) Find the successor, S, of node N. 28

BST Delete Interior Node (3) Copy the payload from node S to node N. 29

BST Delete Interior Node (4) Remove node S from the tree. 30

BST Delete Interior Node Removing an interior node with two children requires 4 steps: Find the node to be deleted, N. Find the logical successor, S, of node N. Copy the payload from node S to node N. Remove node S from the tree. 31

BST Delete Implementation bst.py class BST : #... def remove( self, key ): assert key in self, "Invalid key." self._root = self._bstremove( self._root, key ) self._size -= 1 32

BST Delete Implementation bst.py class BST : #... def _bstremove( self, subtree, target ): if subtree is None : # not found return subtree elif target < subtree.key : # search for the left tree subtree.left = self._bstremove( subtree.left, target ) return subtree elif target > subtree.key : # search for the right tree subtree.right = self._bstremove( subtree.right, target ) return subtree else : # target == subtree.key, delete the node... 33

BST Delete Implementation bst.py 34 class BST : #... def _bstremove( self, subtree, target ):... else : if subtree.left is None and subtree.right is None : return None # leaf node elif subtree.left is None or subtree.right is None : # has one child only if subtree.left is not None : return subtree.left else : return subtree.right else : # has both children successor = self._bstminimum( subtree.right ) subtree.key = successor.key subtree.value = successor.value subtree.right = self._bstremove( subtree.right, successor.key ) return subtree

BST Efficiency Operation _bstsearch( root, k ) _bstminimum( root ) _bstinsert( root, k ) _bstremove( root, k ) traversal Worst Case O(n) O(n) O(n) O(n) O(n) 35

Exercises 1. The remove() method uses a bst_find_min(subtree) method to search and return the node with minimum key in a tree rooted at subtree. Implement this method. 2. Write a method bst_sort_reverse() to sort a binary search tree rooted at subtree in reverse order. 3. Test your methods by running the program testbst.py on the course website.