Professional Documents
Culture Documents
04/01/17
04/01/17
04/01/17
04/01/17
Find the parent of c, the children of g, the siblings of h,, all internal
vertices, and all leaves. What is the subtree rooted at g?
The parent of c is b.
The children of g are h, i, and j .
The siblings of h are i and j .
The internal vertices are a, b, c, g, h, and j .
The leaves are d, e, f , i, k, l, and m.
The subtree rooted at g is shown in right Figure.
04/01/17
04/01/17
Are the rooted trees in Figure full m-ary trees for
some positive integer m?
04/01/17
We can change any unrooted tree into a rooted tree by
choosing any vertex as the root.
04/01/17
Find the level of each vertex in the rooted tree shown in Figure. What is
the height of this tree?
04/01/17
04/01/17
04/01/17
1. How many edges does a tree with 10,000
vertices have?
04/01/17
Applications of
Trees
Binary Search Trees
04/01/17
Binary Search Trees
If a< v:
If v has a left child then v = left child of v(move to the
left)
Else add a new left child to v with this item as its key.
Stop.
04/01/17
Binary Search Trees
If a> v:
Example :
5 9 8 1 2 4 6 10
04/01/17
04/01/17
Exercises :
04/01/17
Answer 1:
04/01/17
What are the codes for a, e, i, k, o, p and u if the coding scheme is
represented by this tree?
0 1
0 1 1
0
0 1
i
0 1 1
a e
o
k 0 1
04/01/17 p u
Class work
Given the coding scheme a: 01, b: 001, e : 1, r :
0000, s: 0100, t: 011, x: 01010. Find the word
represented by
1. 01000110010111
2. 01110100011
3. 000110010000
4. 01100101010
04/01/17
Tree Traversal
Tree traversal:
04/01/17
Traversal Algorithm
Procedures for systematically visiting every vertex of an
ordered rooted tree are called Traversal algorithm.
Preorder Traversal
Inorder Traversal
Postorder Traversal
04/01/17
04/01/17
04/01/17
04/01/17
04/01/17
04/01/17
04/01/17
04/01/17
04/01/17
04/01/17
Preorder Traversal, Inorder Traversal
and Postorder Traversal
a
d
b
c
e g
i
f h
j l m
n o p
04/01/17
Answer:
Preorder: a b e j k n o p f c d g l m h i
Inorder: j e n k o p b f a c l g m d h i
Postorder: j n o p k e f b c l m g h I d a
04/01/17
04/01/17
Infix, Prefix, and Postfix notation
((x + y) 2 ) + ((x 4) / 3) ?
((x + y) 2 ) + ((x 4) / 3) ?
04/01/17
Example 1: What is the ordered rooted tree that represents the
expression
((x + y) 2 ) + ((x 4) / 3) ?
04/01/17
Example 2: What is the prefix form for
((x + y) 2 ) + ((x 4) / 3) ?
Solution: We obtain the prefix form for this expression by
traversing the binary tree that represents it, shown in
previous Figure. This produces + t + x y 2 / - x 4 3 .
((x + y) 2 ) + ((x 4) / 3) ?
Solution: We obtain the prefix form for this expression by
traversing the binary tree that represents it, shown in
previous Figure. This produces the postfix expression:x y
+ 2 t x 4 - 3 / +.
04/01/17
What is the value of the prefix expression
+ - *2 3 5 / 2 3 4?
- * 2 / 8 4 3
* + 3 + 3 3 + 3 3 3
04/01/17
04/01/17
Exercises:
Construct the ordered rooted tree whose
preorder traversal is a, b, f, c, g, h, i, d, e, j, k, l,
where a has four children, c has three children,
j has two children, b and e have one child
each, and all other vertices are leaves.
04/01/17