DSA 03 Tree Structures Part 01
DSA 03 Tree Structures Part 01
Contents
o Terminologies
o Tree traversals
o Tree representation
o Binary tree
o Binary search tree
o AVL tree
o 2-3 tree, 2-3-4 tree
1
Some Examples
a
b c
d e f g h
i j k l m n
o p q
Some Examples
2
Trees
o Used to represent relationships
o hierarchical in nature
• “Parent-child” relationship exists between nodes in tree.
• Generalized to ancestor and descendant
• Lines between the nodes are called edges
o A subtree in a tree is any node in the tree together with all of its
descendants
Trees
r root
r1 r2 rk
T1 T2 Tk
subtree
3
Terminologies
o node: an item/element in a tree.
o parent (of node n): The node directly above node n in the tree.
o child (of node n): The node directly below node n in the tree.
o root: The only node in the tree with no parent.
o leaf: A node with no children.
o path: A sequence of nodes and edges connecting a nodes with the
nodes below it.
Terminologies
o siblings: Nodes with common parent.
o ancestor (of node n): a node on the path from the root to n.
o descendant (of node n): a node on the path from node n to a leaf.
o subtree (of node n): A tree that consists of a child (if any) of n and
the child’s descendants.
4
Terminologies
r root
rk
r1 r2 rk
k1 k2
T1 T2 Tk
k3 k4 k5
subtree
leaf
path k6
fit@hcmus | DSA | 2021 9
Terminologies
o degree/order
• Order of node n: number of children of node n.
• Order of a tree: the maximum order of nodes in that tree.
o depth/level (of node n)
• If n is the root of T , it is at level 1.
• If n is not the root of T , its level is 1 greater than the level of its parent.
if node n is root:
level(n) = 1
Otherwise:
level(n) = 1 + level(parent(n))
10
5
Terminologies
o Height of tree: number of nodes in the longest path from the root
to a leaf.
11
Terminologies
o Height of tree T:
if T is empty:
height(T) = 0
Otherwise:
height (T) = max{level(Ni)}, NiÎT
o Height of tree T:
if T is empty:
height(T) = 0
Otherwise:
height(T) = 1 + max{height(Ti)}, Ti is a subtree of T
12
6
Terminologies
order = k r root
order: 2
height = 4
rk
r1 r2 rk
k1 k2
T1 T2 Tk
k3 k4 k5
leaf
path k6
fit@hcmus | DSA | 2021 13
13
Kinds of Trees
14
7
General Tree
o Set T of one or more nodes such that T is partitioned into disjoint
subsets
• A single node r , the root
• Sets that are general trees, called subtrees of r
15
n-ary tree
o set T of nodes that is either empty or partitioned into disjoint
subsets:
• A single node r , the root
• n possibly empty sets that are n-ary subtrees of r
16
8
Binary tree
o Set T of nodes that is either empty or partitioned into disjoint
subsets
• Single node r , the root
• Two possibly empty sets that are binary trees, called left and right subtrees
of r
17
Traversals
18
9
Traversal
o Visit each node in a tree exactly once.
19
Pre-order Traversal
PreOrder(root)
{
if root is empty
Do_nothing;
Visit root; //Print, Add, ...
//Traverse every Childi.
PreOrder(Child0);
PreOrder(Child1);
…
PreOrder(Childk-1);
}
fit@hcmus | DSA | 2021 20
20
10
Post-order Traversal
PostOrder(root)
{
if root is empty
Do_nothing;
//Traverse every Childi
PostOrder(Child0);
PostOrder(Child1);
…
PostOrder(Childk-1);
Visit at root; //Print, Add, ...
}
21
In-order Traversal
InOrder(root)
{
if root is empty
Do_nothing;
//Traverse the child at the first position
InOrder(Child0);
Visit at root;
//Traverse other children
InOrder(Child1);
InOrder(Child2);
…
InOrder(Childk-1);
}
22
11
Traversals
23
Examples
Pre-order a
In-order b c
d e f g h
Post-order
i j k
24
12
Examples
Pre-order
a
• abdeijcfgkh
In-order b c
• dbiejafckgh
d e f g h
Post-order
i j
• dijebfkghca k
25
Examples
b c
d e f g h
26
13
Tree Representation
27
Tree Representation
A
Root
B C
D E F G H
I J K
28
14
Tree Representation
info child id next
a
1 a 2 3
2 b 4 5
3 c 6 7 8
4 d b c
5 e 9 10
6 f
7 g 11 d e f g h
8 h
9 i
10 j i j k
11 k
29
Tree Representation
Info Eldest Child Next Sibling
a
1 a 2 0
2 b 4 3
3 c 6 0
4 d 0 5 b c
5 e 9 0
6 f 0 7
7 g 11 8 d e f g h
8 h 0 0
9 i 0 10
10 j 0 0 i j k
11 k 0 0
30
15
Tree Representation
A
Root
B C
D E F G H
I J K
31
Tree Representation
Info Parent
1 a 0
a
2 b 1
3 c 1
4 d 2
b c
5 e 2
6 f 3
7 g 3 d e f g h
8 h 3
9 i 5
10 j 5
i j k
11 k 7
32
16