Yan Shi CS/SE 2630 Lecture Notes

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
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.
Chapter 8 Binary Search Tree 1 Fall Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
EC-211 DATA STRUCTURES LECTURE Tree Data Structure Introduction –The Data Organizations Presented Earlier are Linear in That Items are One After.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
1 Chapter 8 Binary Search Trees. 2 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Jake’s Pizza Shop.
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.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Chapter 8 Binary Search Trees. Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Jake’s Pizza Shop.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
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 Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
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.
Chapter 8 Binary Search Trees. 2 Goals Define and use the following terminology: binary tree root descendant subtree binary search tree parent level ancestor.
Chapter 8 Binary Search Trees 1. Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Jake’s Pizza Shop 2.
1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.
Computer Science and Software Engineering University of Wisconsin - Platteville 10. Binary Search Tree Yan Shi CS/SE 2630 Lecture Notes Partially adopted.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Binary Search Trees (BST)
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Binary Search Trees CS Goals Define and use the following terminology: binary tree root descendant subtree binary search tree parent level ancestor.
CSE 373 Data Structures Lecture 7
Chapter 16: Linked Lists.
AA Trees.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Binary Trees and Binary Search Trees
BST Trees
Binary Search Trees Chapter 7 Objectives
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Trees Lecture 12 CS2110 – Fall 2017.
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Chapter 16 Tree Implementations
ITEC 2620M Introduction to Data Structures
Chapter 8 Binary Search Trees.
Revised based on textbook author’s notes.
Data Structures and Database Applications Binary Trees in C#
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.
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
Find in a linked list? first last 7  4  3  8 NULL
Search Sorted Array: Binary Search Linked List: Linear Search
B- Trees D. Frey with apologies to Tom Anastasio
A Robust Data Structure
CMSC 202 Trees.
Binary Search Trees.
B- Trees D. Frey with apologies to Tom Anastasio
Binary Trees, Binary Search Trees
Chapter 8 Binary Search Tree
CSC 143 Binary Search Trees.
Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Heaps and priority queues
Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Yan Shi CS/SE 2630 Lecture Notes 10. Binary Search Tree Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus Data Structure textbook slides

Review: Binary Search in a Sorted Array Based List Examines the element in the middle of the array. Is it the sought item? If so, stop searching. Is the middle element too small? Then start looking in second half of array. Is the middle element too large? Then begin looking in first half of the array. Repeat the process in the half of the list that should be examined next. Stop when item is found, or when there is nowhere else to look and item has not been found.

Example item = 45 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 15 26 38 57 62 78 84 91 108 119

Example item = 45 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 15 26 38 57 62 78 84 91 108 119 first midPoint last LESS last = midPoint - 1 GREATER first = midPoint + 1 15 26 38 57 62 78 84 91 108 119 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first midPoint last

item = 45 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 15 26 38 57 62 78 84 91 108 119 first, last midPoint GREATER first = midPoint + 1 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 15 26 38 57 62 78 84 91 108 119 first, midPoint, last LESS last = midPoint - 1

item = 45 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 15 26 38 57 62 78 84 91 108 119 last first first > last found = false

Motivations for Binary Search Tree array-based list: fast search: binary search O(log2N) slow insertion/deletion linear linked list: fast insertion, deletion slow search Can we have fast search, addition and deletion? Binary search tree

What is a tree? Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len

A Tree Has a Root Node Owner Jake ROOT NODE Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len

Leaf Nodes have No Children Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len

A Tree Has Levels Owner LEVEL 0 Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len LEVEL 0

Level One Owner Jake Manager Chef Brad Carol LEVEL 1 Waitress Waiter Cook Helper Joyce Chris Max Len

Level Two Owner Jake Manager Chef Brad Carol LEVEL 2 Waitress Waiter Cook Helper Joyce Chris Max Len LEVEL 2

