ICOM 4035 – Data Structures Lecture 14 – Binary Search Tree ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico,

Slides:



Advertisements
Similar presentations
ICOM 4035 – Data Structures Lecture 15 – Graph ADT
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
ICOM 4035 – Data Structures Lecture 12 – Hashtables & Map ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico,
ICOM 4035 – Data Structures Lecture 8 – Doubly Linked Lists Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
A balanced life is a prefect life.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
BST Data Structure A BST node contains: A BST contains
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Marc Smith and Jim Ten Eyck
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.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
ICOM 4035 – Data Structures Lecture 2 – ADT and Interfaces Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
ICOM 4035 – Data Structures Lecture 9 – Stack ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
ICOM 4035 – Data Structures Lecture 5 – List ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
ICOM 4035 – Data Structures Lecture 13 – Trees ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
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.
ICOM 4035 – Data Structures Lecture 11 – Map ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
ICOM 4035 – Data Structures Lecture 1 - Introduction Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
ICOM 4035 – Data Structures Lecture 3 – Bag ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
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)
1 Searching the dictionary ADT binary search binary search trees.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
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.
ICOM 4035 – Data Structures Lecture 7 – Linked Lists Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
CSC 213 Lecture 8: (2,4) Trees. Review of Last Lecture Binary Search Tree – plain and tall No balancing, no splaying, no speed AVL Tree – liberté, égalité,
ICOM 4035 – Data Structures Lecture 4 – Set ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
Binary Search Trees (BST)
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
CS261 Data Structures Binary Search Trees Concepts.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
Binary Search Trees Chapter 7 Objectives
Trees Chapter 15.
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
BST Trees
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.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
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.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Find in a linked list? first last 7  4  3  8 NULL
CSC 143 Binary Search Trees.
Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

ICOM 4035 – Data Structures Lecture 14 – Binary Search Tree ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel Rodriguez – All rights reserved

Lecture Organization Part I – Introduction to the Binary Search Tree Part II – Implementation of the Binary Search Tree Part III – Iterating over Binary Search Tree M. Rodriguez-MartinezICOM

Objectives Introduce the concept of binary search tree – Unbalanced, ordered binary tree Discuss design and implementation of binary search trees Provide motivating examples ICOM 4035M. Rodriguez-Martinez 3

Companion videos Lecture14 videos – Contains the coding process associated with this lecture – Shows how to build the interfaces, concrete classes, and factory classes mentioned here M. Rodriguez-MartinezICOM

Part I Introduction to the concept of a binary search tree M. Rodriguez-MartinezICOM

Binary Search Tree A Binary Search Tree (BST) T is an unbalanced binary tree with the following property: – For any node n in T every node v < n that is a descendant of n is located on the left sub-tree rooted at n. every node v ≥ n that is a descendant of n is located on the right sub-tree rooted at n In other words, for any node n – All the descendants on the left are less than n – All the descendants on the right are greater or equal than n The comparison is done base on keys of values at nodes M. Rodriguez-MartinezICOM

Examples of BST M. Rodriguez-MartinezICOM Jil ApuNed Amy Bob Ned Moe Jil Ron Cal Xi T1 T2

Examples of BST (2) M. Rodriguez-MartinezICOM Xi Ken Ben Joe Cal Ned Ron T3 T4 Zoe

Operations on the BST size() – get number of elements makeEmpty() – remove all elements isEmpty() – determine if BST is empty add() – adds new element with given key – Keeping BST property remove() – removes element with a given key – Keeping BST property get() – returns copy of element with given key – Keeping BST property Iterator() – returns an in-order iterator to the tree M. Rodriguez-MartinezICOM

Balanced BST M. Rodriguez-MartinezICOM Level 0, 1 node Level 1, 2 node Level 2, 4 nodes Level 3, 8 nodes This is a full tree, where every internal node is full, meaning each one has the maximum number of children

