Anda di halaman 1dari 43

Data Structures

Trees
Vikas Kumar
Visualizing Trees
Root

Leaves
The root is a node that has no parent; it can have only
child nodes. Leaves, on the other hand, have no children.
Definition
A tree can be defined recursively:
1. An empty structure is a tree
2. If t1, t2, , tk are disjoint trees, then the structure
whose root has the roots of t1, t2, , tk as its
children is also a tree
3. Only structures generated by rules 1 and 2 are
trees

The children of a node are, themselves, trees,


called subtrees
Notes
The definition of a tree does not impose any
condition on the number of children of a
given node. This number can vary from 0 to
any integer
A path is a sequence of nodes
(a0, a1, ..., an)
where ai + 1 is a child of ai
For each node in a tree, there exists a unique
path from the root node to that node
Trees: Some Terminology
Root
Level 0 R

Level 1 S T Internal Node

Level 2 X U V W
Leaf
Level 3 Y Z
Child of X

Subtree
Parent of Z and Y
Binary Trees
Binary trees are characterized by the fact that any
node can have at most two branches
Examples
Tree Traversals
Goal: visit all of the nodes within a tree
Certain predefined orders
breadth-first traversal
depth-first traversal
pre-visit depth-first traversal
post-visit depth-first traversal
in-order depth-first traversal (binary trees only)
Breadth-First Traversal
Performing such a traversal would visit the
nodes in the order:
A, B, H, C, E, I, M, D, F, G, J, K, L
Breadth-First Traversal
The implementation was already discussed
create a queue and enqueue the root node
dequeue a node, perform the appropriate operation
(e.g., print) and enqueue all of its children
continue until the queue is empty
Depth-first Traversal

Depth-first traversal proceeds as


far as possible to the left (or
right), then backs up until the
first crossroad, goes one step to
the right (or left), and again as
far as possible to the left (or
right). Repeat this process until
all nodes are visited.
Three Depth-first Traversals
Preorder: Visit r before traversing rs subtrees

Inorder: Visit r after traversing rs left subtree


but before traversing rs right subtree
binary tree only

Postorder: Visit r after traversing all of rs


subtrees
Pre-order Depth-first Traversal
Performing such a traversal would visit the
nodes in the sequence:
A, B, C, D, E, F, G, H, I, J, K, L, M
Iterative Preorder Traversal
This algorithm was already discussed

stack S
push root onto S
repeat until S is empty
v := pop S
visit v
push vs children onto S
Recursive Preorder Traversal

preOrder(v)
visit node v
for each child w of v do
preOrder(w)
In-order Depth-first Traveral
Performing such a traversal would visit the
nodes in the sequence:
3 4 5 6 7 8 10 13
7

6 10

4 8 13

3 5
Iterative Inorder Traversal
Stack S
Initialize all nodes to white
push root onto S
repeat until S is empty
v := pop S
If v is black
visit v
else
push vs right child (if any) onto S
change v to black
push (black) v onto S
push vs left child (if any) onto S
Recursive Inorder Traversal

inOrder(v)
inOrder(left child of v)
visit node v
inOrder(right child of v)
Post-order Depth-first Traversal
Performing such a traversal would visit the
nodes in the sequence:
D, C, F, G, E, B, J, K, L, I, M, H, A
Iterative Postorder Traversal
Take home exercise
Recursive Postorder Traversal
postOrder(v)
for each child w of v do
postOrder(w)
visit node v
Exercise

B C D E F

G H I J K L
Preorder:
Postorder:
Exercise

6 Preorder:
3 8

1 4 7 10 Inorder:

2 5 l3
Postorder:
11

12
Saving Trees Using Traversals
Imagine a program that creates a large tree
The content of the tree must be stored in a file if
we want to recover and use it at a later time
The stored information must enable re-
construction of an exact copy of the original tree
Can we re-construct a binary tree from just one
of its pre-order, inorder, or post-order
traversals ?
a a
preorder = a b
b b

inorder = a b b a
a b

postorder = a b b b
a a

No traversal by itself is enough to reconstruct


Re-constructing trees
Can we use the results of two traversals to re-
construct a unique binary tree?
pre-order = a b a a
post-order = b a b b

Preorder and postorder traversals are also


not enough to reconstruct
Re-constructing trees
Given inorder sequence and one of
Pre-order sequence
Post-order sequence
the tree is unique!
Inorder: C V U T X F E Y D Z Q W J
Postorder: C U T V E D Y Q J W Z F X

Start with postorder X


Last item (X) must
be root of tree
Inorder: C V U T X F E Y D Z Q W J
Postorder: C U T V E D Y Q J W Z F X

Locate X in inorder X
traversal
Items to left of X are
in left subtree
Items to right of X
are in right subtree
Inorder: C V U T X F E Y D Z Q W J
Postorder: C U T V E D Y Q J W Z F X

Lets try to construct X


the left subtree
V
Locate elements in
postorder
Find root of left
subtree from
postorder (V)
Inorder: C V U T X F E Y D Z Q W J
Postorder: C U T V E D Y Q J W Z F X

Find V in inorder X
traversal
V
C is left child of V

C
Inorder: C V U T X F E Y D Z Q W J
Postorder: C U T V E D Y Q J W Z F X

U & T are right X


subtree of V
V
Use postorder to
find root of right
subtree (T) C T
Inorder: C V U T X F E Y D Z Q W J
Postorder: C U T V E D Y Q J W Z F X

Find T in inorder X
U is left child of T
V

C T

U
Inorder: C V U T X F E Y D Z Q W J
Postorder: C U T V E D Y Q J W Z F X

Now consider right X


subtree of X
V F
Find root from
postorder (F)
C T

U
Inorder: C V U T X F E Y D Z Q W J
Postorder: C U T V E D Y Q J W Z F X

Find F in inorder X
All other nodes are
V F
in right subtree of F

C T

U
Inorder: C V U T X F E Y D Z Q W J
Postorder: C U T V E D Y Q J W Z F X

Find root of right X


subtree of F from
postorder V F
Root is Z
C T Z

U
Inorder: C V U T X F E Y D Z Q W J
Postorder: C U T V E D Y Q J W Z F X

Find Z in inorder X
E Y D in left subtree V F
of Z
Q W J in right
C T Z
subtree of Z

U
Inorder: C V U T X F E Y D Z Q W J
Postorder: C U T V E D Y Q J W Z F X

Consider left subtree X


( E Y D)
Find root from V F
postorder (Y)
C T Z

U Y
Inorder: C V U T X F E Y D Z Q W J
Postorder: C U T V E D Y Q J W Z F X

Find Y in inorder X
E is left child of Y
V F
D is right child of Y

C T Z

U Y

E D
Inorder: C V U T X F E Y D Z Q W J
Postorder: C U T V E D Y Q J W Z F X

Consider right X
subtree of Z
Find root from V F
postorder (W)
C T Z

U Y W

E D
Inorder: C V U T X F E Y D Z Q W J
Postorder: C U T V E D Y Q J W Z F X

Find W in inorder X
Q is left child of W
V F
J is right child of W

C T Z

U Y W

E D Q J
Inorder: C V U T X F E Y D Z Q W J
Postorder: C U T V E D Y Q J W Z F X

Tree complete X
What is pre-order?
V F

C T Z

U Y W

XVCTUFZYEDWQJ E D Q J
Reconstruction procedure
Given Inorder (I) & Postorder (P)
Last in P is root (R)
Find R in I
In I items to the left of R make up the left
subtree
In I items to the right of R make up the right
subtree
Locate each subtree in P and repeat