skewed binary tree

1
Trees
(Unit 7)
2
Outline
Trees: Definition and Basic Terminologies
Representation of Trees
Binary Trees: Basic Terminologies and Types
Representation of Binary Trees
Array representation of binary trees
Linked representation of binary trees
Binary Tree Traversals
Inorder traversal
Postorder traversal
Preorder traversal
Threaded Binary Trees
Applications
3
•
•
•
•
A tree is defined as a finite set of one or
more nodes such that
There is a specially designated node called
the root and
The rest of the nodes could be partitioned
into t disjoint sets (t >0) each set
representing a tree Ti, i=1,2, . . . t known as
subtree of the tree.
A node in the definition of the tree
represents an item of information, and the
links between the nodes termed as
branches, represent an association between
the items of information.
4
Basic terminologies
• degree of the node
• leaf nodes or
•
•
•
•
•
•
•
•
3
terminal nodes
non terminal nodes
children
2
siblings
ancestors
degree of a tree
hierarchical
2 E
structure
height or depth of a
tree
forest
0 K4
0
B
2
30
F
L
4
A
1
1
C
2
30
G
31
0
3
D
H 30 I
M
4
2
30
J
3
5
Representation of a tree
• List Representation
• Linked List Representation
6
List representation
(A (B(F,G,H), C, D(I), E(J,K(L))) )
Level 1
A
B
F
G
C
H
D
I
Level 2
E
J
K
L
Level 3
Level 4
7
• Linked List Representation
DATA
LINK 1
LINK 2
…
LINK n
(a) General node structure
T
A
B
F
C
G
H
D
I
E
K
J
L
(b) Linked list representation of the tree
8
An alternative elegant linked representation
TAG
DATA / DOWNLINK
LINK
1
A
0
1
B
1
1
C
0
1
G
1
0
0/1
(a) General node structure
F
H
H
1
D
1
I
1
E
1
J
O
1
K
1
(b) Linked representation of the tree
L
9
Binary Trees
• A binary tree has the characteristic of all
nodes having at most two branches, that is, all
nodes have a degree of at most 2.
• A binary tree can therefore be empty or
consist of a root node and two disjoint binary
trees termed left subtree and right subtree.
10
Level 1
A
B
D
Level 2
C
E
F
Fig. 2
G
Level 3
11
Important observations regarding binary trees:

The maximum number of nodes on level i of a
binary tree is 2i-1, i>1

The maximum number of nodes in a binary tree
of height h is 2h-1, h>1

For any non empty binary tree, if to is the number
of terminal nodes and t2 is the number of nodes of
degree 2, then to=t2+1
12
• A binary tree of height h which has all its
permissible maximum number of nodes viz., 2h1 intact is known as a full binary tree of height
h.
• A binary tree with n’ nodes and height h is
complete if its nodes correspond to the nodes
which are numbered 1 to n (n’ n) in a full
binary tree of height h.
• Also, the height of a complete binary tree with n
elements has a height h given byh  log (n  1)
2
13
A binary tree which is dominated solely by left child
nodes or right child nodes is called a skewed binary tree or
more specifically left skewed binary tree or right skewed binary
tree respectively.
a
b
m
n
o
c
d
Left skewed
Right skewed
p
14
Types of Binary Trees
• Skewed – only one child
• Complete – always two children
• Balanced – “mostly” two children
– more formal definitions exist, above are intuitive ideas
Skewed
binary tree
Balanced
binary tree
Complete
binary tree
15
Binary Trees Properties
• Skewed
– Height = O(n) for n
nodes
– Similar to linked list
Skewed
binary tree
• Balanced
– Height = O( log(n) )
for n nodes
– Useful for searches
Balanced
binary tree
16
A complete binary tree obeys the following properties
with regard to its node numbering:
If a parent node has a number i then its left child has
the number 2i (2i<n). If 2i>n then i has no left child.
If a parent node has a number i, then its right child
has the number 2i+1 (2i+1<n). If 2i+1>n then i has
no right child.
If a child node (left or right) has a number i then the
parent node has the number
i /2 if i 1. If i
=1 then i is the root and hence has no parent.
17
Representation Of Binary Trees
• Array Representation
1
2
4
a
b
c
10
d
5
11
e
f
1
2
3 4 5
a
b
c d
6
7
8 9 10 11
e
f
18
Linked representation
a
LCHILD
DATA
b
RCHILD
c
d
e
f
19
Binary Tree Traversal
• A binary tree traversal requires that each node
of the tree be processed once and only once
in a predetermined sequence.
• In the depth-first traversal processing process
along a path from the root through one child
to the most distant descendant of that first
child before processing a second child.
20
Pre order/In Order/Post Order traversals
• Pre Order: Contents of the root are printed, followed by the left
subtree and then the right subtree (Visit – left – right)
• In Order: Left sub-tree traversed first before visiting the parent,
followed by traversal of the right sub-tree (left - Visit – right)
• Post Order: Contents of left subtree are printed first, followed
by right subtree and finally
the
f
root node (left - right – Visit)
j
c
a
d
h
k
i
21
22
23
24
25
26
27
28
29
30
Threaded Binary Trees
• A.J. Perlis and C.Thornton devised a prudent method to utilize
the (n+1) empty pointers, introducing what are called threads.
• For a node NODE if RCHILD(NODE) is NIL then the null pointer
is replaced by a thread which points to the node which would
occur after NODE when the binary tree is traversed in inorder.
• Again if LCHILD (NODE) is NIL then the null pointer is replaced
by a thread to the node which would immediately precede
NODE when the binary tree is traversed in inorder.
31
Inorder Traversal: G F B A D C E
A
B
F
C
D
E
G
LEFT
THREAD TAG
(True or
False)
LCHILD
DATA
RCHILD
RIGHT
THREAD TAG
(True or
False)
32
T
LCHILD
DATA
RCHILD
true
false
LEFT THREAD TAG
RIGHT THREAD TAG
(a) Empty threaded binary tree
Head node
T
f
f
f
f
A
f
f
t
t
D
t
E
t
t
F
G
f
t
B
f
C
t
f : false
(b) Non empty threaded binary tree
t : true
t
33
Applications
Expression Trees
((A+B)*C-D) G
• Traversals of an expression tree
• Conversion of Infix Expression to Postfix
Expression