Uploaded by Sa Ran

Tutorial 8

advertisement
Harring
652115051
Blanche
Name____________________________ID____________________________Group______________
Tutorial 8
Binary Tree
1. Draw a Binary Tree stored integer number having root node = 30, there are 3 nodes on the left
of the Tree and the height of this tree is 4
·
2. Give 2 example of Full Binary Tree
ag
I
* G
3. Give 2 example of complete binary tree which is not a full binary tree
⑰
⑬
↓
①
/
②
⑯
N
⑤
11 I
58
1
Harring
652115051
Blanche
Name____________________________ID____________________________Group______________
4. Binary Tree can be used to represented mathematical expression. We usually use “Infix” expression
in everyday life i.e. a+b to say “a plus b”, but there are 2 more possible formats which are “prefix and
postfix” i.e. “+ab and ab+” respectively. Use the inorder, preorder, and postorder traversals to
determine the infix, prefix, and postfix expression from the following tree
4.1
*
5
+
-
7
Infix:
/
8
1
4
5*((1 3) (X)1)
-
+
4.2
*
B
+
X
/
Z
Prefix:
-
,
E
Y
*, t, B, x,/, E,2, Y
4.3
/
-
+
-
+
q
a
u
/
t
Postfix:
q(
-
,a,n,t,t,w,),z,
-
m
z
w
,m,t,/
2
Harring
652115051
Blanche
Name____________________________ID____________________________Group______________
5. From the following tree:
X, H, E, K, G,A,B,D, N,6
Pre-order Traversal:
C, E, H,X,k,GN, B, A,D
In order Traversal :
Post order Traversal:
X, H,k,E, A,D, B,N,0,2
6. Infix to prefix expression conversion using stack (2+45^6/(7+8))
6.1 Reverse the infix expression
178 1))6154 2)
+
+
6.2 Make every ‘(‘ as ‘)’ and every ‘)’ as ‘(‘
(18 1)/615x 2)
+
+
6.3 Convert expression to postfix form
Expression
(18 1)/615x c)
(8 9)/615x 2)
Stack
+
+
+
8
+
1)/6154 2)
+
+
7)/615x 2)
9) 16151 27
+
+
+
7/615x 2)
16154 2)
+
+
51
I
2)
+
2)
+
x 2)
+
e)
+
2)
End
Comment
Intitial.
-
piee
-
i
it
6151 2)
+
15x
Empty
Output
empty
-
%
89t
17t
87 6
+
6
89 6/5
81 6/54
81 6/51
11
+
+
+
+
84 6/5412
+
81+
6/5A12+
Bepresen
promptprint
to
Sprint.
primprinitn
6.4 Reverse the expression and show the result
+2159/6
17.
+
3
Download