A Subtree Owner Jake Manager Chef Brad Carol LEFT SUBTREE OF ROOT NODE Waitress Waiter Cook Helper Joyce Chris Max Len LEFT SUBTREE OF ROOT NODE

Binary Tree A binary tree is a structure in which: Each node can have at most two children, and in which a unique path exists from the root to every other node. The two children of a node are called the left child and the right child, if they exist.

Implementing a Binary Tree with Pointers and Dynamic Data V Q L T E A K S How many leaf nodes? descendant of Q? ancestors of Q?

Binary Search Tree (BST) A special kind of binary tree in which: Each node contains a distinct data value The key values in the tree can be compared using “greater than” and “less than”, and The key value of each node in the tree is less than every key value in its right sub-tree, and greater than every key value in its left sub-tree. Similar as sorted list, the order is maintained when inserting new items into the tree

Example Insert ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ to an empty BST. Insert ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ to an empty BST. ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ The shape of a BST depends on its key values and their order of insertion ‘A’ ‘E’ ‘F’ ‘J’ ‘T’

Exercise ‘J’ ‘E’ ‘T’ ‘A’ ‘H’ ‘M’ ‘K’ ‘P’ Insert nodes containing these values in this order: ‘D’ ‘B’ ‘L’ ‘Q’ ‘S’ ‘V’ ‘Z’

How many nodes do we have? ‘J’ ‘E’ ‘T’ ‘A’ ‘H’ ‘M’ ‘V’ ‘D’ ‘K’ ‘Z’ ‘P’ ‘B’ ‘L’ ‘Q’ ‘S’

Count Nodes in a BST CountNodes Algorithm v1: if left(tree) is NULL AND right(tree) is NULL return 1 else return CountNodes(Left(tree)) + CountNodes(Right(tree)) + 1 What if Left(tree) is NULL?

Count Nodes in a BST CountNodes Algorithm v2: if (Left(tree) is NULL) AND (Right(tree) is NULL) return 1 else if Left(tree) is NULL return CountNodes(Right(tree)) + 1 else if Right(tree) is NULL return CountNodes(Left(tree)) + 1 else return CountNodes(Left(tree)) + CountNodes(Right(tree)) + 1 What if the initial tree is NULL?

Count Nodes in a BST CountNodes Algorithm v3: if tree is NULL return 0 else if (Left(tree) is NULL) AND (Right(tree) is NULL) return 1 else if Left(tree) is NULL return CountNodes(Right(tree)) + 1 else if Right(tree) is NULL return CountNodes(Left(tree)) + 1 else return CountNodes(Left(tree)) + CountNodes(Right(tree)) + 1 Can we simplify it somehow?

Count Nodes in a BST CountNodes Algorithm Final Version: if tree is NULL return 0 else return CountNodes(Left(tree)) + CountNodes(Right(tree)) + 1 Does it cover all possible situations?

Is ‘F’ in the binary search tree? ‘J’ ‘E’ ‘T’ ‘A’ ‘H’ ‘M’ ‘V’ ‘D’ ‘K’ ‘Z’ ‘P’ ‘B’ ‘L’ ‘Q’ ‘S’

Search an item in a BST Search Algorithm Similar algorithm for retrieving an item given its key Given a BST tree and an item to find: if tree is NULL found = false else if ( item < current node’s info ) Search( left(tree), item ) else if ( item > current node’s info ) Search( right(tree), item ) else found = true

Insert an item to a BST Use recursion Base case: General case: tree is NULL (empty tree or leaf node’s left/right) CANNOT add  item already in the BST! General case: if item < tree->info, Insert item to left(tree) else if item > tree->info, Insert item to right(tree)

The “tree” Parameter bool BST::Insert( TNode * tree, Type item ) { if ( tree == NULL ) tree = new TNode( item ); return true; } else if ( item < tree->info ) return Insert( tree->left, item ); else if ( item > tree->info ) return Insert( tree->right, item ); else return false; } //Is it correct?

