Trees 1
Trees 1
+ inorder traversal
A/B*C*D+E
infix expression
* E
preorder traversal
+**/ABCDE
* D prefix expression
postorder traversal
AB/C*D*E+
/ C
postfix expression
level order traversal
A B +*E*D/CAB
1
1
§2 Binary Trees
Expression Trees (syntax trees) +
〖 Example 〗 Given an infix expression: A
A+BCD D
Constructing an Expression Tree B C
〖 Example 〗 ( a + b ) * ( c * ( d + e ) ) = a b + c d e + * *
(from postfix expression)
+
a b *c d * +
e
T2 T2 a + b c T1d * + e T1 T1
a bT2 T2 c d + e T1
d e 2
§2 Binary Trees
Threaded Binary Trees
Rule 1: If root->llink is null, replace it with a pointer to the
inorder predecessor of Tree.
Rule 2: If root->rlink is null, replace it with a pointer to the
inorder successor of Tree.
3
4
Heap or priority queue
5
Function to create a max heap
void heapify_max(int a[], int n)
{
int i, j, k, item;
for(k=2;k≤n;k++)
{
item=a[k];
i=k;
j=i/2;
while(j!=0 && item>a[j])
{
a[i]=a[j];
i=j;
j=i/2;
}
a[i]=item;
}
}
Trace the above algorithm for the array elements: 60, 15, 28, 70, 90, 37, 78
6