Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Trees
Tree
Tree Traversals
Binary Trees
Tree (T)
Set of nodes storing elements in a parent–child
relationship.
v is a child node of u.
Cannot be empty.
1–
2 / 3 +
4X 5+ 6X 76
8+ 9 3 10 – 11 2 12 3 13 –
16 3 1 17 20 9 21 5 26 7 27 4
Siblings
Nodes that are children of the same parent.
External node
Has no children.
Leaf node.
Internal node
Has one or more children.
D. S. R. Murthy DSA-4 Basic Data Structures (Contd…) 3
1–
2 / 3 +
4X 5+ 6X 76
8+ 9 3 10 – 11 2 12 3 13 –
16 3 1 17 20 9 21 5 26 7 27 4
Ancestor of a node
Node itself
or
an ancestor of the parent of the node.
Descendent of a node
If u is an ancestor of v, then v is a descendent of a
node u.
Child node.
Ordered tree
Linear ordering defined for the children of each
node.
D. S. R. Murthy DSA-4 Basic Data Structures (Contd…) 4
Binary tree
Ordered tree in which every node has at most two
children.
Left Subtree
Subtree rooted at a left child of an internal node v.
Right Subtree
Subtree rooted at a right child of an internal node v.
D. S. R. Murthy DSA-4 Basic Data Structures (Contd…) 5
Arithmetic expression
((((3 + 1) X 3) / ((9 – 5) + 2)) – ((3 X (7 – 4)) + 6))
1 –
2 / 3 +
4 X 5 + 6 X 7 6
8 + 9 3 10 – 11 2 12 3 13 –
16 3 1 17 20 9 21 5 26 7 27 4
External nodes
associated with variables or constants.
Internal nodes
associated with the operators +, –, X, /.
1 –
2 / 3 +
4 X 5 + 6 X 7 6
8 + 9 3 10 – 11 2 12 3 13 –
16 3 1 17 20 9 21 5 26 7 27 4
Node Value
7 6
9 3
11 2
12 3
16 3
17 1
20 9
21 5
26 7
27 4
D. S. R. Murthy DSA-4 Basic Data Structures (Contd…) 7
1 –
2 / 3 +
4 X 5 + 6 X 7 6
8 + 9 3 10 – 11 2 12 3 13 –
16 3 1 17 20 9 21 5 26 7 27 4
Node Value
13 (–) 3
10 (–) 4
8 (+) 4
6 (X) 9
5 (+) 6
4 (X) 12
3 (+) 15
2 (/) 2
1 (–) –13
D. S. R. Murthy DSA-4 Basic Data Structures (Contd…) 8
Height of a of a tree T
Maximum depth of an external node of T.
Tree Traversals
Systematic way of accessing, or visiting, all the
nodes of T.
Example:
1
2 3 4 7 11 14
5 6 8 9 10 12 13
D. S. R. Murthy DSA-4 Basic Data Structures (Contd…) 10
Example:
14
1 2 5 9 12 13
3 4 6 7 8 10 11
D. S. R. Murthy DSA-4 Basic Data Structures (Contd…) 11
Example:
1 –
2 / 13 +
3 X 8 + 14 X 19 6
4 + 7 3 9 – 12 2 15 3 16 –
5 3 1 6 10 9 11 5 17 7 18 4
Example:
19 –
11 / 18 +
5 X 10 + 16 X 17 6
3 + 4 3 8 – 9 2 12 3 15 –
1 3 1 2 6 9 7 5 13 7 14 4
D. S. R. Murthy DSA-4 Basic Data Structures (Contd…) 12
Example:
12 –
6 / 18 +
4 X 10 + 14 X 19 6
2 + 5 3 8 – 11 2 13 3 16 –
1 3 1 3 7 9 9 5 15 7 17 4
Each traversal
Visits the nodes of a tree in a certain order.
Example:
1 – 57
35
2 / 34 36 + 56
18 52
3 X 17 19 + 33 37 X 51 53 6 55
13 29 41 54
4 + 12 14 3 16 20 – 28 30 2 32 38 3 40 42 – 50
8 15 24 31 39 46
5 3 7 9 1 11 21 9 23 25 5 27 43 7 45 47 4 49
6 10 22 26 44 48
1 –
2 / 3 +
4 X 5 + 6 X 7 6
8 + 9 3 10 – 11 2 12 3 13 –
16 3 1 17 20 9 21 5 26 7 27 4
Fast
Simple
Very space inefficient,
if the height of the tree is large.
D. S. R. Murthy DSA-4 Basic Data Structures (Contd…) 15
Each node v of T
represented by an object with
references to the element stored at v.
Parent
Left Right
Element
If v is the root of T,
then the reference to the parent node is null.
If v is an external node,
then the references to the children of v are null.