0% found this document useful (0 votes)
25 views

DSA 03 Tree Structures Part 01

The document discusses tree structures and their terminology. It defines trees as hierarchical structures consisting of nodes connected by edges in a parent-child relationship. Key terms discussed include root, leaf, height, depth, siblings, ancestors and descendants. The document also covers different types of trees like binary trees and n-ary trees. It explains common tree traversal algorithms like pre-order, in-order and post-order traversals and provides examples.

Uploaded by

Oanh Kim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

DSA 03 Tree Structures Part 01

The document discusses tree structures and their terminology. It defines trees as hierarchical structures consisting of nodes connected by edges in a parent-child relationship. Key terms discussed include root, leaf, height, depth, siblings, ancestors and descendants. The document also covers different types of trees like binary trees and n-ary trees. It explains common tree traversal algorithms like pre-order, in-order and post-order traversals and provides examples.

Uploaded by

Oanh Kim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Tree Structures

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

fit@hcmus | DSA | 2021 2

1
Some Examples
a

b c

d e f g h

i j k l m n

o p q

fit@hcmus | DSA | 2021 3

Some Examples

Organizational chart Directory tree


fit@hcmus | DSA | 2021 4

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

fit@hcmus | DSA | 2021 5

Trees
r root

r1 r2 rk

T1 T2 Tk

subtree

fit@hcmus | DSA | 2021 6

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.

fit@hcmus | DSA | 2021 7

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.

fit@hcmus | DSA | 2021 8

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))

fit@hcmus | DSA | 2021 10

10

5
Terminologies
o Height of tree: number of nodes in the longest path from the root
to a leaf.

o Height of a tree T in terms of the levels of its nodes


• If T is empty, its height is 0.
• If T is not empty, its height is equal to the maximum level of its nodes.

fit@hcmus | DSA | 2021 11

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

fit@hcmus | DSA | 2021 12

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

fit@hcmus | DSA | 2021 14

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

fit@hcmus | DSA | 2021 15

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

fit@hcmus | DSA | 2021 16

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

fit@hcmus | DSA | 2021 17

17

Traversals

fit@hcmus | DSA | 2021 18

18

9
Traversal
o Visit each node in a tree exactly once.

o Many operations need using tree traversals.

o The basic tree traversals:


• Pre-order
• In-order
• Post-order

fit@hcmus | DSA | 2021 19

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, ...
}

fit@hcmus | DSA | 2021 21

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);
}

fit@hcmus | DSA | 2021 22

22

11
Traversals

fit@hcmus | DSA | 2021 23

23

Examples

Pre-order a

In-order b c

d e f g h

Post-order
i j k

fit@hcmus | DSA | 2021 24

24

12
Examples
Pre-order
a
• abdeijcfgkh

In-order b c

• dbiejafckgh
d e f g h

Post-order
i j
• dijebfkghca k

fit@hcmus | DSA | 2021 25

25

Examples

b c

d e f g h

fit@hcmus | DSA | 2021 26

26

13
Tree Representation

fit@hcmus | DSA | 2021 27

27

Tree Representation
A
Root

B C

D E F G H

I J K

fit@hcmus | DSA | 2021 28

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

fit@hcmus | DSA | 2021 29

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

fit@hcmus | DSA | 2021 30

30

15
Tree Representation
A
Root

B C

D E F G H

I J K

fit@hcmus | DSA | 2021 31

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

fit@hcmus | DSA | 2021 32

32

16

You might also like