You are on page 1of 11

TREES

TREE - DEFINITION
A tree is a finite set of one or more nodes such that:
1. There is a specially designated node called the root.
2. The remaining nodes are partitioned in n  0 disjoint sets
T1, T2, …, Tn, where each of these sets is a tree.
3. T1, T2, …, Tn are called the sub-trees of the root.

Recursive Definition
TREE

B F K

C H D L X

Q G M I N P
NODE TYPES
A

B F K

C H D L X

Q G M I N P

Root (no parent)

Intermediate nodes (has a parent and at least one child)

Leaf nodes (0 children)


DEGREE OF A NODE
NUMBER OF CHILDREN
A

B F K

C H D L X

Q G M I N P

Degree 1 Degree 3

Degree 0 Degree 2
LEVEL OF A NODE
DISTANCE FROM THE ROOT
A

B F K

C H D L X

Q G M I N P

Level 0 Level 2

Level 1 Level 3

Height of the Tree = Maximum Level


The height of a tree is a height of the root.
EXAMPLES OF BINARY TREES

A A

B K B

C H L X C

Q M P Q

M
BINARY TREE TRAVERSAL
IN ORDER TRAVERSAL (LNR)

B K

C H L X

Q M P

Q C M B H A L K X P
BINARY TREE TRAVERSAL
PRE ORDER TRAVERSAL (NLR)

B K

C H L X

Q M P

A B C Q M H K L X P
BINARY TREE TRAVERSAL
POST ORDER TRAVERSAL (LRN)

B K

C H L X

Q M P

Q M C H B L P X K A
BINARY TREE TRAVERSAL
A

B K

C H L X

Q M P

NLR – visit when at the left of the Node A B C Q M H K LX P

LNR – visit when under the Node Q C M B H A L KX P

LRN – visit when at the right of the Node Q M C H B L P XK A

You might also like