Balanced BST: Intuition on height Max nodes by levels – Level 0, 1 node – Level 1, 2 nodes – Level 2, 4 nodes – Level 3, 8 nodes – … – Level k, 2 k nodes Notice Level and height are the same thing! If a tree is balanced and it has N nodes, how many levels L it has? Estimate for full tree: Since L and height h are the same it follows that – h = log 2 (N) M. Rodriguez-MartinezICOM

Some notes BST can be used to speed up search for ordered data Operations are O(h), where h is height of tree If height h is much smaller than n = number of elements, then BST is very efficient! On average, h = log 2 (N), so bound is O(log 2 (N)) on average BST can be used to implement – Sets – Maps But, BST is not balanced – Tree can degenerate into long chain Worst case scenario h = n, where n is size of tree, giving O(n) – Balanced implementation: AVL Tree M. Rodriguez-MartinezICOM

Applications for BST Keeping track of student records M. Rodriguez-MartinezICOM Moe 24 NY Jil 19 SF Ron 19 LA Cal 32 LA Xi 29 SJ

Applications for BST(2) Keeping track of movie information M. Rodriguez-MartinezICOM Jaws R 1975 ET 1982 PG Shrek 2001 PG Twilight 2008 PG-13

Part II Implementation of the Binary Search Tree M. Rodriguez-MartinezICOM

Nodes in a BST Node in BST has 4 fields – Key of the value stored at node – Value stored at node – Reference to left child – Reference to right child M. Rodriguez-MartinezICOM Key Value leftChild rightChil d

Structure of BST M. Rodriguez-MartinezICOM Jil {Jil, NY, 19} Al {Al, NY, 21} Ron {Ron, SF, 40} Sam {Sam, LA, 23} currentSize = 4

BST Interface and Container Class M. Rodriguez-MartinezICOM > BST BSTImp

BST Implementation BSTImp class has three fields – root – the root node of the tree – currentSize – the current size of the tree – keyComparator – object to compare keys BSTImp class used two generic parameters – K – keys of nodes – V – value in nodes Empty tree has root set to null! M. Rodriguez-MartinezICOM

Recursive implementation Most methods will be implemented with recursion – Will use an auxiliary recursive method to do the work Terminating condition will be reaching a null reference for a node that is considered the root of a sub-tree being explored M. Rodriguez-MartinezICOM

Get element with key K Start at the root – Call getAux(key, root) getAux(K key, Node N) if (N == null){ return null; } else if (key == N.key){ return N.value; } else if ( key < N.key){ return getAux(key, N.leftChild); } else{ Return getAux(key, N.rightChild); } M. Rodriguez-MartinezICOM Moe 24 NY Jil 19 SF Ron 19 LA Cal 32 LA Xi 29 SJ T.get(Xi) Found! currentSize =5

Get element with key K(2) Start at the root – Call getAux(key, root) getAux(K key, Node N) if (N == null){ return null; } else if (key == N.key){ return N.value; } else if ( key < N.key){ return getAux(key, N.leftChild); } else{ Return getAux(key, N.rightChild); } M. Rodriguez-MartinezICOM Moe 24 NY Jil 19 SF Ron 19 LA Cal 32 LA Xi 29 SJ T.get(Ken) Not found! x currentSize =5

Add element with key K If root == null Root = new node with value else addAux(key, value, root) addAux(K key, V value, Node N) if (key <=N.key){ if (N.leftChild == null) N.leftChild = new node with value else addAux(key, value, N.leftChild); } else { if (N.rightChild == null) N.rightChild = new node with value else addAux(key, value, N.rightChild); } M. Rodriguez-MartinezICOM Moe 24 NY Jil 19 SF Ron 19 LA Cal 32 LA Xi 29 SJ T.add(Ned, {Ned, 19, LA}) Ned 19 LA currentSize =5 6

