Sei sulla pagina 1di 15

D. S. R.

Murthy DSA-4 Basic Data Structures (Contd…) 1

Trees

Tree

Tree Traversals

Binary Trees

Data Structures for Representing Trees

Tree (T)
Set of nodes storing elements in a parent–child
relationship.

Has a special node (root) r.

Stores elements hierarchically.

Each node v (other than root node r) has a parent


node u.

v is a child node of u.

Each node v has zero or more children nodes.

Cannot be empty.

Must have at least one node, the root.


D. S. R. Murthy DSA-4 Basic Data Structures (Contd…) 2

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

Subtree of Tree T at a node v


Tree consisting of all the descendents of v in T.

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.

Proper Binary tree


Each internal node has two children, left child and
right child.
Left child comes before a right child.

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 represented by a tree

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.

Nodes 7, 9, 11, 12, 16, 17, 20, 21, 26, 27.

Internal nodes
associated with the operators +, –, X, /.

Nodes 1, 2, 3, 4, 5, 6, 8, 10, 13.


D. S. R. Murthy DSA-4 Basic Data Structures (Contd…) 6

External node value


Variable or constant value.

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

Internal node value


Value obtained by applying its operation to the
values of its children.

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

Depth of a node v of a tree T


No. of ancestors of v, excluding v itself.

Depth of the root of T


0 (Zero)

Recursive definition for Depth of a node v


• If v is the root, then the depth of v is 0.

• Otherwise, the depth of v is one plus the depth of the


parent of v.

Height of a of a tree T
Maximum depth of an external node of T.

Recursive definition for Height of a node v


• If v is an external node, then the height of v is 0.

• Otherwise, the height of v is one plus the maximum


height of a child of v.
D. S. R. Murthy DSA-4 Basic Data Structures (Contd…) 9

Tree Traversals
Systematic way of accessing, or visiting, all the
nodes of T.

Preorder Traversal of a tree T


1. Visit the root of T first.

2. Recursively traverse subtrees rooted at its children.

For ordered tree


Subtrees are traversed according to the order of
the children.

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

Postorder Traversal of a tree T


Opposite of the Preorder traversal.

1. Recursively Traverse the subtrees rooted at its


children first.
2. Visit the root of T.

For ordered tree


Visit a node v after it has visited all the other
nodes in the subtree rooted at v.

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

Binary Tree Traversals

Preorder Traversal of a Binary tree


1. Visit the root.
2. Search the left subtree if it exists.
3. Search the right subtree if it exists.

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

Postorder Traversal of a Binary tree


1. Search the left subtree if it exists.
2. Search the right subtree if it exists.
3. Visit the root.

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

Inorder Traversal of a Binary tree


Informally viewed as visiting the nodes of T “from
left to right”.

For every node v, the Inorder traversal visits v


after all the nodes in the left subtree of v and
before all the nodes in the right subtree of v.

1. Search the left subtree if it exists.


2. Visit the root.
3. Search the right subtree if it exists.

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.

Guaranteed to visit each node exactly once.


D. S. R. Murthy DSA-4 Basic Data Structures (Contd…) 13

Euler Tour Traversal of a Binary Tree


Informally defined as a “walk” around T.

Start by going from the root toward its left child,


viewing the edges of T as being “walls” that we
always keep to our left.

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

Each node v of T is encountered three times by the


Euler tour:
• “On the left”

(before the Euler tour of v’s left subtree)


• “From below”

(between the Euler tour of v’s two subtrees)


• “On the right”

(after the Euler tour of v’s right subtree)

If v is external, then the three “visits” actually all


happen at the same time.
D. S. R. Murthy DSA-4 Basic Data Structures (Contd…) 14

Data Structures for representing Trees


Vector–Based Structure for Binary Trees

Linked Structure for Binary Trees

Vector–Based Structure for Binary Trees


For every node v of T, let p(v) be the integer defined
as:
• If v is the root of T, then p(v) =1.
• If v is the left child of node u, then p(v) = 2p(u).
• If v is the right child of node u, then p(v) = 2p(u) + 1.

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

Representation of a binary tree T by means of a vector S


– / + X + X 6 + 3 – 2 3 – 3 7 9 5 7 4
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

Fast
Simple
Very space inefficient,
if the height of the tree is large.
D. S. R. Murthy DSA-4 Basic Data Structures (Contd…) 15

Linked Structure for Binary Trees

Each node v of T
represented by an object with
references to the element stored at v.

Positions associated with


the children and parent of v.

Object associated with a node

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.

Potrebbero piacerti anche