π Lecture 24 β Deletion in AVL Tree & Other Uses of Binary Trees
π Overview: This lecture completes the discussion of deletion in AVL trees by examining five distinct cases that arise when a node is removed. It also introduces other practical applications of binary trees, including expression trees, parse trees in compilers, and query trees in databases. Understanding deletion cases is critical for maintaining AVL tree balance, while expression trees form the foundation of compiler design and optimization.
ποΈ Topics Covered
The lecture covers the five cases of node deletion in AVL trees, each requiring specific actions including changing balance factors, single rotations, or double rotations. It then explores other uses of binary trees such as expression trees, parse trees for compilers and SQL queries, and how compilers perform optimization using common subexpression detection and graph conversion.
π Lecture Summary
Deletion in AVL Tree
When a node is deleted from an AVL tree, the tree can become unbalanced. The balance factor of each node is calculated and rotations are performed for unbalanced nodes. Unlike insertion where only one node's balance is adjusted, deletion may require rotation propagation all the way to the root node. There are five cases to consider, involving different balance factor values and subtree deletion locations.
π Definition β Balance Factor: The height difference between left and right subtrees of a node. In AVL trees, acceptable values are -1, 0, or 1.
Case 1a: The parent of the deleted node had a balance of 0 and a node was deleted in the parent's left subtree.
After deletion, the balance changes from 0 to -1 (tilted toward right), which remains within AVL limits.
Action required: Change the balance of the parent node and stop. No further effect on balance of any higher node.
π Example: In a tree with root 4, nodes 2 and 6 at level 1, and nodes 1,3,5,7 at level 2, deleting node 1 tilts node 2 toward right (-1). The root node 4 remains unchanged. No rotation needed.
Case 1b: The parent of the deleted node had a balance of 0 and the node was deleted in the parent's right subtree.
Action required: Change the balance of the parent node and stop. No further effect on balance of any higher node (same as 1a).
Case 2a: The parent of the deleted node had a balance of 1 and the node was deleted in the parent's left subtree.
After deletion, the balance becomes 0, but this change may affect higher nodes.
Action required: Change the balance of the parent node. May have caused imbalance in higher nodes so continue up the tree.
Case 2b: The parent of the deleted node had a balance of -1 and the node was deleted in the parent's right subtree.
Action required: Change the balance of the parent node. May have caused imbalance in higher nodes so continue up the tree.
Case 3a: The parent had a balance of -1 and the node was deleted in the parent's left subtree, and the right subtree was balanced.
After deletion, the height of left subtree changes to h-1.
Action required: Perform single rotation, adjust balance. No effect on balance of higher nodes so stop here.
π Formula: Single rotation swaps parent A and child B β A becomes left subtree of B, and B's left subtree becomes A's right subtree.
Case 4a: Parent had a balance of -1 and the node was deleted in the parent's left subtree, and the right subtree was unbalanced.
Action required: Double rotation at B. May have affected the balance of higher nodes, so continue up the tree.
π Formula: Double rotation involves two rotations β first rotate the child, then rotate the parent. Node A becomes left child of new root B, node C becomes right child of new root B.
Case 5a: The parent had a balance of -1 and the node was deleted in the parent's left subtree, and the right subtree was unbalanced (with a different configuration than Case 4a).
Action required: Single rotation at B. May have affected the balance of higher nodes, so continue up the tree.
π‘ Why this matters: The phrase "continue up the tree" appears in several cases. This is implemented using recursion β work to be done later is pushed onto the stack, deletion occurs when reaching the desired node, and rotation operations are performed while traversing back to the root node.
Symmetrical cases 3b, 4b, and 5b exist for deletion from the right subtree, which follow analogous patterns.
Other Uses of Binary Trees
A characteristic of binary trees is that values inside nodes on the left of a node are smaller than the node's value, and values on the right are greater.
For searching, binary trees require traversing up to logβn levels maximum. AVL trees in the worst case require searching 1.44 logβn levels.
Expression Trees
Expression trees, more general parse trees, and abstract syntax trees are significant components of compilers. They represent mathematical expressions in tree form where operators become internal nodes and operands become leaf nodes.
π Example: The expression (a+b*c)+((d*e+f)*g) is represented as a tree where b and c share a parent * node, a connects to + along with b*c, and the right subtree represents (d*e+f)*g with the root node being +.
Parse Tree in Compilers
A parse tree represents the syntactic structure of a programming language statement according to a grammar.
π Example: The assignment A := A + B * C is represented as a parse tree with root node <assign>, which has three parts: <id> (identifier A on left), assignment operator :=, and <expr> representing A + B * C on the right.
The parse tree expands through grammar rules:
<assign>β<id> := <expr><id>β A | B | C<expr>β<expr> + <term>|<term><term>β<term> * <factor>|<factor><factor>β<id>
Parsing is the process of reading and extracting the required structure. Compilers parse computer languages to form parse trees. Speech recognition and handwriting recognition also involve similar tree structures.
Parse Tree for an SQL Query
Parse trees are used in database query processing. Consider a database with tables:
- StarsIn(title, year, starName)
- MovieStar(name, address, gender, birthdate)
SQL query: SELECT title FROM StarsIn, MovieStar WHERE starName = name AND birthdate LIKE '%1960'
This query retrieves movie titles where actors were born in 1960. The database engine creates a tree with root <Query> that branches into SELECT, FROM, WHERE, and Condition subnodes, expanding downward to attributes and values.
Compiler Optimization
Compilers detect common subexpressions within parse trees β identical subtrees that appear multiple times.
π Example: In the expression (f+d*e) + ((d*e+f)*g), the subexpressions f+d*e and d*e+f are equivalent (commutative property of addition).
Instead of calculating common subexpressions again, compilers calculate them once and reuse them. The optimizer (part of the compiler) creates a directed acyclic graph (DAG) by connecting both paths to the same subtree. This graph has two or more different paths to reach a node, making it no longer a tree but a graph with directed edges.
π‘ Why this matters: Converting expression trees to graphs for common subexpression elimination dramatically improves runtime efficiency by avoiding redundant calculations.
β Key Takeaways
The five deletion cases in AVL trees are distinguished by the balance factor of the parent node (0, 1, or -1) and which subtree the deletion occurs in (left or right). Cases where the parent had balance 0 (1a and 1b) only require changing the balance factor without rotation, while cases 2a and 2b may require propagating balance adjustments upward through recursion. Cases 3a, 4a, and 5a require single or double rotations depending on whether the sibling subtree is balanced or unbalanced, with some cases requiring continuation up the tree and others stopping immediately. Beyond search trees, binary trees serve as expression trees and parse trees in compilers, databases, and speech recognition systems, with optimizers converting common subexpressions into directed acyclic graphs for efficiency.
π§ Quick Revision Questions
- In which deletion case(s) is no rotation required and only the balance factor of the parent node is changed?
- What is the difference between Case 3a and Case 4a in terms of the sibling subtree's condition and the type of rotation required?
- Why does deletion in AVL trees potentially require "continuing up the tree" while insertion typically only affects one node?
- How does a compiler's optimizer convert an expression tree into a graph for common subexpression elimination?
- What are the three main components of an assignment statement's parse tree in a compiler, and how do they correspond to the grammar rules?
π Lecture 25 β Expression Tree and Huffman Encoding
π Overview: This lecture continues the discussion on expression trees, demonstrating how to build them from postfix expressions using a stack. It also introduces Huffman Encoding, a data compression technique that uses binary trees to reduce the size of transmitted data by assigning variable-length codes based on character frequency.
ποΈ Topics Covered
The lecture first revisits expression trees, covering their structure with operators as inner nodes and operands as leaf nodes, then explains inorder traversal with parenthesis and postorder traversal to obtain postfix expressions. It presents a step-by-step algorithm to build an expression tree from a postfix expression using a stack. The second half introduces Huffman Encoding, discussing data compression, its importance in networking, and the algorithm to build a Huffman tree by combining character-frequency nodes.
π Lecture Summary
Expression tree
The lecture revisits expression trees, which are binary trees where inner nodes contain operators and leaf nodes contain operands. For binary operators like + and *, the tree is a binary tree, but unlike a Binary Search Tree (BST) , there is no sortingβthe structure reflects operator precedence. Unary operators like negation (-) would produce a node with only one child.
The inorder traversal of an expression tree yields the infix expression, but without parentheses. For the tree shown, inorder traversal gives: a+b*c+d*e+f*g. To include parentheses, a modified inorder routine is used:
void inorder(TreeNode<int>* treeNode)
{
if( treeNode != NULL ){
cout << "(";
inorder(treeNode->getLeft());
cout << ")";
cout << *(treeNode->getInfo());
cout << "(";
inorder(treeNode->getRight());
cout << ")";
}
}
This routine places an opening parenthesis before traversing the left subtree and a closing parenthesis after, and similarly for the right subtree. Executing this on the example tree produces: ( a + ( b * c )) + ((( d * e ) + f ) * g ).
Postorder traversal of the same tree yields the postfix expression: a b c * + d e * f + g * +. In postfix form, no parentheses are needed because the operator order inherently defines precedence.
Building an Expression Tree
The algorithm to build an expression tree from a postfix expression is as follows:
- Read a symbol from the postfix expression (left to right).
- If the symbol is an operand, create a tree node for it and push it onto a stack.
- If the symbol is an operator, pop two trees from the stack, create a new tree node with the operator as the root, make the first popped node the right subtree and the second popped node the left subtree, and push this new tree onto the stack.
The stack is implemented using templates so it can hold TreeNode objects.
Example: Building the tree for postfix expression a b + c d e + * *.
- Step 1: Read
a(operand) β create node, push onto stack. - Step 2: Read
b(operand) β create node, push onto stack. Stack now hasa,b. - Step 3: Read
+(operator) β popaandb, create node+with left childaand right childb, push+node. Stack now has+. - Step 4: Read
c(operand) β create node, push onto stack. - Step 5: Read
d(operand) β create node, push onto stack. - Step 6: Read
e(operand) β create node, push onto stack. Stack now has+,c,d,e. - Step 7: Read
+(operator) β popdande, create node+with left childdand right childe, push+node. Stack now has+,c,+. - Step 8: Read
*(operator) β popcand+(the one withdande), create node*with left childcand right child+, push*node. Stack now has+,*. - Step 9: Read
*(operator) β pop the*node and the+node (the one withaandb), create node*with left child+and right child*, push final tree onto stack.
The final tree's inorder traversal (without parentheses) gives: a + b * c * d + e.
π‘ Why this matters: This algorithm shows how compilers and interpreters can parse mathematical expressions from their postfix form into a tree structure for evaluation or code generation.
Huffman Encoding
Huffman Encoding is a data compression technique that uses a binary tree to assign variable-length codes to characters, where more frequent characters get shorter codes. Data compression is crucial in computer networks to reduce transmission timeβeither by increasing the data rate of the media or by sending less data without losing information.
Example: The phrase "traversing threaded binary trees" (33 characters including spaces and newline) normally requires 33 Γ 8 = 264 bits using ASCII. Huffman Encoding can represent it in only 116 bits, a saving of about 40%.
The Huffman algorithm:
- List all characters used (including space and newline) along with their frequency in the message.
- Consider each (character, frequency) pair as a node (these are the leaf nodes).
- Pick the two nodes with the lowest frequency. In case of a tie, pick randomly.
- Create a new parent node with these two as children, and assign it the sum of their frequencies.
- Continue combining the two nodes of lowest frequency until only one node (the root) remains.
Example frequencies for the phrase:
- NL (newline): 1
- SP (space): 3
- a: 3
- b: 1
- d: 2
- e: 5
- g: 1
- h: 1
- i: 2
- n: 2
- r: 5
- s: 2
- t: 3
- v: 1
- y: 1
Building the tree: Nodes with frequency 1 (v, y, g, h, NL, b) are combined first into parent nodes of frequency 2. These are then combined with other low-frequency nodes (d, i, n, s) to form larger subtrees. Nodes a and t (frequency 3 each) combine into a parent of frequency 6. The SP node (frequency 3) combines with the v-y subtree (frequency 2) to form a parent of frequency 5. Finally, all subtrees combine into a root node with frequency 33.
π Definition β Huffman Encoding: A method for compressing data by assigning variable-length binary codes to characters based on their frequency, where more frequent characters receive shorter codes, using a binary tree to generate the codes.
π‘ Why this matters: Huffman Encoding is used in JPEG image compression, modem data compression, and file compression utilities like WinZip. It demonstrates how binary trees can solve real-world efficiency problems.
β Key Takeaways
Expression trees encode mathematical expressions with operators as inner nodes and operands as leaves; traversing them in inorder (with parenthesis), preorder, or postorder yields infix, prefix, or postfix notation. The algorithm to build an expression tree from a postfix expression uses a stack: operands are pushed as nodes, and when an operator is encountered, two nodes are popped to form a new subtree with the operator as root. Huffman Encoding compresses data by using a binary tree where more frequent characters get shorter codes, significantly reducing transmission size. The Huffman tree is built by repeatedly combining the two lowest-frequency nodes into a parent node until a single root remains. This technique achieves about 40% compression for typical text and is foundational to modern compression standards.
π§ Quick Revision Questions
- In an expression tree, what type of data is stored in inner nodes versus leaf nodes?
- How does the inorder traversal routine with parenthesis ensure correct operator precedence?
- What are the steps of the algorithm to convert a postfix expression into an expression tree?
- What is the primary goal of Huffman Encoding, and how does it achieve data compression?
- In the Huffman algorithm, how is a parent node created from two child nodes, and what value does it store?
π Lecture 26 β Hoffman Encoding and Mathematical Properties of Binary Trees
π Overview: This lecture continues the discussion on Huffman encoding for data compression, demonstrating the complete process of building a Huffman tree from character frequencies and generating variable-length codes. It also introduces important mathematical properties of binary trees, particularly the relationship between internal and external nodes.
ποΈ Topics Covered
The lecture covers the complete Huffman encoding process including frequency counting, building a binary tree from bottom up using frequency nodes, assigning 0/1 to tree branches, generating variable-length codes, and compressing the message. It also discusses how the receiver decodes the message using the same tree, the use of priority queues in the algorithm, and modem compression techniques. Finally, it introduces mathematical properties of binary trees concerning internal and external nodes.
π Lecture Summary
Hoffman Encoding
In the previous lecture, we began discussing Huffman encoding for data compression. Huffman encoding is a method for compressing standard text documents by using a binary tree to develop codes of varying lengths for the letters used in the original message. It is also part of the JPEG image compression scheme, introduced by David Huffman in 1952.
The example uses the 32-character phrase: "traversing threaded binary trees". If sent using standard 8-bit ASCII codes, this would require 8 Γ 32 = 256 bits. However, the Huffman algorithm can reduce the message size to 116 bits.
The steps involved in Huffman encoding are:
- List all letters used (including space character) along with their frequency in the message
- Consider each (character, frequency) pair as a node (these become leaf nodes)
- Pick two nodes with the lowest frequency (if there is a tie, pick randomly among those with equal frequencies)
- Make a new node out of these two, making the two nodes its children
- Assign this new node the sum of the frequencies of its children
- Continue combining the two nodes of lowest frequency until only one node (the root) remains
In the example, the character frequencies from the phrase "traversing threaded binary trees" were counted (including NL for newline and SP for space). Characters with frequency 1 include NL, b, g, h, v, and y. Characters with frequency 2 include d, i, n, and s. Characters with frequency 3 include a, t, and SP. Characters with frequency 5 include e and r.
The process begins by joining nodes with the lowest frequency (1): v and y are joined, creating a parent node of frequency 2. Then g and h are joined (frequency 2), and NL and b are joined (frequency 2). Next, nodes with frequency 2 are joined: d and i create a node of frequency 4, and n and s create another node of frequency 4. Then a and t are joined to create a node of frequency 6. The process continues upward: SP joins with the parent of v and y (frequency 5), r joins with that node (frequency 10), e joins with a node of frequency 4 (frequency 9), and two nodes of frequency 4 combine to make frequency 8. Finally, nodes of frequency 6 and 8 combine to make 14, nodes of frequency 9 and 10 combine to make 19, and the root combines 14 and 19 to make frequency 33.
To generate codes, we start at the root and assign 0 to the left branch and 1 to the right branch, repeating this process down the left and right subtrees. To get the code for a character, we traverse the tree from the root to the character leaf node and read off the 0 and 1 along the path.
For example, to reach letter d from the root: go through node 14 (value 0), then to node 8 (value 1), then to node 4 on the left (value 0), then finally to d (value 0). Thus, the code for letter d is 0100. Similarly, the code for i is 0101.
π Definition β Huffman Encoding: A data compression method that uses a binary tree to create variable-length codes for characters, where more frequent characters receive shorter codes and less frequent characters receive longer codes.
π Example: For the phrase "traversing threaded binary trees":
- Letter t has code 001 (3 bits)
- Letter r has code 110 (3 bits)
- Letter a has code 000 (3 bits)
- Letter v has code 11100 (5 bits)
- Letter e has code 101 (3 bits)
The encoded message becomes: 00111000011100101110011101010110100101111001100111101010000100101010011111000101010110000110111011111001110101101011110000
π‘ Why this matters: The Huffman encoded message requires only 120 bits compared to 264 bits using ASCII encoding - a reduction of 54%. This means data transmission time is approximately halved.
Decoding Process
The receiver needs to know the codes used for each letter. The sender sends the tree structure to the receiver. When the receiver gets the encoded message, it starts at the root and processes each bit: if the bit is 0, go left; if the bit is 1, go right. Upon reaching a leaf node (where both left and right links are NULL), the receiver knows that character has been received. The receiver then returns to the root and continues with the next bit to find the next character, repeating the process until the entire message is decoded.
Priority Queue in Huffman Encoding
Instead of randomly choosing nodes with equal frequencies, a priority queue can be used. Characters are placed in the queue according to their frequencies, with the lowest frequency at the front. Two nodes are removed from the queue, joined to create a parent node with the sum of their frequencies, and this new node is inserted back into the queue according to its frequency. This process continues until the queue is empty, and the last remaining node is the root.
Mathematical Properties of Binary Trees
A binary tree of N internal nodes has N + 1 external nodes. Internal nodes include all nodes with data (including leaf nodes). External nodes are NULL pointers - positions where nodes could potentially exist but currently do not. In the figure shown, there are 9 internal nodes (A, B, C, D, E, F, G plus two others) and 10 external nodes (represented by squares), confirming the property that external nodes = internal nodes + 1.
π Definition β External node: A NULL pointer position in a binary tree where a node could potentially exist. These are not actual data nodes but represent the children of leaf nodes that are NULL.
π Formula: For a binary tree with N internal nodes, the number of external nodes = N + 1
β Key Takeaways
The most critical concepts to remember are: (1) Huffman encoding creates variable-length codes where more frequent characters get shorter codes, achieving data compression; (2) the tree is built bottom-up by repeatedly joining the two nodes with lowest frequencies until one root remains; (3) codes are generated by assigning 0 to left branches and 1 to right branches, then reading the path from root to leaf; (4) the encoded message requires significantly fewer bits than ASCII encoding (54% reduction in the example); and (5) in any binary tree, the number of external nodes always equals the number of internal nodes plus one.
π§ Quick Revision Questions
- What are the six steps involved in building a Huffman encoding tree from a text message?
- How do you generate the code for a specific character once the Huffman tree is built with 0/1 branch assignments?
- Why do letters with higher frequency get shorter codes in Huffman encoding?
- How does the receiver decode a Huffman-encoded message without knowing the code table in advance?
- State the mathematical property relating internal nodes and external nodes in a binary tree.
π Lecture 27 β Properties of Binary Tree
π Overview: This lecture covers two main topics: the mathematical properties of binary trees regarding internal and external nodes/links, and the concept of threaded binary trees. The threaded tree modification is important because it allows stack-free inorder traversal, eliminating the overhead of recursive calls and explicit stack operations during repeated traversals.
ποΈ Topics Covered
The lecture begins by discussing the second property of binary trees concerning link counts (2N total links, N-1 internal links, N+1 external links). It then introduces threaded binary trees as a solution to the costly overhead of stack operations during recursive traversals. The process of adding threads during insertion is explained with code and diagrams, followed by how to find the inorder successor in a threaded tree. Finally, a non-recursive, stack-free inorder traversal routine is presented.
π Lecture Summary
Properties of Binary Tree
A binary tree with N internal nodes has 2N links total. Of these, N-1 links connect internal nodes to other internal nodes, and N+1 links connect internal nodes to external nodes (NULL pointers). This is a mathematical property that can be proven.
π Definition β Internal Node: A node that has at least one child. Leaf nodes are also considered internal nodes in this context. π Definition β External Node: A NULL pointer or square node representing an absent child. π Formula: 2N = (N-1) + (N+1) β In a tree with N internal nodes, total links are 2N, internal-to-internal links are N-1, and links to external nodes are N+1. π Example: In a tree with 9 internal nodes (marked A-I), there are 8 internal links and 10 external links. Total = 18 = 2 Γ 9.
π‘ Why this matters: These properties help understand the number of NULL pointers available in a binary tree, which is the foundation for threaded binary trees.
Threaded Binary Trees
In many applications, binary tree traversals are carried out repeatedly. The overhead of stack operations during recursive calls can be costly. The same would be true if we use a non-recursive but stack-driven traversal procedure. So, we modify the tree data structure to speed up inorder traversal and make it "stack-free."
π Definition β Threaded Binary Tree: A binary tree where NULL pointers are replaced with pointers to the inorder successor (or predecessor) of a node.
Most pointer fields in a standard binary tree representation are NULL. Since every node (except the root) is pointed to, there are only N-1 non-NULL pointers out of a possible 2N (for an N node tree), so that N+1 pointers are NULL.
In a threaded tree, these NULL pointers are replaced with pointers to the inorder successor (or predecessor) as appropriate. We need to know whenever formerly NULL pointers have been replaced by non-NULL pointers to successor/predecessor nodes, since otherwise there's no way to distinguish those pointers from the customary pointers to children.
π Definition β LTH / RTH flags: Boolean flags in each node that indicate whether the left/right pointer is a thread (pointing to predecessor/successor) or a child pointer. If the flag is "child," the pointer is a normal tree link. If the flag is "thread," the pointer is a thread to the inorder predecessor or successor.
Adding Threads During Insert
When inserting a new node into a threaded binary tree, we must set the threads correctly.
Consider inserting node 16 into a tree containing nodes 14, 15, 18, and 20. The inorder sequence would be: 14, 15, 16, 18, 20.
The code to insert node 16 as the left child of node 18 (pointed to by p) is:
1. t->L = p->L; // copy the thread (predecessor of 18 was 15, now becomes predecessor of 16)
2. t->LTH = thread; // mark left pointer as thread
3. t->R = p; // *p (18) is successor of *t (16)
4. t->RTH = thread; // mark right pointer as thread
5. p->L = t; // attach the new leaf as left child of 18
6. p->LTH = child; // mark left pointer as child (not a thread)
π Example: Before insertion, node 18's left pointer was a thread pointing to node 15 (its predecessor). After insertion, node 16's left pointer is set to the thread (now pointing to 15), and node 16's right pointer is set to point to node 18 (its successor). Node 18's left pointer is then changed to point to node 16 as its actual left child.
Where is Inorder Successor?
For any node in a binary tree, the inorder successor is the leftmost node in the right subtree of that node.
To find the inorder successor:
- If the node has a right thread (RTH == thread), the successor is simply the node pointed to by the right pointer.
- Otherwise (RTH == child), go to the right child, then follow left child pointers until you reach a node whose left pointer is a thread (LTH == thread).
π Example: For node 4, its inorder successor is 5. We go to node 9 (right child of 4), then to node 7, then to node 5 (the leftmost node in the right subtree of 4, and also the leaf node whose left pointer is a thread).
Inorder Traversal
Using the nextInorder routine, we can implement a non-recursive, stack-free inorder traversal.
TreeNode* nextInorder(TreeNode* p) {
if (p->RTH == thread)
return (p->R);
else {
p = p->R;
while (p->LTH == child)
p = p->L;
return p;
}
}
If we call nextInorder with the root of the tree, we have some difficulty. The code won't work properly because we need to find the first node to print (the leftmost node in the tree). The lecture indicates a small change is needed to handle the root case.
π Example: Starting at root node 14, if we call nextInorder(14), the right pointer is not a thread (it points to child 15), so we go to the else part. We set p = 15 (right child), then try to follow left pointers, arriving at the wrong node. We need to start at the leftmost node first.
β Key Takeaways
The critical student must remember that a binary tree with N internal nodes has 2N total links, N-1 internal-to-internal links, and N+1 links to external nodes (NULL pointers). Threaded binary trees repurpose these NULL pointers to point to inorder successors and predecessors, enabling stack-free traversal. Two flags (LTH and RTH) are essential to distinguish threads from child pointers. The inorder successor of any node is found by going to its right child and then following left children until a thread is encountered. The nextInorder routine can then be called repeatedly in a loop for complete stack-free inorder traversal, though root handling requires special attention.
π§ Quick Revision Questions
- What is the relationship between internal nodes and external links in a binary tree?
- Why is a "stack-free" traversal desirable, and how do threaded binary trees achieve it?
- What is the purpose of the LTH and RTH flags in a threaded binary tree node?
- How do you find the inorder successor of a node in a threaded binary tree?
- What potential problem arises when calling
nextInorderwith the root node of a threaded tree?
π Lecture 28 β Inorder Traversal in Threaded Trees & Complete Binary Tree
π Overview: This lecture continues the discussion of inorder traversal in threaded binary trees, presenting a solution to the problem of incorrect traversal using a dummy node trick. It then introduces the concept of complete binary trees, their properties, and how they can be efficiently stored in an array without pointers.
ποΈ Topics Covered
The lecture covers two main topics: first, fixing the nextInorder routine for threaded binary trees by adding a dummy node to ensure correct traversal direction, along with the fastInorder routine that uses this trick. Second, it introduces the complete binary tree definition, its height and node count properties, and the array storage scheme using the 2i and 2i+1 indexing rule, with examples showing level-order traversal correspondence.
π Lecture Summary
Inorder traversal in threaded trees
The nextInorder routine is designed to find the inorder successor of a node in a threaded binary tree. It first checks if the right pointer of the node is a thread β if so, it returns the right pointer as it points to the inorder successor. Otherwise, it moves to the right child and then follows left children until it finds a node whose left pointer is a thread.
TreeNode* nextInorder(TreeNode* p){
if(p->RTH == thread) return(p->R);
else {
p = p->R;
while(p->LTH == child)
p = p->L;
return p;
}
}
When this routine is applied to the sample tree by passing the root node (14), it does not work properly. The pointer moves to node 15 (the right child of root) instead of going to the leftmost node (3) first. This is because the routine checks the right pointer first, which for the root is a link, causing movement in the wrong direction for inorder traversal.
π‘ Why this matters: The routine fails because it cannot distinguish between being at the root node (where left traversal is needed first) versus being at an internal node. A programming trick is needed to fix this.
To fix this problem, a dummy node is inserted into the tree. The dummy node has:
- Its left pointer pointing to the root node of the tree
- Its right pointer pointing to itself (the dummy node)
- The left thread of the leftmost node points to the dummy node
- The right thread of the rightmost node points to the dummy node
void fastInorder(TreeNode* p)
{
while((p = nextInorder(p)) != dummy)
cout << p->getInfo();
}
The fastInorder routine is called with the dummy node as argument: fastInorder(dummy). Starting from the dummy node, the routine correctly traverses to the leftmost node first. The pointer moves from dummy β node 14 β node 4 β node 3 (where left pointer is a thread), returning node 3 as the first inorder element. Subsequent calls follow threads and links correctly to produce the complete inorder traversal without recursion.
π Definition β Dummy node: An extra node inserted into a threaded binary tree whose left pointer points to the root, right pointer points to itself, and left/right threads of extreme nodes point to it, enabling correct inorder traversal from a known starting point.
π Formula: fastInorder(dummy) β starts traversal at the dummy node, which leads to the leftmost node first, producing correct inorder sequence.
π Example: From the sample tree with dummy node, calling nextInorder(dummy) moves p from dummy β 14 β 4 β 3 (thread found, return 3). Next call with p=3 follows the right thread to node 4. Next call with p=4 follows the right link to node 9, then left to node 5, etc. The full inorder sequence produced is: 3, 4, 5, 7, 9, 14, 15, 16, 18, 20.
Complete Binary Tree
A complete binary tree is defined by two properties:
- A tree that is completely filled, with the possible exception of the bottom level
- The bottom level is filled from left to right
In a complete binary tree, all nodes except those at the bottom level have both left and right children. At the bottom level, leaf nodes appear from left to right, but the level may not be completely filled.
π Definition β Complete binary tree: A binary tree where all levels are completely filled except possibly the last level, which is filled from left to right.
Properties of complete binary trees:
- A complete binary tree of height h has between 2^h to 2^(h+1) β 1 nodes
- The height of such a tree is floor(logβN) where N is the number of nodes
- Because the tree is so regular, it can be stored in an array β no pointers are necessary
π Formula β Array indexing for complete binary tree: For any array element at position i, the left child is at 2i, the right child is at (2i + 1), and the parent is at floor(i/2).
π‘ Why this matters: Array storage starts at position 1 (not 0) because if position were 0, then 2i would also be 0, making it impossible to distinguish the root from its children. Position 0 is ignored.
π Example: Consider the tree nodes A(1), B(2), C(3), D(4), E(5), F(6), G(7), H(8), I(9), J(10).
- Root A at position 1 β left child B at 2(1)=2, right child C at 2(1)+1=3 β
- Node B at position 2 β left child D at 2(2)=4, right child E at 2(2)+1=5 β
- Node C at position 3 β left child F at 2(3)=6, right child G at 2(3)+1=7 β
- Node D at position 4 β left child H at 2(4)=8, right child I at 2(4)+1=9 β
- Node E at position 5 β left child J at 2(5)=10, right child at 2(5)+1=11 (empty) β
- Node J at position 10 β parent at floor(10/2)=5 (node E) β
- Node I at position 9 β parent at floor(9/2)=4 (node D) β
The level-order traversal of the tree directly gives the indices for array storage. The order is: A(1), B(2), C(3), D(4), E(5), F(6), G(7), H(8), I(9), J(10). These numbers correspond exactly to the array indices when using the 2i scheme.
Comparison: Pointers vs. Arrays for tree storage:
- Arrays are fast and efficient, supported directly by the language compiler
- Pointers can cause problems with paging (virtual memory), where loading/unloading code segments slows execution
- Use arrays whenever they can fulfill requirements; use pointers only where beneficial (e.g., AVL tree balancing where moving large data in arrays would be inefficient)
β Key Takeaways
The inorder traversal problem in threaded trees is solved by inserting a dummy node that points to the root, allowing the nextInorder routine to start correctly and traverse to the leftmost node first. The fastInorder routine using this trick produces correct inorder traversal without recursion or stack, making it faster than recursive traversal. A complete binary tree is a regular structure where all levels are filled except possibly the bottom level, which is filled left to right. Such trees can be stored efficiently in an array using the 2i and 2i+1 indexing scheme, where position 1 holds the root. This array storage eliminates the need for pointers, leveraging the compiler's native support for arrays to improve performance and avoid paging issues. The level-order traversal directly corresponds to the array index positions, making storage straightforward.
π§ Quick Revision Questions
-
What is the main problem with the original nextInorder routine when called with the root node, and how does the dummy node trick fix it?
-
In the fastInorder routine, why is the function called with the dummy node as argument instead of the root? Trace the first two steps of traversal starting from the dummy node.
-
State the definition of a complete binary tree and list its two key properties regarding height and number of nodes.
-
Given a complete binary tree stored in an array using the 2i and 2i+1 scheme starting at position 1, find the left child, right child, and parent of the node at position 6. Show the calculations.
-
Why does array storage for complete binary trees start at position 1 instead of position 0? What would happen if we used position 0 as the starting point?
π Lecture 29 β Data Structures
π Overview: This lecture introduces the heap data structure, a complete binary tree that follows the heap order property. It explains how heaps are stored efficiently using arrays, distinguishes between min heaps and max heaps, and demonstrates the insertion algorithm for maintaining heap order after adding new elements. Understanding heaps is essential for implementing priority queues and efficient sorting algorithms.
ποΈ Topics Covered
The lecture covers the storage of complete binary trees in arrays using the 2i and 2i+1 scheme, explains why arrays are unsuitable for incomplete trees due to memory wastage, defines the heap data structure with its heap order property, distinguishes between min heaps and max heaps, and provides a detailed step-by-step demonstration of inserting a new value into a min heap while preserving the heap property through upward swapping.
π Lecture Summary
Complete Binary Tree
In the previous lecture, we discussed storing a complete binary tree in an array using the 2i and 2i+1 scheme. This scheme allows a programmer to navigate from a parent to its children and from children back to the parent. Arrays are efficient built-in data structures in many languages.
π‘ Why this matters: Arrays are only efficient for complete binary trees because they can be stored contiguously without wasting space. For incomplete trees, the array would have "holes" (empty positions), leading to memory wastage.
The 2i scheme stores the left child of node at index i at position 2i, and the right child at position 2i+1. When a tree is not complete, we would need to treat it as complete by imagining missing nodes exist, then remove the data at those positions afterward β leaving empty holes in the array.
π Definition β Complete Binary Tree: A binary tree in which all levels are completely filled except possibly the last level, which is filled from left to right.
Heap
Heap is a data structure of significant use and benefit. It is primarily used in priority queues. Recall the bank simulation example where events were placed in a priority queue that does not follow FIFO rules β elements are retrieved based on their priority rather than insertion order.
π‘ Why this matters: Implementing priority queues with arrays requires sorting and shifting elements, which is expensive in terms of time. The heap provides a much more efficient alternative for priority queue operations.
The heap data structure is defined as:
"A heap is a complete binary tree that conforms to the heap order."
The heap order property states that in a min heap, for every node X, the key in the parent is smaller than or equal to the key in X. In other words, the parent node has a key smaller than or equal to both of its children nodes.
This differs from a binary search tree (BST), where the left child is smaller and the right child is larger than the parent. In a heap, both children must be greater than the parent, but there is no ordering relationship between the left and right children themselves.
π Definition β Heap Order: For a min heap, the value of every parent node is less than or equal to the values of its children nodes.
π Example: In the min heap shown, node 13 has children 21 and 16 β both are greater than 13. Node 21 has children 24 and 31 β both greater than 21. Node 16 has children 19 and 68 β both greater than 16. This pattern holds for every node.
In a min heap, the root node contains the smallest value in the entire tree. An inorder traversal of a heap does NOT produce sorted output, confirming it is not a binary search tree.
Max Heap
In a max heap, each node has a value greater than or equal to the values of its left and right children. Consequently, the root node contains the largest value, and values become smaller at lower levels.
By repeatedly removing the root from a min heap and allowing the remaining values to adjust themselves into a heap again, we get the smallest value each time. Continuing this process yields data in ascending order β this is the basis of heap sort.
Similarly, a max heap can be used for priority queues where the highest priority element is always at the top.
π Definition β Max Heap: A complete binary tree where every parent node has a value greater than or equal to the values of its children.
Insertion in a Heap
When inserting a new value into a heap, we need to:
- Place the new node at a position that maintains the complete binary tree property (the next available position in level-order)
- Then adjust upward to preserve the heap order property
π Example β Inserting value 14 into an existing min heap:
Step 1: The existing heap has values stored in an array at positions 1-10: 13, 21, 16, 24, 31, 19, 68, 65, 26, 32. Position 11 is empty.
Step 2: Add node 14 at position 11 (right child of node 31 at position 5). The tree remains complete binary.
Step 3: Check heap order. Compare 14 with its parent (31 at position 5). Since 14 < 31, the heap property is violated. Exchange them β 14 goes to position 5, 31 goes to position 11.
Step 4: Now compare 14 (at position 5) with its new parent (21 at position 2). Since 14 < 21, exchange them β 14 goes to position 2, 21 goes to position 5.
Step 5: Compare 14 (at position 2) with its new parent (13 at position 1). Since 13 < 14, the heap property is preserved. The insertion is complete.
The final array is: 13, 14, 16, 24, 21, 19, 68, 65, 26, 32, 31.
To find the parent of any node at position i, use the formula: floor(i / 2).
π Example β Inserting value 15 into the new heap:
Step 1: The new heap has values at positions 1-11. Position 12 is empty. Add node 15 as the left child of node 19 (at position 6), so position 12.
Step 2: Compare 15 with parent (19 at position 6). Since 15 < 19, exchange β 15 goes to position 6, 19 goes to position 12.
Step 3: Compare 15 (at position 6) with new parent (16 at position 3). Since 15 < 16, exchange β 15 goes to position 3, 16 goes to position 6.
Step 4: Compare 15 (at position 3) with new parent (13 at position 1). Since 13 < 15, the heap property is preserved. Done.
The final array is: 13, 14, 15, 24, 21, 16, 68, 65, 26, 32, 31, 19.
π Formula: parent(i) = floor(i / 2) β used to find the parent's position when traversing upward during insertion.
π‘ Why this matters: The upward-swapping algorithm efficiently maintains heap order without requiring full sorting. Each comparison moves the new value one level up, so the worst-case time complexity is O(log n), where n is the number of nodes in the heap.
β Key Takeaways
The heap is a complete binary tree stored efficiently in an array using the 2i and 2i+1 scheme, which eliminates memory wastage and pointer overhead. The heap order property distinguishes min heaps (parent smaller than children) from max heaps (parent larger than children). Insertion into a heap involves first placing the new element at the next available position to maintain the complete tree property, then repeatedly swapping it with its parent until the heap order is restored β a process called "percolating up" or "bubbling up." The parent of any node at position i is found using floor(i/2), making upward traversal straightforward in the array. The heap data structure is fundamental for implementing priority queues and heap sort algorithms.
π§ Quick Revision Questions
- What are the two essential properties that define a heap data structure?
- Why is an array inefficient for storing an incomplete binary tree?
- In a min heap with values 10, 25, 15, 30, 20, 40, 35, what is the value at the root and which values could be the children of 25?
- If you insert the value 8 into the min heap 13, 21, 16, 24, 31, 19, 68, trace the steps showing which values are swapped and what the final heap looks like in the array.
- Using the formula floor(i/2), determine the parent positions of nodes at indices 5, 9, and 12 in an array-based heap.