The “tree” parameter Treat “tree” as a pointer pointing within the tree! What happens when creating a new node? tree become a real address instead of NULL! Make “tree” a reference parameter!

The “tree” Parameter bool BST::Insert( TNode * & tree, Type item ) { if ( tree == NULL ) tree = new TNode( item ); return true; } else if ( item < tree->info ) return Insert( tree->left, item ); else if ( item > tree->info ) return Insert( tree->right, item ); else return false; } //tree is a reference parameter!

Delete an item in the BST Use recursion Base case: If item's key matches key in Info(tree), delete node pointed to by tree. If tree is empty, cannot delete. General case: if item < tree->info, Delete item in left(tree) else if item > tree->info, Delete item in right(tree) How to delete a node?

Code for Delete bool BST::Delete( TNode * & tree, Type item ) { if ( tree == NULL ) return false; if ( item < tree->info ) return Delete( tree->left, item ); else if ( item > tree->info ) return Delete( tree->right, item ); else DeleteNode( tree ); return true; } we are changing the structure of the tree! // How to do this?

Deleting a Leaf Node

Deleting a Node with One Child

Deleting a Node with Two Children

Delete a Node in BST Algorithm: Indirect Recursion! if (Left(tree) is NULL) AND (Right(tree) is NULL) Set tree to NULL else if Left(tree) is NULL Set tree to Right(tree) else if Right(tree) is NULL Set tree to Left(tree) else Find predecessor Set Info(tree) to Info(predecessor) Delete predecessor Indirect Recursion!

How to find a predecessor? Algorithm: if ( tree->left != NULL ) { tree = tree->left; while (tree->right != NULL) tree = tree->right; predecessor = tree->info; }

Tree Traversal In-order pre-order post-order from smallest to largest current info first then left then right post-order left first then current info

In-Order Print Algorithm: How about pre-order and post-order print? if tree is not NULL InOrderPrint( left(tree) ) print tree->info InOrderPrint( right(tree) )

Big-3 for BST Destructor: Copy constructor and assignment operator: need to delete all node traverse the tree in which order? Copy constructor and assignment operator: How to copy a tree? How to implement operator=? post-order! pre-order!

Iterative Solutions for BST Binary Search: Has( item ) Insert: Add( item ) Delete: Remove( item )

Iterative Solution for Search Algorithm for search: Has( item ) Set nodePtr to tree   while more elements to search if item < Info(nodePtr) Set nodePtr to Left(nodePtr) else if item > Info(nodePtr) Set nodePtr to Right(nodePtr) else return true return false

Iterative Solution for Insert create a node to contain the new item find the insertion place similar as the previous search need to keep track of parent as well insert new node insert as either the left or right child of parent

Example: Insert Insert 13 to the tree

Insert 13 to the tree

Insert 13 to the tree

Insert 13 to the tree

Insert 13 to the tree

Iterative Solution for Delete TNode * node, * parent; bool found = false; FindNode( item, found, node, parent ); if ( found == false ) return false; if ( node == root ) DeleteNode( root ); else if ( parent->left == node ) DeleteNode( parent->left ); else DeleteNode( parent->right ); return true; //why parent->left? is it really iterative if we use the previous DeleteNode function?

nodePtr and parentPtr Are External to the Tree

parentPtr is External to the Tree, but parentPtr-> left is an Actual Pointer in the Tree

Full and Complete BST Full Binary Tree: A binary tree in which all of the leaves are on the same level and every non-leaf node has two children Complete Binary Tree: A binary tree that is either full or full through the next-to-last level, with the leaves on the last level as far to the left as possible

Examples

Array Representation of BST For any tree node nodes[index] left child: nodes[index*2 + 1] right child: nodes[index*2 + 2] parent: nodes[(index – 1)/2]

Array Representation of Non-Complete BST Use dummy values to fill the space