Unit 2 part 3
Unit 2 part 3
“threads”.
THREADED BINARY TREE
• Binary trees have a lot of wasted space: the
leaf nodes each have 2 null pointers
• We can use these pointers to help us in
inorder traversals
• We have the pointers reference the next
node in an inorder traversal; called threads
• We need to know if a pointer is an actual link
or a thread, so we keep a boolean for each
pointer
THREADED TREE TRAVERSAL
We start at the leftmost node in the tree,
print it, and follow its right thread
If we follow a thread to the right, we output
3 8
1 5 7 11
9 13
Start at leftmost node, print it
THREADED TREE TRAVERSAL
Output
6 1
3
3 8
1 5 7 11
9 13
3 8 5
1 5 7 11
9 13
Follow link to right, go to
leftmost node and print
THREADED TREE TRAVERSAL
Output
6 1
3
3 8 5
6
1 5 7 11
9 13
3 8 5
6
7
1 5 7 11
9 13
Follow link to right, go to
leftmost node and print
THREADED TREE TRAVERSAL
Output
6 1
3
3 8 5
6
7
1 5 7 11 8
9 13
3 8 5
6
7
1 5 7 11 8
9
9 13
Follow link to right, go to
leftmost node and print
THREADED TREE TRAVERSAL
Output
6 1
3
3 8 5
6
7
1 5 7 11 8
9
11
9 13
3 8 5
6
7
1 5 7 11 8
9
11
9 13 13
14
THREADED TREE MODIFICATION
6
3 8
1 5 7 11
9 13
15
THREADED TREE TRAVERSAL CODE
Node leftMost(Node n) void inOrder(Node n) {
{ Node cur = leftmost(n);
Node ans = n; while (cur != null) {
if (ans == null) { print(cur);
return null; if (cur.rightThread) {
} cur = cur.right;
while (ans.left != } else {
cur =
null) {
leftmost(cur.right);
ans = ans.left; }
} }
return ans; }
}
THREADED BINARY TREES (CONTINUED)
If ptr->left_child is null,
replace it with a pointer to the node that would be
visited before ptr in an inorder traversal
If ptr->right_child is null,
replace it with a pointer to the node that would be
visited after ptr in an inorder traversal
A Threaded Binary Tree
root A
dangling
B C
dangling D E F G
inorder traversal:
H I H, D, I, B, E, A, F, C, G
Data Structures for Threaded
BT
left_thread left_child data right_child right_thread
TRUE FALSE
root --
f f
f A f
f B f f C f
f D f t E t t F t t G t
t H t t I t
Inorder Traversal of Threaded BT
void tinorder(threaded_pointer temp)
{ while(temp!=dummy)
{ while(!temp->left_thread)
temp=temp->left
cout<<temp->data
while(temp->right_thread)
{temp=temp-> right
if(temp==dummy) return;
cout<<temp->data
}
temp=temp-> right
}
}
}
Inserting Nodes into Threaded BTs
Insert child as the right child of node parent
change parent->right_thread to FALSE
set child->left_thread and
child->right_thread to TRUE
set child->left_child to point to parent
set child->right_child to
parent->right_child
change parent->right_child to point to child
22
Examples
Insert a node D as a right child of B.
root root
A A
(1)
parent
B parent B
(3)
child child
C D C D
(2)
empty
(3)
(2) (1)
(4)
nonempty
frequency!
Yet all characters are allocated the same
amount of space
1 char = 1 byte, be it e or x
THE BASIC ALGORITHM
Scan text to be compressed and tally occurrence
of all characters.
1101111100101111001
struct node
{
char data;
int freq;
node *left, *right;
node(char data, int freq)
{
left = right = NULL;
this->data = data;
this->freq = freq;
}
};
struct compare
{
bool operator()(node* l, node* r)
{
return (l->freq > r->freq);
}
};
void HuffmanCodes(char data[], int freq[], int size)
{
struct node *left, *right, *top;
priority_queue<node*, vector<node*>, compare>
minHeap;
for (int i = 0; i < size; ++i)
minHeap.push(new node(data[i], freq[i]));
while (minHeap.size() != 1) {
left = minHeap.top();
minHeap.pop();
right = minHeap.top();
minHeap.pop();
top = new node('$', left->freq + right->freq);
top->left = left;
top->right = right;
minHeap.push(top);
}
printCodes(minHeap.top(), "");
}
void printCodes(struct node* root, string str)
{
if (!root)
return;
if (root->data != '$')
cout << root->data << ": " << str << "\n";
printCodes(root->left, str + "0");
printCodes(root->right, str + "1");
}
int main()
{char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
int freq[] = { 10, 5, 14, 14, 7, 9 };
cout<<"The Character and its Frequency:- "<<endl;
int size = sizeof(arr) / sizeof(arr[0]);
for(int i=0;i<sizeof(arr);i++)
{
cout<<"\t"<<arr[i]<<"\t"<<freq[i]<<endl;
}
cout<<"The Huffman Codes:- "<<endl;
HuffmanCodes(arr, freq, size);
return 0;
}