Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree.

Slides:



Advertisements
Similar presentations
1 Functional Programming Lecture 8 - Binary Search Trees.
Advertisements

CS16: Introduction to Data Structures & Algorithms
Linked List: Traversal Insertion Deletion. Linked List Traversal LB.
Chapter 12 Binary Search Trees
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.
Trees Types and Operations
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
Introduction to Trees. Tree example Consider this program structure diagram as itself a data structure. main readinprintprocess sortlookup.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules.
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.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
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:
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
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 ),
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
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.
Starting at Binary Trees
Binary Search Tree Qamar Abbas.
Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Data Structures Types of Data Structure Data Structure Operations Examples Choosing Data Structures Data Structures in Alice.
Review Records Dynamic Memory and Pointers Introduction to Linked Lists.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
Review Problems. What is the Big O? i
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Reading data into sorted list Want to suck text file in and produce sorted list of the contents: Option 1 : read directly into array based list, sort afterwards.
Search: Binary Search Trees Dr. Yingwu Zhu. Review: Linear Search Collection of data items to be searched is organized in a list x 1, x 2, … x n – Assume.
Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary 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
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
Chapter 12 – Data Structures
Trees ---- Soujanya.
Binary Search Tree (BST)
Lecture 14 Traversals of Linked Lists Preorder BST Traversal Postorder BST Traversal Miscellaneous BST Traversals Depth First vs Breadth First Array Traversals.
Tree.
Lecture 7 Algorithm Analysis
Chapter 20: Binary 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.
Chapter 21: Binary Trees.
Lecture 7 Algorithm Analysis
Chapter 12: Binary Search Trees
CS6045: Advanced Algorithms
Lecture 7 Algorithm Analysis
Computer Science 2: Trees
CSE 1002 Fundamentals of Software Development 2 More Linked Structures
Chapter 20: Binary Trees.
Binary Search Trees.
Yan Shi CS/SE 2630 Lecture Notes
Heaps and priority queues
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree

Traversing a Binary Tree Inorder Traversal

The Scenario Imagine we have a binary tree We want to traverse the tree –Its not linear –We need a way to visit all nodes Three things must happen: –Deal with the entire left sub-tree –Deal with the current node –Deal with the entire right sub-tree

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do left

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do left At 9 – do left

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do left At 9 – do left At 2 – do left

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do left At 9 – do left At 2 – do left At NIL

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do left At 9 – do left At 2 – do current

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do left At 9 – do left At 2 – do right

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do left At 9 – do left At 2 – do right At NIL

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do left At 9 – do left At 2 - done

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do left At 9 – do current

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do left At 9 – do right

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do left At 9 – do right At NIL

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do left At 9 – done

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do current

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do right

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do right At 42 – do left

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do right At 42 – do left At NIL

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do right At 42 – do current

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do right At 42 – do right

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do right At 42 – do right At NIL

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – do right At 42 – done

Use the Activation Stack With a recursive module, we can make use of the activation stack to visit the sub-trees and remember where we left off At 13 – done

Outline of In-Order Traversal Three principle steps: –Traverse Left –Do work (Current) –Traverse Right Work can be anything Separate work from traversal

Traverse the tree In order: –Visit the trees left sub-tree –Visit the current and do work –Visit right sub-tree

In-Order Traversal Procedure procedure In_Order(cur iot in Ptr toa Tree_Node) // Purpose: perform in-order traversal, call // Do_Something for each node // Preconditions: cur points to a binary tree // Postcondition: Do_Something on each tree // node in in-order order if( cur <> NIL ) then In_Order( cur^.left_child ) Do_Something( cur^.data ) In_Order( cur^.right_child ) endif endprocedure // In_Order

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root L P R L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root L P R L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root L P R L L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root L P R L L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root L P R L L L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root L P R L L L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root L P R L L L L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root L P R L L L L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: 1 L P R L L L L P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: 1 L P R L L L L P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: 1 L P R L L L L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: 1 L P R L L L L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: 1 L P R L L L L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: 1 3 L P R L L L L P R P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: 1 3 L P R L L L L P R P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: 1 3 L P R L L L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: 1 3 L P R L L L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: 1 3 L P R L L L P R L P R L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: 1 3 L P R L L L P R L L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L L L P R L L P R P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L L L P R L L P R P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L L L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L L L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L L L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L L L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L L P L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L L P L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L L P R L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L L P R L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L L P R L P R L P R L P R L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L L P R L P R L P R L P R L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L L P R L P R L P R L P R L P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L L P R L P R L P R L P R L P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L L P R L P R L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L L P R L P R L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L L P R L P R L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L L P R L P R L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P L P R L P R L P R L P R L P R

