π Lecture 12 β Operations on Binary Tree
π Overview: This lecture covers the fundamental operations on binary trees, their implementation in C++, and a practical application for searching duplicates in a list. It introduces the TreeNode class and the insert algorithm, demonstrating how binary trees can dramatically reduce the number of comparisons compared to linear data structures like linked lists.
ποΈ Topics Covered
The lecture discusses operations on binary tree including left, right, parent, brother, and info methods. It presents an application of binary trees for searching duplicates in a numerical list and explains the step-by-step process of building a binary search tree. The C++ implementation includes the TreeNode class and the insert function, followed by a detailed trace of the insertion algorithm showing how pointers move through the tree structure.
π Lecture Summary
Operations on Binary Tree
The lecture begins by discussing the methods of the tree data type. These methods can be classified into two categories: retrieval methods and construction methods. The retrieval methods include left(p) which returns a pointer to the left sub-tree, right(p) which returns a pointer to the right sub-tree, parent(p) which returns the father node of p, brother(p) which returns the brother node of p, and info(p) which returns the contents of node p. The construction methods include setLeft(p, x) which creates the left child node of p and sets the value x into it, and setRight(p, x) which creates the right child node of p with the info x.
Applications of Binary Tree
The binary tree is particularly useful when two-way decisions are made at each point. As an example, the lecture considers finding all duplicates in the following list of numbers: 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5. In addition to detecting duplicates, we may also require the frequency of numbers in the list. While a small list can be checked quickly, practical lists can range to thousands or millions of entries.
Searching for Duplicates
One approach for finding duplicates is to compare each number with all those that precede it. For example, to find duplicates for the number 4, we start scanning from the first number 14. Whenever we find the number 4, we remember the position and increment its frequency counter by 1. This comparison continues till the end of the list. However, this whole scanning process must be performed every time for each number, making it a long and time-consuming process.
A linked list can handle growth but does not reduce the number of comparisons since it is a linear data structure. To search a number in a linked list, we must begin from the start and traverse each node in linear fashion. The search operation actually becomes slower than in an array because the linked list is not contiguous.
The solution lies in using a binary tree. The binary tree is built in a special way. The first number in the list is placed in a node designated as the root. Initially, both left and right sub-trees of the root are empty. We take the next number and compare it with the number placed in the root. If it is the same, this means a duplicate. Otherwise, we create a new tree node. The new node becomes the left child if the second number is less than the root, or the right child if it is greater.
The construction process proceeds step by step:
- The first number 14 becomes the root
- Number 15 is compared with 14, found greater, and becomes the right child
- Number 4 is compared with 14, found smaller, and becomes the left child
- Number 9 is compared with 14 (smaller), then with 4 (greater), and becomes the right child of 4
- This process continues for all numbers, resulting in a complete binary tree structure
C++ Implementation of Binary Tree
The lecture presents the TreeNode class which serves as a factory for creating binary tree nodes. Since we want to use this class for different data types, it is implemented as a template class.
The class contains three private data members:
- object (type Object*) β stores the tree element (value) inside the node
- left (type TreeNode*) β stores a pointer to the left sub-tree
- right (type TreeNode*) β stores a pointer to the right sub-tree
The public methods include:
- A parameter-less constructor that initializes all data members to NULL
- A parameterized constructor that takes an object value and sets left and right to NULL
- getInfo() β returns the object (element) of the TreeNode
- setInfo(Object)* β sets the value of the object data member
- getLeft() β returns the pointer to the left sub-tree
- getRight() β returns the pointer to the right sub-tree
- setLeft(TreeNode)* β sets the pointer to the left sub-tree
- setRight(TreeNode)* β sets the pointer to the right sub-tree
- isLeaf() β returns 1 if the current node is a leaf node (both left and right are NULL), otherwise returns 0
The main program creates a root node and inserts all numbers from the list using the insert() function. The list ends with -1 as a delimiter marker.
The insert() function accepts two parameters: a pointer to a TreeNode object and the info to insert. It uses two pointer variables p and q, both initialized to point to the root. The while loop continues as long as the number to insert is not equal to the number in the current node AND q is not NULL. Inside the loop, p is assigned the value of q. If the number to insert is smaller than the number in p's node, q moves to the left child; otherwise, q moves to the right child.
After the loop terminates, if the numbers are equal, a duplicate message is displayed and the new node is deleted. If the number is less than the number in p's node, the new node is inserted as the left child; otherwise, it becomes the right child.
void insert(TreeNode<int>* root, int* info) {
TreeNode<int>* node = new TreeNode<int>(info);
TreeNode<int>* p, * q;
p = q = root;
while(*info != *(p->getInfo()) && q != NULL) {
p = q;
if(*info < *(p->getInfo()))
q = p->getLeft();
else
q = p->getRight();
}
if(*info == *(p->getInfo())) {
cout << "attempt to insert duplicate: " << *info << endl;
delete node;
}
else if(*info < *(p->getInfo()))
p->setLeft(node);
else
p->setRight(node);
}
Trace of insert
The lecture provides a detailed trace of inserting the number 17 into the existing tree. Initially, pointers p and q both point to the root node (containing 14). Since 17 is greater than 14, q moves to the right child (node with 15). Then p moves to where q was. This process continues: 17 is greater than 15, so q moves to the right child (node with 18). p follows. Now 17 is less than 18, so q moves to the left child (node with 16). p follows. Finally, 17 is greater than 16, so q tries to move to the right child, but it is NULL, causing the while loop to terminate. The new node with 17 is then inserted as the right child of the node with 16.
π‘ Why this matters: This algorithm will be used rigorously in future lectures. Complete understanding of the pointer movements and comparison logic is essential for comprehending more complex tree implementations.
β Key Takeaways
A binary tree drastically reduces the number of comparisons when searching for duplicates compared to linear data structures like linked lists or arrays. The tree is built by comparing each new number with existing nodes, placing smaller numbers to the left and larger numbers to the right, which naturally organizes data for efficient search. The insert algorithm uses two pointers (p and q) to traverse the tree, where q explores forward and p follows behind, allowing the algorithm to identify the correct insertion point when q reaches NULL. The C++ TreeNode class is implemented as a template to handle different data types and provides essential methods for tree construction and traversal. Understanding the pointer-based insertion algorithm is fundamental for all future tree-based data structures.
π§ Quick Revision Questions
- What are the two categories of methods for the binary tree data type, and what is the purpose of each?
- How does the binary tree insertion algorithm decide whether to place a new node as a left child or right child?
- In the insert() function, what causes the while loop to terminate, and what happens after termination?
- What is the purpose of having two pointer variables p and q in the insertion algorithm instead of just one?
- How does the isLeaf() method determine whether a node is a leaf, and what does it return in each case?
π Lecture 13 β Cost of Search, Binary Search Tree (BST), Traversing a Binary Tree
π Overview: This lecture focuses on the efficiency gains of using a binary tree over a linked list for search operations. It formally introduces the Binary Search Tree (BST) and explores the three fundamental methods for traversing a binary tree: preorder, inorder, and postorder. The C++ implementations of these traversal methods are provided, and the concept of recursion is explained in the context of tree traversal.
ποΈ Topics Covered
This lecture begins by analyzing the cost of search in a binary tree, demonstrating a significant performance advantage over a linked list for large datasets. It then formally defines a Binary Search Tree (BST), where the left subtree contains smaller values and the right subtree contains larger values. The core of the lecture explains the three methods of traversing a binary tree β preorder, inorder, and postorder β with their respective C++ code implementations. An example tree is used to illustrate the output of each traversal, with a key observation that inorder traversal produces sorted data. Finally, the concept of recursion is discussed as the mechanism enabling these traversals.
π Lecture Summary
Cost of Search
To find if a number exists in a binary tree, the search process goes down one level per comparison. In a tree of depth d, the maximum number of comparisons is equal to d. For a complete binary tree with n nodes, its depth d is approximately logβ(n). This means that searching a tree of 100,000 nodes requires at most about 20 comparisons, while a linked list of the same size might require up to 100,000 comparisons. The difference in time is massive: microseconds vs. hours for a billion-node structure.
π Definition β Complete Binary Tree: A binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
π Formula: d = logβ(n + 1) β 1 β The depth (d) of a complete binary tree with n nodes.
π Example: For a tree with 100,000 nodes, the depth d is calculated as logβ(100001) β 1, which is approximately 20.
π‘ Why this matters: The logarithmic search time of a binary tree makes it vastly superior to a linked list for storing and searching large datasets, converting a search that takes hours into one that takes microseconds.
Binary Search Tree (BST)
A binary tree has the specific property that, for any node, all items in its left subtree are smaller than the node's value, and all items in its right subtree are larger. This structure is called a Binary Search Tree (BST). Because the data is stored in this specific order, it is also known as an ordered tree. This ordering is the foundation for the efficient search operations discussed earlier.
π Definition β Binary Search Tree (BST): A binary tree with the property that the items in the left sub-tree are smaller than the root, and the items in the right sub-tree are larger than the root.
Traversing a Binary Tree
Traversing a binary tree is different from traversing a linear list because a node has two distinct children. A generic binary tree consists of three components: a root (N), a left subtree (L), and a right subtree (R). From the six possible permutations of these three components, three fundamental traversal methods are selected:
- Preorder (N, L, R)
- Inorder (L, N, R)
- Postorder (L, R, N)
The C++ code for these methods uses recursion.
π Definition β Preorder Traversal: Visit the root first, then traverse the left subtree, then the right subtree. π Definition β Inorder Traversal: Traverse the left subtree first, then visit the root, then the right subtree. π Definition β Postorder Traversal: Traverse the left subtree, then the right subtree, then visit the root.
C++ code
The code for these methods is short, recursive, and applies to trees of any size.
preorder method:
void preorder(TreeNode<int>* treeNode) {
if( treeNode != NULL ) {
cout << *(treeNode->getInfo()) << " ";
preorder(treeNode->getLeft());
preorder(treeNode->getRight());
}
}
Logic: Print the node, then recursively print the left subtree, then the right subtree.
inorder method:
void inorder(TreeNode<int>* treeNode) {
if( treeNode != NULL ) {
inorder(treeNode->getLeft());
cout << *(treeNode->getInfo()) << " ";
inorder(treeNode->getRight());
}
}
Logic: Recursively traverse the left subtree, then print the node, then recursively traverse the right subtree.
postorder method:
void postorder(TreeNode<int>* treeNode) {
if( treeNode != NULL ) {
postorder(treeNode->getLeft());
postorder(treeNode->getRight());
cout << *(treeNode->getInfo()) << " ";
}
}
Logic: Recursively traverse the left subtree, then the right subtree, then print the node.
These are called by passing the root pointer: inorder(root);
Example
Consider a tree with the root value 14 and the following structure:
14
/ \
4 15
/ \ \
3 9 18
/ / \
7 16 20
/ \
5 17
Preorder output: 14 4 3 9 7 5 15 18 16 17 20 Explanation: Root is printed first (14). Then the entire left subtree is traversed (4, 3, 9, 7, 5), followed by the right subtree (15, 18, 16, 17, 20).
Inorder output: 3 4 5 7 9 14 15 16 17 18 20 Explanation: The left subtree is traversed first. The first number printed is the leftmost node (3). The output is in ascending sorted order.
Postorder output: 3 5 7 9 4 17 16 20 18 15 14 Explanation: The root (14) is printed last.
A key takeaway is that Inorder traversal of a BST produces a sorted list of the numbers.
Recursion is the mechanism by which these functions call themselves to traverse the subtrees. Each function call is pushed onto a run-time stack, which keeps a record of parameters, local variables, and the return address for that specific call.
π Example: In the inorder traversal, the function recursively calls itself for the left child of 14. It calls itself for the left child of 4, which is 3. When the function checks 3, it has no left child and its left subtree call returns. The value "3" is then printed. This shows the "left, root, right" order being implemented recursively.
β Key Takeaways
The critical concept is the dramatic performance advantage of a Binary Search Tree (BST) over a linked list for search operations, achieving a search cost of O(log n) compared to O(n). You must understand the property of a BST that enables this: left subtrees hold smaller values, and right subtrees hold larger values. The three tree traversal methods (preorder, inorder, postorder) are fundamental, and you must know their order and output. The key insight is that an inorder traversal of a BST yields the data in sorted order. Finally, the recursive nature of these traversals is essential, and you should be able to walk through the algorithm's execution on a given tree.
π§ Quick Revision Questions
- What is the approximate depth of a complete binary tree with 1,000,000 nodes?
- Define the property of a Binary Search Tree (BST). What makes it an "ordered tree"?
- For the tree in Figure 13.6 of the lecture (with root 40), what is the preorder traversal?
- Why does an inorder traversal of a BST automatically produce a sorted list of values?
- Explain the concept of recursion as it is used in the binary tree traversal functions. How does the run-time stack manage these calls?
π Lecture 14 β Data Structures
π Overview: This lecture explores the recursive mechanisms behind binary tree traversals (preorder, inorder, and postorder) in depth, contrasting them with non-recursive implementations using explicit stacks. Understanding how recursion uses the call stack is essential for mastering tree operations and appreciating the efficiency of recursive solutions on inherently recursive data structures.
ποΈ Topics Covered
This lecture details how recursive calls are implemented using the system stack, then provides a step-by-step trace of recursive preorder and inorder traversals on a specific binary search tree. It subsequently introduces non-recursive inorder traversal using an explicit stack, compares the two approaches, and previews level-order traversal as a breadth-oriented alternative to depth-first methods.
π Lecture Summary
Recursive Calls
Function calls, whether to another function or recursively to itself, use the system stack to store parameters, return addresses, and local variables. When a called function finishes, the stored return address is used to resume execution of the caller. Recursion is implemented with the same stack mechanism as any other function call.
π Definition β Recursion: A technique where a function calls itself to solve a smaller instance of the same problem, relying on a terminating condition to stop.
Preorder Recursion
In preorder traversal, the node's value is printed before traversing its left and right subtrees. Starting at the root (14), the value is printed. The function then calls itself recursively on the left subtree (node 4), prints 4, and continues to the left subtree of 4 (node 3), printing 3. The left subtree of 3 is NULL, which acts as the terminating condition β the recursion stops on that branch. After a NULL left subtree is encountered at node 3, the return address from the stack takes control back to node 3, and its right subtree (also NULL) is checked. Control returns to node 4, whose right subtree (node 9) is then traversed in preorder. This pattern of pushing calls onto the stack (indicated by increasing dots in figures) and popping them upon return continues until all nodes are visited.
π Terminology: The pattern for preorder is: Root β Left subtree β Right subtree.
π Example: On the given tree (root 14), the preorder recursion prints: 14, 4, 3, 9, 7, 5 after traversing the left subtree of 14. The right subtree (starting with 15) prints: 15, 18, 16, 17, 20, completing the full preorder output: 14, 4, 3, 9, 7, 5, 15, 18, 16, 17, 20.
π‘ Why this matters: The recursive call stack mirrors the tree's structure, pushing deeper as we descend and popping back as we return β a natural fit for hierarchical data.
Inorder Recursion
In inorder traversal, the nodeβs value is printed after traversing its left subtree but before traversing its right subtree. Starting with root 14, the function recursively calls itself on the left subtree (node 4), which in turn calls on its left subtree (node 3). From node 3, its left subtree is NULL β the terminating condition. After returning from NULL, the node (3) is printed, then its right subtree (NULL) is checked. Control returns to node 4, which prints 4 after its left subtree is done. The recursion proceeds to the right subtree (node 9), following the same pattern: traverse left (to 7 then 5), print when left is NULL, then traverse right. After completing node 4's subtree, control returns to root 14, which is printed. The same pattern is applied to the right subtree (15 β 18 β 16 β 17 β 20).
π Terminology: The pattern for inorder is: Left subtree β Root β Right subtree.
π Example: On the given tree, the inorder traversal prints: 3, 4, 5, 7, 9, 14, 15, 16, 17, 18, 20 β a sorted ascending sequence, as expected from a binary search tree.
π‘ Why this matters: Inorder traversal of a binary search tree always yields sorted output, making it invaluable for retrieval and sorting operations.
Non-Recursive Traversal
Non-recursive traversal replaces the system's call stack with an explicit stack created by the programmer. For inorder, the algorithm uses a Stack<TreeNode<int>*> to store nodes. A pointer p starts at the root. An inner while loop pushes all nodes along the leftmost path (root, then left child, then its left child, etc.) onto the stack. When a left child is NULL, the loop exits. A node is popped from the stack, its value printed, and p is set to its right child. The outer loop repeats until the stack is empty and p is NULL.
π Definition β Explicit stack: A user-defined stack data structure that the programmer manually manages to store and retrieve nodes during iteration.
π Example: On the tree with root 14, the explicit stack pushes 14, 4, 3 in sequence. After exiting the inner loop (left of 3 is NULL), 3 is popped and printed. Then 4 is popped and printed. Then nodes 9, 7, 5 are pushed; 5 is popped and printed; 7 popped; 9 popped; 14 popped; then 15, 18, 16, 17, 20 are processed similarly.
π Comparison: Recursive inorder uses inorder(14), inorder(4), inorder(3)... while non-recursive uses push(14), push(4), push(3).... The only difference is the function name vs. push operation β the logic is identical.
Traversal Trace
Comparing recursive and non-recursive inorder shows no difference in the order of node processing. Both use the stack data structure: recursive uses the system stack implicitly; non-recursive uses an explicit stack. The recursive version is more readable (3-4 lines vs. many lines for non-recursive) and is often more efficient for recursive data structures because the stack operations are highly optimized at the assembly level. Program readability is crucial β well-commented, clean code is easier to debug and maintain.
π Definition β Readability: The ease with which a human can understand the logic and flow of a program, often enhanced by recursive solutions on recursive data structures.
Level-Order Traversal
Level-order traversal visits nodes level by level, from left to right, in contrast to depth-first traversals (preorder, inorder, postorder). It is not recursive but breadth-oriented.
π Example: On the given tree, level-order traversal visits: 14 (level 0), then 4, 15 (level 1), then 3, 9, 18 (level 2), then 7, 16, 20 (level 3), then 5, 17 (level 4). Output: 14, 4, 15, 3, 9, 18, 7, 16, 20, 5, 17.
π‘ Why this matters: Level-order is used in algorithms that require processing nodes by depth, such as printing trees visually or finding shortest paths in unweighted graphs (when applied to trees).
β Key Takeaways
All depth-first traversals (preorder, inorder, postorder) are naturally implemented recursively, leveraging the system stack. The specific order of visiting the root relative to left and right subtrees defines each traversal type. A non-recursive inorder traversal using an explicit stack produces the same output as its recursive counterpart. Recursive solutions are generally preferred for recursive data structures due to superior readability and often-equal or better efficiency. A terminating condition is mandatory to prevent infinite recursion and stack overflow. Level-order traversal, in contrast, visits nodes level by level.
π§ Quick Revision Questions
- What is the terminating condition for recursive tree traversals, and why is it essential?
- Trace the first five steps (nodes visited and order) of preorder traversal on a tree with root 10, left child 5, right child 15, left child of 5 = 2, right child of 5 = 7.
- Explain how the explicit stack is used in non-recursive inorder traversal. What is pushed, when, and what is the LIFO implication?
- Compare and contrast the recursive inorder and non-recursive inorder traversals in terms of readability and efficiency.
- What is the key difference between depth-first traversals (preorder, inorder, postorder) and level-order traversal in terms of node visitation order?
π Lecture 15 β Data Structures
π Overview: This lecture explores level-order traversal of binary trees using queues, demonstrates how to store non-integer data types like strings in binary search trees, and covers the complex operation of deleting nodes from a BST. Understanding these concepts is crucial for mastering tree operations in real-world applications like sorting and directory management.
ποΈ Topics Covered
The lecture covers level-order traversal of a binary tree using a queue data structure with step-by-step execution on a sample tree, storing other types of data in binary trees including strings, building a binary search tree (BST) with strings using lexicographic comparison and the strcmp function, and the three cases of deleting a node from a BST: leaf nodes, nodes with one child, and nodes with two children requiring inorder successor replacement.
π Lecture Summary
Level-order Traversal of a Binary Tree
In the last lecture, we implemented tree traversal in preorder, postorder, and inorder using recursive and non-recursive methods. Level-order traversal prints the binary tree level by level, starting from the root and moving to each subsequent level. In the example tree shown in Fig 15.1, at the first level there is only node 14; at the second level, nodes 4 and 15; at the third level, 3, 9, and 18; at the fourth level, 7, 16, and 20; and at the fifth and final level, nodes 5 and 17. The output of level-order traversal for this tree would be: 14 4 15 3 9 18 7 16 20 5 17.
Surprisingly, the implementation of level-order traversal is simple using a non-recursive method and by employing a queue instead of a stack. A queue is a FIFO (First-In-First-Out) structure, which makes level-order traversal easier because levels come turn by turn.
void levelorder( TreeNode <int> * treeNode )
{
Queue <TreeNode<int> *> q;
if( treeNode == NULL ) return;
q.enqueue(treeNode);
while( !q.empty() )
{
treeNode = q.dequeue();
cout << *(treeNode->getInfo()) << " ";
if(treeNode->getLeft() != NULL )
q.enqueue( treeNode->getLeft());
if(treeNode->getRight() != NULL )
q.enqueue( treeNode->getRight());
}
cout << endl;
}
The method levelorder accepts a pointer of type TreeNode <int>. It first creates a queue q containing TreeNode<int>* objects. If treeNode is NULL, the method returns immediately. Otherwise, the first node is added to the queue. The while loop runs until the queue becomes empty. Inside the loop, a node is dequeued and its integer value is printed. Then, if the left subtree exists, it is inserted into the queue, and if the right subtree exists, it is also inserted.
π‘ Why this matters: The selection of appropriate data structure is very critical for a successful solution. Without using the queue data structure, the problem of level-order tree traversal would not have been so easy. The turn-by-turn nature of levels guided us to use a queue.
π Definition β Level-order traversal: A tree traversal method that visits nodes level by level from top to bottom, left to right, using a queue data structure.
π Queue operation: enqueue β add to back; dequeue β remove from front β enables FIFO processing of tree levels.
π Example: For the tree with root 14, left child 4, right child 15, and their subtrees, the level-order traversal proceeds as:
- Queue: [14] β dequeue 14, print "14", enqueue 4 and 15 β Queue: [4, 15]
- Dequeue 4, print "4", enqueue 3 and 9 β Queue: [15, 3, 9]
- Dequeue 15, print "15", enqueue 18 β Queue: [3, 9, 18]
- Dequeue 3, print "3", no children β Queue: [9, 18]
- Dequeue 9, print "9", enqueue 7 β Queue: [18, 7]
- Continue until queue is empty β Output: 14 4 15 3 9 18 7 16 20 5 17
Storing Other Types of Data in Binary Tree
Until now, we have placed int numbers in tree nodes because they were easier for sorting and comparison problems. However, we can put any data type in tree nodes depending upon the problem. For example, if we want to enter the names of people in a telephone directory, we build a binary tree of strings.
Binary Search Tree (BST) with Strings
Let's write C++ code to insert non-integer data in a binary search tree. The function wordTree() constructs a root tree node containing char type data, then creates a static character array word containing words like "babble", "fable", etc., with NULL as the last element. The first word is placed in the root node, and a for loop inserts the remaining words using the insert method until a NULL is encountered. After insertion, the inorder() method prints the tree node elements in sorted order.
The insert(TreeNode<char> * root, char * info) method accepts a pointer to a TreeNode containing char type elements and a pointer to char for the new word. It creates a new TreeNode with the info value and declares two pointers p and q of type TreeNode<char>, both initially pointing to root.
For string comparison, we use the lexicographic order based on ASCII values. For example, the ASCII value of 'B' (66) is greater than 'A' (65), so 'B' is greater than 'A'. Similarly, a word starting with 'A' is smaller than words starting with 'B' or any other character up to 'Z'.
The strcmp function from the standard C library compares two strings. It takes info as the first parameter and returns: a negative number if info is smaller, 0 if both are equal, and a positive number if info is greater.
Inside the while loop, strcmp compares info with the value in the node pointed to by p. The loop terminates when a duplicate is found (strcmp returns 0) or when q points to NULL. If the new word is smaller, we traverse to the left subtree; otherwise, we go to the right subtree. After the loop, if duplication is found, a message is displayed and the new node is deleted. Otherwise, the new node is inserted to the left or right of the current node based on comparison.
The output of inorder traversal on the tree built from the word array is:
abandon
abash
accuse
adhere
advise
babble
backup
bandit
cease
chain
daily
debunk
eagle
economy
fable
feeder
fetch
gain
genius
jacket
Notice the words are printed in sorted increasing order. Building a binary search tree and doing an inorder traversal leads to a sorting algorithm.
π Definition β Lexicographic order: The ordering of strings based on the ASCII values of their characters, where 'A' < 'B' < ... < 'Z' and shorter strings typically precede longer ones when they share a common prefix.
π strcmp function: strcmp(string1, string2) returns negative if string1 < string2, 0 if equal, positive if string1 > string2.
π Example: When inserting "babble" (root), then "fable" (greater, right), then "jacket" (greater, right of "fable"), then "backup" (less than "fable" but greater than "babble", so left of "fable" and right of "babble"), the tree is built following BST rules. Inorder traversal produces the sorted output shown above.
Deleting a Node From BST
Deletion is often the hardest operation with many data structures. Once we have found the node to be deleted, we need to consider several possibilities.
Case 1: The node is a leaf β It can be deleted quite easily. For example, deleting node containing number 3 (a leaf node) simply requires deleting it and pointing the parent's right subtree pointer to NULL.
Case 2: The node has one child β The node can be deleted after its parent adjusts a pointer to bypass the node and connect to the inorder successor. For example, deleting node containing number 4 requires adjusting the right subtree pointer in the node containing value 2 to point to the inorder successor of 4. The important point is that the inorder traversal order must be maintained after the delete.
Case 3: The node has both left and right subtrees β This is more complicated. The strategy is to replace the data of this node with the smallest data of the right subtree (the inorder successor) and recursively delete that node.
Consider deleting the node containing number 2 from a tree. The inorder traversal gives: 1, 2, 3, 4, 5, 6, 8. First, find the leftmost node in the right subtree of node 2, which is node containing number 3. Copy the contents of this leftmost node (3) to the node to be deleted (2). Then delete the original leftmost node containing value 3. Since this node has no left subtree but may have a right subtree, its deletion follows Case 2. After deletion, inorder traversal gives: 1, 3, 4, 5, 6, 8 β still sorted.
π Definition β Inorder successor: The node that comes immediately after a given node in an inorder traversal of a binary search tree; it is the leftmost node in the right subtree of the node to be deleted.
π Example: Deleting node 2 (with both children) from a BST containing values 1, 2, 3, 4, 5, 6, 8:
- Find inorder successor of 2 β leftmost node in right subtree β node containing 3
- Copy 3 to node 2's position
- Delete the original node containing 3 (now a leaf or one-child node)
- Tree maintains BST property and inorder traversal yields sorted order: 1, 3, 4, 5, 6, 8
β Key Takeaways
Level-order traversal is efficiently implemented using a queue data structure in a non-recursive manner, where nodes are processed level by level in FIFO order β this demonstrates the critical importance of selecting appropriate data structures during the design phase before writing code. Binary search trees can store any data type, including strings, using appropriate comparison functions like strcmp for lexicographic ordering, and performing an inorder traversal on such a BST produces a sorting algorithm. Deleting nodes from a BST requires handling three distinct cases: leaf nodes (simple deletion), nodes with one child (bypass with parent pointer adjustment), and nodes with two children (replace with inorder successor and recursively delete that successor). The inorder traversal of a BST always produces sorted output, whether the tree contains integers or strings, making it a fundamental property for sorting and searching operations. When deleting a node with two children, the key strategy is to copy the inorder successor's data to the target node and then delete the successor node, which ensures the BST property is preserved.
π§ Quick Revision Questions
- What data structure is used for level-order traversal and why is it more suitable than a stack?
- How does the
preordertraversal differ fromlevel-ordertraversal in terms of node visit order? - In the string BST insertion, what function is used to compare strings and what are its possible return values?
- What are the three cases for deleting a node from a BST, and what strategy is used for the case where a node has both children?
- Why does inorder traversal of a binary search tree always produce sorted output regardless of the data type?
π Lecture 16 β Deleting a Node in BST & Binary Search Tree Class
π Overview: This lecture covers the complete procedure for deleting nodes from a Binary Search Tree (BST), handling three distinct cases. It also presents the full C++ implementation of the
removemethod, thefindMinhelper function, and the complete Binary Search Tree class definition with its interface and implementation files.
ποΈ Topics Covered
The lecture begins with a detailed explanation of the three cases for deleting a node from a BST: leaf node deletion, node with one child, and node with two children (using inorder successor replacement). It then presents the complete C++ code for the remove method and the findMin function, explaining recursion and tail recursion. Finally, it introduces the complete Binary Search Tree Class including its header file (BinarySearchTree.h) with the BinaryNode class definition, the implementation file (BinarySearchTree.cpp), and a test program.
π Lecture Summary
Deleting a node in BST
There are three cases for deleting a node from a BST. Case I: The node to be deleted is a leaf node (no children). This is the simplest caseβyou make the pointer in the parent node pointing to this node NULL and release the dynamically allocated memory. Case II: The node to be deleted has either a left child (subtree) or a right child (subtree). Case III: The node to be deleted has both left and right children. This is the most difficult case. The strategy is to find the inorder successor of the node to be deleted, which is the smallest element in the nodeβs right subtree. You copy the inorder successorβs value into the node to be deleted, then delete the inorder successor node (which will fall under Case I or Case II).
π Definition β Inorder Successor: The node that appears next in an inorder traversal of a BST. For a node with two children, the inorder successor is the leftmost (minimum) node in its right subtree.
π Example: To delete node 2 from a BST (which has children 1 on left and 5 on right), find the inorder successor by going to the right subtree (rooted at 5) and finding the smallest elementβthis is node 3. Copy value 3 into node 2. Now node 3 (the inorder successor) must be deleted. Since node 3 has only a right child (node 4), this falls under Case II. Connect node 5's left pointer directly to node 4, deleting node 3.
C++ code for remove
The method is named remove because delete is a reserved keyword in C++. The remove method returns a pointer to TreeNode and takes two arguments: a pointer to the root of the tree (TreeNode<int>* tree) and an integer info (the value to delete). A temporary variable cmp is computed as info - *(tree->getInfo()). If cmp < 0, the node is in the left subtree; the method calls remove recursively on the left child and reassigns the left pointer. If cmp > 0, the node is in the right subtree, and the method calls remove recursively on the right child. If the node has two children (checked with tree->getLeft() != NULL && tree->getRight() != NULL), the method finds the minimum node in the right subtree using findMin, copies its value into the current node, and then recursively removes that minimum node from the right subtree. The else block handles the cases where the node has zero or one childβit saves the node to delete, replaces it with its non-NULL child (or NULL if both are NULL), and then deletes the original node.
π Definition β Tail Recursion: A recursive call that appears as the last statement in a function. It can be replaced with a loop to eliminate the recursion stack overhead.
π Example: The findMin method is implemented with tail recursion. It checks if tree == NULL (return NULL), or if tree->getLeft() == NULL (return tree as the minimum), otherwise it recursively calls findMin(tree->getLeft()). This can be rewritten as an iterative loop that traverses left children until reaching a node with a NULL left pointer.
Binary Search Tree Class (BST)
The BST class is implemented with a header file (BinarySearchTree.h) containing the interface and a separate implementation file (BinarySearchTree.cpp). The .h file uses conditional compilation with #ifndef, #define, and #endif to prevent multiple inclusions. It contains a forward declaration of BinarySearchTree before defining BinaryNode. The BinaryNode class stores an element (template type EType), and pointers left and right. Its constructor uses an initialization list to set element(theElement), left(lt), and right(rt). The BinarySearchTree class is declared as a friend of BinaryNode, allowing it to access private members. The public interface includes constructors, destructor, findMin, findMax, find, isEmpty, printTree, insert, remove, and assignment operator. Private helper methods have different signatures (including the tree root pointer as an additional parameter). The ITEM_NOT_FOUND constant is used to signal failed finds. The implementation file provides method bodies, including a recursive findMin, an iterative findMax, and a clone method for deep copying. A test program inserts numbers 1 to 30, prints the tree, removes all even numbers, and prints the resulting tree.
π Definition β Forward Declaration: A declaration that tells the compiler a class exists before its full definition, allowing it to be referenced by other classes (e.g., as a friend).
π Definition β Initialization List: In C++, the list after the colon in a constructor that initializes member variables by calling their constructors explicitly, rather than assigning values inside the constructor body.
π‘ Why this matters: The friend declaration between BinaryNode and BinarySearchTree is necessary because BinarySearchTree needs direct access to node internals (for efficient tree operations) while keeping those details hidden from external users.
β Key Takeaways
Students must understand the three deletion cases for BST: leaf (simply remove), one child (replace with that child), and two children (replace with inorder successor, then recursively delete successor). The remove method code uses recursion to traverse the tree, compares using cmp, and handles each case with specific logic. The findMin function demonstrates tail recursion (which can be replaced by iteration) and locates the leftmost node. The complete BST class uses templates for genericity, separates interface (.h) from implementation (.cpp), and employs C++ features like forward declarations, friend classes, and initialization lists. The public methods are simple wrappers that call private recursive helpers.
π§ Quick Revision Questions
- What are the three cases for deleting a node from a Binary Search Tree, and how does the algorithm differ for each?
- How is the
removemethod implemented in C++ to handle the case where a node has two children? What is the role of thefindMinfunction? - What is an inorder successor, and why is the inorder successor guaranteed to have at most one child?
- In the BinarySearchTree class, why is
BinarySearchTreedeclared as a friend ofBinaryNode? What purpose does the forward declaration serve? - How does the
findMinmethod work recursively, and how can its tail recursion be replaced with an iterative loop?