Download Binary Tree Traversal and Removal Algorithms and more Slides Data Structures and Algorithms in PDF only on Docsity!
CPSC 223
Algorithms & Data Abstract Structures
Lecture 14:
Quicksort exercise
Binary Search Tree Traversal and Deletion
Today …
• Quiz 6
• Sorting Exercise (Quicksort)
• Tree traversals
• Binary Search Tree Deletion
– Ch 10: pp. 546-
CPSC 223 -‐-‐ Fall 2010 2
Quicksort (based on textbook)
void Quicksort(Entry theArray[], int first, int last)
if(first < last) {
int pivotIndex = Partition(theArray, first, last);
// quicksort first half
Quicksort(theArray, first, pivotIndex – 1);
// quicksort second half
Quicksort(theArray, pivotIndex + 1, last);
CPSC 223 -‐-‐ Fall 2010 3
Quicksort (based on textbook)
int Partition(Entry theArray[], int first, int last)
Entry pivot = theArray[first]; // pivot value
int lastP1 = first; // last index of first partition
for(int i = first + 1; i <= last; i++) {
if(theArray[i] < pivot) {
lastP1++;
Swap(theArray[i], theArray[lastP1]);
Swap(theArray[first], theArray[lastP1]);
return lastP1;
CPSC 223 -‐-‐ Fall 2010 4
Traversing binary trees
CPSC 223 -‐-‐ Fall 2010 7
Tree traversals are usually implemented using recursion
- Don’t need to know number of nodes, height, etc.
- It’s possible to traverse without using recursion (but tricky)
But, when traveling a specific path …
- Can use recursion or iteration (loops)
- E.g., lookup and retrieve (typically simple loops)
D
B G
A C E H
F
Traversing binary trees
CPSC 223 -‐-‐ Fall 2010 8
Given a tree T , we have the following two cases:
- T is empty (we have nothing to traverse)
- T is not empty, and so we:
- Visit the root of T
- Visit the left subtree of T
- Visit the right subtree of T
D
B G
A C E H
F
Le< subtree of T Right subtree of T Root of T
The basic structure of each type
of tree traversal we’ll look at
(preorder, inorder, postorder)
Depth-First, Left-to-Right Tree Traversal
CPSC 223 -‐-‐ Fall 2010 9
D
B G
A C E H
NULL NULL NULL NULL NULL F
NULL NULL
NULL NULL
Lets do a ( preorder ) traversal of this tree …
– This means visit the root node first
– Once visited, traverse le< and then traverse right
Depth-First, Left-to-Right Tree Traversal
CPSC 223 -‐-‐ Fall 2010 10
D
B G
A C E H
NULL NULL NULL NULL NULL F
NULL NULL
NULL NULL
1. Visit the root D of the (sub) tree
current subtree
Depth-First, Left-to-Right Tree Traversal
4. Traverse to the le/ subtree of B
CPSC 223 -‐-‐ Fall 2010 13
D
B G
A C E H
NULL NULL NULL NULL NULL F
NULL NULL
NULL NULL
previous subtree previous subtree current subtree
Depth-First, Left-to-Right Tree Traversal
5. Visit the root A of the le< subtree of B
CPSC 223 -‐-‐ Fall 2010 14
D
B G
A C E H
NULL NULL NULL NULL NULL F
NULL NULL
NULL NULL
previous subtree previous subtree current subtree
Depth-First, Left-to-Right Tree Traversal
5. Traverse to the le/ subtree of A
– The subtree is empty , so nothing to visit
– We’re done going le< …
CPSC 223 -‐-‐ Fall 2010 15
D
B G
A C E H
NULL NULL NULL NULL NULL F
NULL NULL
NULL NULL
previous subtree previous subtree previous subtree current subtree
Note this is
what is meant
by “ depth first”
Depth-First, Left-to-Right Tree Traversal
6. Traverse to the right subtree of A
– The subtree is empty , so nothing to visit
– We’re done going right …
CPSC 223 -‐-‐ Fall 2010 16
D
B G
A C E H
NULL NULL NULL NULL NULL F
NULL NULL
NULL NULL
previous subtree previous subtree previous subtree current subtree
Depth-First, Left-to-Right Tree Traversal
9. Visit the root C of the right subtree of B
CPSC 223 -‐-‐ Fall 2010 19
D
B G
A C E H
NULL NULL NULL NULL NULL F
NULL NULL
NULL NULL
previous subtree previous subtree (^) current subtree
Depth-First, Left-to-Right Tree Traversal
10. Traverse to the le/ subtree of C
– The subtree is empty , so nothing to visit
– We’re done going right …
CPSC 223 -‐-‐ Fall 2010 20
D
B G
A C E H
NULL NULL NULL NULL NULL F
NULL NULL
NULL NULL
previous subtree current subtree (^) previous subtree current subtree
Depth-First, Left-to-Right Tree Traversal
11. Traverse to the right subtree of C
– The subtree is empty , so nothing to visit
– We’re done going left and right …
CPSC 223 -‐-‐ Fall 2010 21
D
B G
A C E H
NULL NULL NULL NULL NULL F
NULL NULL
NULL NULL
previous subtree current subtree (^) previous subtree current subtree
Depth-First, Left-to-Right Tree Traversal
12. Finished traversing the right subtree of B
– Now go back to the previous subtree
CPSC 223 -‐-‐ Fall 2010 22
D
B G
A C E H
NULL NULL NULL NULL NULL F
NULL NULL
NULL NULL
previous subtree current subtree
Traversing binary trees
When should we “ visit ” a node?
- In a “ preorder ” traversal
- Visit the node first
- Then traverse left and right void traverse(const Node* subtreeRoot) { if(subTreeRoot == NULL) return; visit (subTreeRoot->item); traverse(subtreeRoot->leftChild); traverse(subtreeRoot->rightChild); } CPSC 223 -‐-‐ Fall 2010 25
D
B G
A C E H
F
OUTPUT: D B A C G E F H 1 2 3 4
What order will the nodes be visited?
Traversing binary trees
When should we “ visit ” a node?
- In an “ inorder ” traversal
- Traverse left
- Visit the node
- Then traverse right void traverse(const Node* subtreeRoot) { if(subTreeRoot == NULL) return; traverse(subtreeRoot->leftChild); visit (subTreeRoot->item); traverse(subtreeRoot->right); } CPSC 223 -‐-‐ Fall 2010 26
D
B G
A C E H
F
OUTPUT: A B C D E F G H 4 2 1 3
What order will the nodes be visited?
Traversing binary trees
When should we “ visit ” a node?
- In a “ postorder ” traversal
- Traverse left and right
- Then visit the node void traverse(const Node* subtreeRoot) { if(subTreeRoot == NULL) return; traverse(subtreeRoot->leftChild); traverse(subtreeRoot->rightChild); visit (subTreeRoot->item); } CPSC 223 -‐-‐ Fall 2010 27
D
B G
A C E H
F
OUTPUT: A C B F E H G D 8 3 1 2
What order will the nodes be visited?
Homework 6: Pre, Post, and In Order Print
These methods should each …
- Use the appropriate traversal (pre, post, inorder)
- Visit the nodes by printing them to an output stream (out)
- To print, use Entry’s operator<< method out << subTreeRoot->item;
- The result should look like a dictionary file
You’ll need to create
- Helper methods for the recursion, e.g., void prePrintTraversal(const Node*, ostream&)
- These methods should be declared protected or private CPSC 223 -‐-‐ Fall 2010 28 Dictionary’s << should use inorder
Removing Nodes
Cases to consider …
- The tree is empty!
- The item to be removed is not in the tree
- The node containing the item is a leaf node
- The node containing the item has one child
- The node containing the item has two children CPSC 223 -‐-‐ Fall 2010 31
Removing Nodes
- To remove a leaf node
- We delete the node
- And set the left or right pointer in the parent to NULL
- To remove a node with one child
- The parent of the node to be deleted “adopts” the child CPSC 223 -‐-‐ Fall 2010 32
B
A C
B
C
Remove A
C
A
B
C
B
Remove A
Removing Nodes
- Hardest Case : Remove a node with two children
- Both children cannot be “adopted” by the parent CPSC 223 -‐-‐ Fall 2010 33
D
B
A C
D
A C
Remove B
Removing Nodes
- To remove a node with two children
- one solution is to not delete the node …
- and instead replace the item in the node with another node’s item CPSC 223 -‐-‐ Fall 2010 34
D
B
A C
D
A C
Remove B
Removing Nodes
- The left-most node of the right subtree is often referred
to as the “ inorder successor ”
- The node visited next in an inorder traversal CPSC 223 -‐-‐ Fall 2010 37 right child le/-‐most node
E
B
A D
C
F
The inorder successor of B is C