0% found this document useful (0 votes)
3 views6 pages

Trees 1

The document discusses various types of binary trees, including expression trees, threaded binary trees, and heaps. It provides examples of constructing expression trees from infix and postfix expressions, as well as rules for threaded binary trees. Additionally, it includes a function for creating a max heap and an algorithm to trace its execution with specific array elements.

Uploaded by

akul.kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Trees 1

The document discusses various types of binary trees, including expression trees, threaded binary trees, and heaps. It provides examples of constructing expression trees from infix and postfix expressions, as well as rules for threaded binary trees. Additionally, it includes a function for creating a max heap and an algorithm to trace its execution with specific array elements.

Uploaded by

akul.kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Arithmetic Expression Using BT

+ 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+BCD  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

• Complete Binary Tree

 Min heap or ascending heap: Parent data is less than child


data
 Max heap or descending heap: Parent data is greater than
child data

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

You might also like