Continue? Yes! Enough Already!

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P L P R L P R L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L L P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L L P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L L P R L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L L P R L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L L P R L P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L L P R L P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L L P R L P R P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L L P R L P R P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) 22 root Output: L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

Algorithm Example... InOrderPrint(root) root Output: L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R L P R

Summary An In-Order traversal visits every node –Recurse left first –Do something with current –Recurse right last The left, current, right logic is repeated recursively at every node. For a BST, an in-order traversal accesses the elements in ascending order.

Questions?

Binary Search Tree Insertion

Tree Node Defined In general: Node definesa record data isoftype left, right isoftype ptr toa Node endrecord In this example: Node definesa record data isoftype num left, right isoftype ptr toa Node endrecord

Scenario We have a Binary Search Tree –It can be empty –Or have some elements in it already We want to add an element to it –Inserting/adding involves 2 steps: Find the correct location Do the steps to add a new node Must maintain search structure

Finding the Correct Location Start with this tree

Finding the Correct Location Where would 4 be added?

Finding the Correct Location To the top doesnt work 4

Finding the Correct Location In the middle doesnt work 4

Finding the Correct Location At the bottom DOES work! 4

Finding the Correct Location Must maintain search structure –Everything to left is less than current –Everything to right is greater than current Adding at the bottom guarantees we keep search structure. Well recurse to get to the bottom (i.e. when current = nil)

Finding the Correct Location if (current = nil) DO ADD NODE WORK HERE elseif (current^.data > value_to_add) then // recurse left Insert(current^.left, value_to_add) else // recurse right Insert(current^.right, value_to_add) endif

Adding the Node Current is an in/out pointer –We need information IN to evaluate current –We need to send information OUT because were changing the tree (adding a node) Once weve found the correct location: –Create a new node –Fill in the data field (with the new value to add) –Make the left and right pointers point to nil (to cleanly terminate the tree)

Adding the Node current <- new(Node) current^.data <- value_to_add current^.left <- nil current^.right <- nil

The Entire Module procedure Insert(cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert

Tracing Example The following example shows a trace of the BST insert. Begin with an empty BST (a pointer) Add elements 42, 23, 35, 47 in the correct positions.

Head iot Ptr toa Node head <- NIL Insert(head, 42)

Head iot Ptr toa Node head <- NIL Insert(head, 42) head

Head iot Ptr toa Node head <- NIL Insert(head, 42) head

Head iot Ptr toa Node head <- NIL Insert(head, 42) head

Head iot Ptr toa Node head <- NIL Insert(head, 42) procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert head

Head iot Ptr toa Node head <- NIL Insert(head, 42) procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert head

Head iot Ptr toa Node head <- NIL Insert(head, 42) procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert head 42

Head iot Ptr toa Node head <- NIL Insert(head, 42) procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert head 42

Head iot Ptr toa Node head <- NIL Insert(head, 42) procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert head 42

Head iot Ptr toa Node head <- NIL Insert(head, 42) procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert head 42

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47).

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 23

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 23

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 23

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 23

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 23 23

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 35

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 35

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 35

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 35

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 35

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert data_in = 35 35

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47)

Continue? yes...Ive had enough!

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 data_in = procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 data_in = procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 data_in = procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 data_in = procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 data_in = procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 data_in = procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 47

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 data_in = procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 47

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 data_in = procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 47

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 data_in = procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 47

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47). 23 data_in = procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endif endprocedure // Insert 47

