Post on 21-Feb-2021
7. Trees
Human’s View
root
leaf
Computer’s View
root
leaf
Tree : Introduction
Array, Stack, Queue, Link list come under Linear Structure
Tree is Non-linear Structure
Very Efficient when using it for Search, Insert, Delete, etc.
Data are organized in a Hierarchical manner.
One common example is in the investigation of genealogies
The pedigree chart shows someone’s ancestors
The lineal chart is a chart of descendants rather than ancestors
Dusty
Honey Bear Brandy
Brunhilde Terry Coyote Nugget
Italic
Osco-Umbrain Latin
Oscan Umbrain Spanish French Italian
Dusty’s father
Dusty’s mother
What is a Tree?
Definition
A tree is a finite nonempty set of nodes such that
1) there is a special node called root
2) remaining nodes are partitioned into n ( 0) disjoint sets
T1, T2, · · ·,Tn where each of these is also a tree;
(Especially, we call each Ti a subtree of the root)
Properties
All nodes must be connected.
At least, one node must exist
Any cycle should not be allowed.
It must have a hierarchical structure
A
B C
D E F G H
Tree : Example
Node A is a root; it has 3 subtrees T1, T2, T3
where T1 = {B, E, F, K, L}, T2 = {C, G}, T3 = {D, H, I, J, M}.
Node B is a root of T1; it has two subtrees T11 and T12
where T11 = {E, K, L}, T12 = {F}.
The remaining nodes are similarly defined.
A
B C
E F
K
G
L
D
H I
M
J
Tree : Terminologies
Degree of a node : The number of subtrees of the node
Degree of a tree : The maximum degree in the tree
Parent / Child : One to Many (1 : m) relationship
Sibling : Child nodes with the same parent
Leaf : node with degree 0
Ancestor : All the nodes along the path from the root to the node
Descendant : All the nodes that are in its subtrees
Level : Level of root is 1;
For subsequent nodes, the level is the node’s parent + 1
Height (= Depth) of a tree : the maximum level of the tree
Tree : Terminologies
Consider a node E; What is its parent? child? sibling? ancestor?
What are leaf nodes? Non-leaf nodes?
What is degree of tree?
What is height of tree?
A
B C
E F
K
G
L
D
H I
M
J
3 Degree
2
2 0
0 0
1
0
3
1 0 0
0
Level
1
2
3
4
What is a Binary Tree?
Definition
A binary tree is a finite set of nodes such that
it is either empty or it consists of root and two disjoint binary trees, called, left subtree and right subtree
Key Characteristics
Degree of any given node must not exceed two. (i.e., 0, 1, or 2)
Left subtree and right subtree are distinguished.
The order/sequence of subtrees is important.
Binary tree may be empty (i.e., zero node)
≠
A
B Two different binary trees, but viewed as trees, they are the same
A
B
B
D E
C
F
A
Left subtree Right subtree
Binary Tree : Examples
(a) (b)
A
(c) (d)
A
B
A
B
(e)
B
D E
C
A
(f)
B
C
A
(g)
B
C
A
Binary Tree : Properties (1)
Number of Nodes of a Binary Tree
Maximum Number of Nodes on level i : 2i–1 (for i ≥ 1)
Maximum Number of Nodes of height h : 2h – 1 (for h ≥ 1)
Minimum Number of Nodes of height h : h (for h ≥ 1)
level
1
2
3
…
h. . . . . . . . . . . . .
Max. Number of Nodes
= 1 + 2 + 4 + … + 2h-1 = 2h–1
hei
ght =
h
level
1
2
3
…
h. . .
Min. Number of Nodes
= 1 + 1 + … + 1 = h
Height (= h) of a Binary Tree
Let n be the number of nodes of a binary tree; Then, h n 2h – 1
1) From h n, maximum height = n
2) From n 2h – 1, minimum height = log2(n+1)
Thus, log2(n+1) h n
Binary Tree : Properties (2)
level
1
2
3
…
h
. . . h ≤ n
. . . . . . . . . . . . .
. . . . .
level
1
2
3
…
h
n ≤ 2h–1
Relationship between n0 and n2
Let n0 : number of leaf nodes
n1 : number of nodes with degree 1
n2 : number of nodes with degree 2
Then, n0 = n2 + 1
Proof) n : total number of nodes;
Clearly, n = n0 + n1 + n2 ……(1)
Let B : number of branches;
Then, n = B + 1 where B = n1 + 2n2;
Thus, n = n1 + 2n2 + 1 ………(2)
From (1), (2), n0 = n2 + 1
Binary Tree : Properties (3)
Ex:
n0 = 4, n1 = 2, n2 = 3
B = n1 + 2n2 = 8
Types of Binary Trees
Full Binary Tree
Degree of every node except leaves is ‘2’
All leaves are located at the same level h
Total number of nodes is 2h – 1
Complete Binary Tree
It is a full binary tree down to the level (h – 1)
At the level h, nodes are filled from left to right sequentially.
Skewed(= Degenerated) Binary Tree
Degree of every node except leaves is ‘1’
The tree is skewed or zigzagged
Total number of nodes is h (h is height)
Full Binary Tree
Skewed(= Degenerated) Binary Tree
Binary Trees : Examples
(a) height = 2 (b) height = 3 (c) height = 4
???
A
B
C
D
A
B C
A
B
D E
C
F G
A
B
C
D
(a) (b)
Complete Binary Tree
Not Complete Binary Tree
Binary Trees : Examples
(a) (b) (c)A
B
A
B
D E
C
F
(a) (b)
A
B
D E
C
F G
H I
A
B
D E
C
F G
H I J
A
B
D E
C
F
G H
Binary Tree : Numbering Nodes
Numbering scheme in a Binary Tree
Numbering nodes from top to bottom (in a tree)
Number nodes from left to right (at the same level)
Cf) The numbers are used for recognizing each position at the tree
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Position of Parent and Children
Parent of node i is the node floor(i/2), where i ≠ 1
Left child of node i is the node 2i, where 2i n
Right child of node i is the node 2i+1, where 2i + 1 n)
Binary Tree: Parent & Children Position
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Ex; for node 5,- Its parent is node 2- Its left child is node 10- Its right child is node 11
Binary Tree Implementation: Array
How to Implement Binary Tree?
Use of One-dimensional Array such that
its size becomes at most 2h if Binary tree has h height (and its 0th
position is not used)
Store elements sequentially from the Root to the Leaves.
Each position of Parent, Left child, Right child of node i is given as
PARENT(i) = floor(i/2) (where, i ≠ 1)
LEFT_CHILD(i) = 2i (where, 2i n)
RIGHT_CHILD(i) = 2i + 1 (where, 2i + 1 n)
What are the Parent, Left child, Right child of node e in tree[ ]? Which positions are they located in?
Binary Tree Implementation: Array
1
2 3
4 5 6 7
8 9 10
a
b c
d e f g
h i j
a b c d e f g h i jtree[ ]
1 2 3 4 5 6 7 8 9 10
Any Problem?
For Some Cases(e.g., Skewed Binary Tree), many unused spaces exist!
Binary Tree Implementation: Array
1
3
7
a
b
c
d
15
a - b - - - c - - -tree[ ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
- - - - d
In case of n nodes, the array size of binary tree has
Minimum: n + 1 (for full/complete tree)
Maximum: 2n (for skewed tree)
In case of h height, the array size of binary tree has
Maximum: 2h – 1 (excluding 0th position)
If Binary tree is not full or complete, many empty spaces may be incurred within the array.
Especially, a binary tree is skewed, memory(space) utilization is n/2n
Ex) if n is 10, what is the utilization?
Binary Tree Implementation: Array
. . . . . . . .
. . .
n nodes
h =
nh =
log
(n+
1)
Node Definition
typedef struct node *tree_ptr;
typedef struct node {
int data;
tree_ptr left_child;
tree_ptr right_child;
}
Non-Leaf Node Leaf Node
☞ Links of all leaves are NULL!
Binary Tree Implementation: Linked List
DATAleft_child
right_child
Indicate Left child
Indicate Right child
DATANULL NULL
Example 1
Binary Tree Implementation: Linked List
Binary Tree Linked List Representation
A
B
D E
C
F G
H I
A
B
root
C
D E
NULL
NULL F
NULL
NULL G
NULL
NULL
HNULL
NULL I
NULL
NULL
Binary Tree Implementation: Linked List
Example 2
Binary Tree Linked List Representation
A
B
C
D
E
root
D
NULL
E
NULL
NULL
C
NULL
B
NULL
A
NULL
Use of Array
(Different from Binary tree), the use of arrayis hard to find the positions corresponding toparent and children of the given node
Use of Linked List (more general)
Number of link fields is identical to the maximum degree k of the treesince each node’s degree is usually different
Every nodes whose degree is less than k have NULL links
Leaf has all NULL links
Tree Implementation
child 1 child 2
DATA
… …
child k… …
a b - - c - -
tree[ ] 1 2 3 4 5 6 7
Too Many Null Links in a Tree
We need k links per each node (where k : degree of a tree)
Number of links : n * k (where n : number of nodes in a tree)
Number of non-null links : n – 1
There exist one link that points to each node (excluding the root)
Number of null links : (n * k) – (n – 1)
Null links percentage : ((k – 1) * n + 1) / (k * n)
Ex1) if k = 10, then approx. 90%
Ex2) if k = 2, then approx. 50%
Tree Implementation
A
B C
D E F G H
For instance, n=8, k=3
(k, #non-null)(3,2)
(3,2) (3,3)
(3,0) (3,0) (3,0) (3,0) (3,0)
Binary Tree Representation to a Tree
It consists of Two Procedures: Convert & Rotate
(1) Convert each node in a tree by choosing its left-most child and its right-next-sibling;
(2) Rotate the left-child/right-sibling tree by 45 degrees clockwise;
Note : - Right link of the root node always has NULL value.
- NULL link percentage considerably decreases.
child 1 child 2
DATA
… …
child k… … Left-most-child Right-next-Sibling
DATA
Tree
Left child-Right sibling
Tree
Binary Tree
convert
Binary Tree Representation to a Tree
A
B
E G
D
F
C
A
B
E G
D
F
C
A
B
E
G
DF
C
Binary Tree Representation to a Tree
Tree Left child-Right sibling Tree Binary Tree
A
B
A
B
A
B
A
B C
Tree Left child-Right sibling Tree Binary Tree
A
B C
A
B
C
convert rotate
convert rotate
Binary Tree Traversal
Binary Tree Traversal
It comes under visiting each node in a binary tree exactly once.
It produces a linear order for the information in a binary tree.
Traversal methods
INORDER : Left, Visit, Right (LVR)
PREORDER : Visit, Left, Right (VLR)
POSTORDER : Left, Right, Visit (LRV)
LEVEL-ORDER
(Where, Left moves to the left of the current node
Visit visits to the current node
Right moves to the right of the current node)
Procedures
Inorder : LVR
ExampleINORDER : B A C
PREORDER: A B C
POSTORDER: B C A
A
B C
Preorder : VLR
2
1 3
left subtree right subtree
1
2 3
left subtree right subtree
3
1 2
left subtree right subtree
Postorder : LRV
Binary Tree Traversal
INORDER : Recursive Version
INORDER (tree_ptr ptr) {
if (ptr != NULL)
{
INORDER (ptr -> left_child)
printf (ptr -> data)
INORDER (ptr -> right_child)
}
}
INORDER Traversal in a Binary Tree
ptr points to the first node in a tree
Inorder: LVR
a
b c
d e f
Inorder : LVR
4
2
6
1 3 5
Result: d b e a f c
*
+
E
* D
/ C
A B
6 7 9 10
12 13
15 16
18 19
5
4
3
2
1
17
14
11
8
Output: A / B * C * D + E
INORDER (LVR) : ExampleTrace of ProgramBinary Tree with Arithmetic Expression
Inorder (Value)
ValueInorder (Value)
Value
1 (+) - 11 (C) -
2 (*) - 12 (Null) -
3 (*) - 11 (C) printf
4 (/) - 13 (Null) -
5 (A) - 2 (*) printf
6 (Null) - 14 (D) -
5 (A) Printf 15 (Null) -
7 (Null) - 14 (D) printf
4 (/) Printf 16 (Null) -
8 (B) - 1 (+) printf
9 (Null) - 17 (E) -
8 (B) Printf 18 (Null) -
10 (Null) - 17 (E) printf
3 (*) Printf 19 (Null) -
INORDER (tree_ptr ptr) {
tree_ptr stack [MAX_STACK_SIZE];
top = -1; /* Initialize stack */
for( ; ; ) {
while (ptr != NULL) {
top = top + 1;
if (top >= MAX_STACK_SIZE–1) return;
stack[top] = ptr;
ptr = ptr -> left_child; }
if (top == -1) break; /*empty stack*/
ptr = stack[top];
top = top – 1;
printf(ptr -> data);
ptr = ptr -> right_child;
}
}
INORDER (LVR) : Iterative Version
*
/ C
A B
4 5 7 8
10 113
2
1
9
6
A
/
stack
pop
*
pop B
stack
*
poppop
stack
C pop
Result:A /
Result:A / B *
Result:A/B*C
INORDER : Performance Analysis
For the Iterative Version
We need a stack to return to previously visited nodes safely.
Every node in a binary tree is placed on and removed from the stack once.
Time Complexity
Let n be the number of nodes in a binary tree
Every node is pushed and popped exactly once; i.e., n times
Also, ptr will be NULL once for every (n + 1) NULL links
Thus, the time complexity is O(n)
Space Complexity
The stack size required is equal to the height h of the binary tree;
log2(n+1) h n
Thus, the space complexity becomes O(n)
PREORDER
PREORDER (tree_ptr ptr) {
if (ptr != NULL)
{
printf(ptr->data)
PREORDER(ptr->left_child)
PREORDER(ptr ->right_child)
}
}
PREORDER Traversal in a Binary Tree
ptr points to the first node in a tree
Preorder: VLR
a
b c
d e f
Preorder : VLR
1
2 5
3 4 6
Result: a b d e c f
POSTORDER (tree_ptr ptr) {
if (ptr != NULL)
{
POSTORDER(ptr->left_child)
POSTORDER(ptr ->right_child)
printf(ptr->data)
}
}
POSTORDER
POSTORDER Traversal in a Binary Tree
ptr points to the first node in a tree
Postorder: VLR
a
b c
d e f
Postorder : LRV
6
3
5
1 2 4
Result: d e b f c a
LEVEL-ORDER (= Breadth First)
LEVEL_ORDER (tree_ptr ptr) {
int front = rear = 0;
tree_ptr queue[MAX_QUE_SIZE];
if (ptr) insert(ptr); /*Insert ptr into queue*/
while (ptr != null) {
ptr = delete(); /*Delete from the queue */
printf(ptr -> data);
if (ptr -> left_child )
insert(ptr -> left_child);
if (ptr -> right_child )
insert(ptr -> right_child);
}
}
LEVEL-ORDER Traversal
It visits the nodes using the orders by the numbering scheme (for trees):
It traverses a tree top to bottom and left to right.
a
b c
d e f
Lev
el-O
rder
3
4 5 6
Result: a b c d e f
1
2
Queue
abc
ptr
d
Insert delete
e
PREORDER (VLR) : a b d g h e i c f j
POSTORDER (LRV) : g h d i e b j f c a
LEVEL-ORDER : a b c d e f g h i j
INORDER (LVR) : g d h b e i a f j c
Example
a
b c
d e f
jg h i
INORDER : (a + b) * (c – d) / (e + f)
PREORDER : / * + a b – c d + e f
POSTORDER : a b + c d – * e f + /
/
* +
+ - e f
a b c d
Expression Tree
Infix form!
Prefix form!
Postfix form!
Binary Tree Reconstruction
Possible to Reconstruct a Binary Tree from its Traversal results?
☞ Yes! But we need two traversal results.
Available Combinations for Reconstruction
INORDER & PREORDER
INORDER & POSTORDER
INORDER & LEVEL-ORDER
Root Position in Traversal Results
PREORDER & LEVEL-ORDER : the Root exists at the first position
POSTORDER : the Root exists at the last position
INORDER : the Root exists somewhere in the result
PREORDER : a b d g h e i c f j
POSTORDER : g h d i e b j f c a
LEVEL-ORDER : a b c d e f g h i j
INORDER : g d h b e i a f j c
a
b c
d e f
jg h i
Root of T
Subtree:T1
Subtree:T2
Binary Tree Reconstruction
Tree: T
T1 T2
Root of T1
Root of T2
With INORDER & PREORDER
What is the Binary Tree for the Traversal results of INORDER & PREORDER?
INORDER : g d h b e i a f j c
PREORDER : a b d g h e i c f j
Scan PREORDER left to right using INORDER to separate left and right subtrees.
a is the root of the tree; {g, d, h, b, e, i} are in the left subtree; {f, j, c} are in the right subtree.
{ g, d, h, b, e, i } { f, j, c }
a
With INORDER & PREORDER
PREORDER : a b d g h e i c f j
b is the next root (of the left branch); {g, d, h} are in the left subtree; {e, i} are in the right subtree.
{ g, d, h, b, e, i } { f, j, c }
a
a
b { f, j, c }
{ g, d, h } { e, i }
With INORDER & PREORDER
PREORDER : a b d g h e i c f j
d is the next root; g is the left subtree; h is the right subtree.
a
b { f, j, c }
{ g, d, h } { e, i }
a
b
d
{ f, j, c }
{ e, i }
{ g } { h }
With INORDER & PREORDER
{g, h} has no child
PREORDER : a b d g h e i c f j
e is the next root; i is the right subtree
PREORDER : a b d g h e i c f j
c is the next root; {f, j} is in the left subtree;
f is the next root; j is in the right subtree.
a
b
d
{ f, j, c }
{ e, i }
{ g } { h }
e
{ i }
c
f
{ j }
With INORDER & POSTORDER
What is the Binary Tree for the Traversal results of INORDER & POSTORDER?
INORDER : g d h b e i a f j c
POSTORDER : g h d i e b j f c a
Scan POSTORDER right to left using INORDER to separate left and right subtrees.
a is the root of the tree;{g, d, h, b, e, i} are in the left subtree; {f, j, c} are in the right subtree.
{ g, d, h, b, e, i } { f, j, c }
a
With INORDER & POSTORDER
POSTORDER : g h d i e b j f c a
c is the next root (of the right branch);There is no right subtree; {f, j} are in the left subtree.
{ g, d, h, b, e, i } { f, j, c }
a
a
c{ g, d, h, b, e, i }
{ f, j }
Do the remaining
steps!
Binary Tree Traversal : Copy
tree_ptr COPY (tree_ptr original) {
tree_ptr temp;
if (original != NULL) {
temp = (tree_ptr) malloc (sizeof(node));
if ( IS_FULL(temp) ) exit;
temp -> left_child = COPY(original -> left_child)
temp -> right_child = COPY(original -> right_child);
temp -> data = original -> data;
return temp;
}
return NULL;
}
Copy a Binary Tree by using POSTORDER
Null Null
d
temp->left_child->left_child=COPY( )
temp->left_child=COPY( )
Null Null
b
Null Null
e
a
b c
d e f
Null Null Null Null Null Null
temp->left_child->left_child->left_child=Null
…