Remove element with key Idea: – First find the element – Next determine cases 1: Leaf node – Erase it 2: Internal node with no right child – Replace with left child 3: Internal node with right child – Replace with in-order successor on right subtree (leftmost node) » Erase that successor M. Rodriguez-MartinezICOM Moe 24 NY Jil 19 SF Ron 19 LA Cal 32 LA Xi 29 SJ T.remove(Cal) Found! currentSize =5 currentSize =4

Remove element with key Idea: – First find the element – Next determine cases 1: Leaf node – Erase it 2: Internal node with no right child – Replace with left child 3: Internal node with right child – Replace with in-order successor on right subtree (leftmost node) » Erase that successor M. Rodriguez-MartinezICOM Moe 24 NY Jil 19 SF Ron 19 LA Cal 32 LA Xi 29 SJ T.remove(Cal) Found! currentSize =5 currentSize =4

Remove element with key(2) Idea: – First find the element – Next determine cases 1: Leaf node – Erase it 2: Internal node with no right child – Replace with left child 3: Internal node with right child – Replace with in-order successor on right subtree (leftmost node) » Erase that successor M. Rodriguez-MartinezICOM Moe 24 NY Jil 19 SF Ron 19 LA Cal 32 LA Xi 29 SJ T.remove(Jil) Found! currentSize =5 currentSize =4

Remove element with key(3) Idea: – First find the element – Next determine cases 1: Leaf node – Erase it 2: Internal node with no right child – Replace with left child 3: Internal node with right child – Replace with in-order successor on right subtree (leftmost node) » Erase that successor M. Rodriguez-MartinezICOM Moe 24 NY Jil 19 SF Ron 19 LA Cal 32 LA Xi 29 SJ T.remove(Cal) Found! currentSize =5 currentSize =4 Ned 19 LA Ned 19 LA

Cost of Operation get() – O(h), h = height of tree add() – O(h), h = height of tree remove() – O(h), h = height of tree Reasoning: You might need to reach leaf level – Traversal down to leaf if O(h) Things are good if h is much smaller than N = size of tree – Best scenario h = log 2 N – Worst scenario h = N M. Rodriguez-MartinezICOM

Easy operations size() – return currentSize – Cost: O(1) isEmpty() – return size() == 0 – Cost: O(1) constains() – return get() != null – Cost: O(h), h = height of Tree makeEmpty() – while not isEmpty() remove(root.key) – Cost: O(n), n = size of tree M. Rodriguez-MartinezICOM

Part III Iterating over a binary search tree M. Rodriguez-MartinezICOM

Iteration Scheme Traversal of tree is done in a recursive manner – Bad idea to suspend recursion halfway thru What is the next element? – Should be given in the increasing order How to keep track of them? Solution: – Iterate over tree using in-order traversal – Place value of visited nodes in a queue – Iterator fetches values from queue M. Rodriguez-MartinezICOM

Organization of BST Iterator M. Rodriguez-MartinezICOM Moe 24 NY Jil 19 SF Ron 19 LA Cal 32 LA Xi 29 SJ currentSize =5 Cal 32 LA Jil 19 SF Moe 24 NY Ron 19 LA Xi 29 SJ BST BST Iterator

BST Iterator Class The BST iterator simply has a queue as private field The queue is made of values V in the tree Elements are inserted as you visit tree in-order M. Rodriguez-MartinezICOM Cal 32 LA Jil 19 SF Moe 24 NY Ron 19 LA Xi 29 SJ BST Iterator

Filling up the Queue Constructor of Iterator is calls recursive method traverseAux Algorithm traverseAux(Node n, Queue Q): – Start at the root and visit tree in-order – For each node n visited If n is null – return Else – traveseAux(n.leftChild, Q); – Q.enqueue(n.value); – traverseAux(n.rightChild, Q); M. Rodriguez-MartinezICOM

Summary Introduced the concept of Binary Search Tree – Unbalanced ordered tree Discussed application of BST Presented implementation of BST and iterator class M. Rodriguez-MartinezICOM

Questions? – ICOM 4035M. Rodriguez-Martinez 36