head 42. Insert(head, 23) Insert(head, 35) Insert(head, 47)

Summary Preserve search structure! Inserting involves 2 steps: –Find the correct location For a BST insert, always insert at the bottom of the tree –Do commands to add node Create node Add data Make left and right pointers point to nil

Questions?

Deleting from a Binary Search Tree (BST)

The Scenario We have a Binary Search Tree and want to remove some element based upon a match. Must preserve search property Must not lose any elements (i.e. only remove the one element)

BST Deletion Search for desired item. If not found, then return NIL or print error. If found, perform steps necessary to accomplish removal from the tree.

Four Cases for Deletion Delete a leaf node Delete a node with only one child (left) Delete a node with only one child (right) Delete a node with two children Cases 2 and 3 are comparable and only need slight changes in the conditional statement used

Delete a Leaf Node Simply use an in/out pointer and assign it to nil. This will remove the node from the tree. cur <- nil

Delete a Leaf Node Simply use an in/out pointer and assign it to nil. This will remove the node from the tree. cur <- nil Lets delete

Delete a Leaf Node Simply use an in/out pointer and assign it to nil. This will remove the node from the tree. cur <- nil Move the pointer; now nothing points to the node

Delete a Leaf Node Simply use an in/out pointer and assign it to nil. This will remove the node from the tree. cur <- nil The resulting tree

Delete a Node with One Child Use an in/out pointer. Determine if it has a left or a right child. Point the current pointer to the appropriate child: cur <- cur^.left_child or cur <- cur^.right_child

Delete a Node with One Child Use an in/out pointer. Determine if it has a left or a right child. Point the current pointer to the appropriate child: cur <- cur^.left_child or cur <- cur^.right_child Lets delete

Delete a Node with One Child Use an in/out pointer. Determine if it has a left or a right child. Point the current pointer to the appropriate child: cur <- cur^.right_child Move the pointer; now nothing points to the node

Delete a Node with One Child Use an in/out pointer. Determine if it has a left or a right child. Point the current pointer to the appropriate child: cur <- cur^.right_child The resulting tree

Delete a Node with One Child Use an in/out pointer. Determine if it has a left or a right child. Point the current pointer to the appropriate child: cur <- cur^.left_child or cur <- cur^.right_child Lets delete

Delete a Node with One Child Use an in/out pointer. Determine if it has a left or a right child. Point the current pointer to the appropriate child: cur <- cur^.left_child Move the pointer; now nothing points to the node

Delete a Node with One Child Use an in/out pointer. Determine if it has a left or a right child. Point the current pointer to the appropriate child: cur <- cur^.left_child The resulting tree

Delete a Node with Two Children Copy a replacement value from a descendant node. - Largest from left - Smallest from right Then delete that descendant node to remove the duplicate value. - We know this will be an easier case

Delete a Node with Two Children Lets delete 50.

Delete a Node with Two Children Look to the left sub-tree.

Delete a Node with Two Children Find and copy the largest value (this will erase the old value but creates a duplicate).

Delete a Node with Two Children The resulting tree so far.

Delete a Node with Two Children Now delete the duplicate from the left sub-tree.

Delete a Node with Two Children The final resulting tree – still has search structure.

Delete a Node with Two Children Lets delete 94.

Delete a Node with Two Children Look to the right sub-tree.

Delete a Node with Two Children Find and copy the smallest value (this will erase the old value but creates a duplicate). 108

Delete a Node with Two Children The resulting tree so far.

108 Delete a Node with Two Children Now delete the duplicate from the left sub-tree.

Delete a Node with Two Children The final resulting tree – still has search structure. 108

Summary Deleting a node from a binary search tree involves two steps: –Search for the element –Then perform the deletion We must preserve the search structure and only delete the element which matches. Four cases: –Deleting a leaf node –Deleting a node with only the left child –Deleting a node with only the right child –Deleting a node with both children

Questions?