Threaded Binary Trees
Threaded Binary Trees
Binary Trees
An observation: Majority of pointers in a
binary tree are NULL
e.g. a binary tree with n nodes has n+1 NULL
pointers, and they are wasted
Based on traversal
order
Preorder threaded
In-order threaded
Post-order threaded
data
Right
struct BinaryTree {
struct BinaryTree *left;
int data;
struct BinaryTree *right;
}
LTag
data
RTag
Right
struct ThreadedBinaryTree {
struct ThreadedBinaryTree *left;
int Ltag;
int data;
int Rtag;
struct ThreadedBinaryTree *right;
}
Meaning
LTag == 0
LTag == 1
RTag == 0
RTag == 1
6
8
3
1
11
9
13
6
8
3
1
11
9
13
6
8
3
1
11
9
13
6
8
3
1
11
9
13
8/8/02
12
6
8
3
1
11
9
13
8/8/02
13
6
8
3
1
11
9
13
6
8
3
1
11
9
13
3
1
11
9
13
Output
1
3
5
6
7
8
9
11
3
1
11
9
13
Output
1
3
5
6
7
8
9
11
13
18
10