Professional Documents
Culture Documents
Defining Trees
A tree are
is a used
nonlinear
data structure
that represent
Trees
in applications
in which
the relationa between
hierarchical
relationship
among
the various
elements.
data
elements
needs to be
represented
in adata
hierarchy.
A
C
J
L
J
L
node
J
L
node
Tree Terminology
Leafusnode:
It refers
to aterms
nodethat
withare
nomost
children.
Let
discuss
various
frequently used
with trees.
A
C
Nodes E, F, G, H, I, J,
L, and M are leaf nodes.
J
L
E, F, with
G, and
are
Tree
rootHB,
children of nodes
node B.
containing
E,BF,
is the
these of
G,
andparent
H is aof
subtree
nodes.
node
A.
J
L
A
C
Degree of node C is 1
Degree of node D is 2
Degree of node A is 3
Degree of node B is 4
Edge
E
J
L
A
C
J
L
A
C
Nodes B, C, D, and K
are internal nodes. Level 1
J
L
Level 2
Level 3
A
C
Level 1
J
L
Level 2
Level 3
Just a minute
Consider the following tree and answer the questions that
follow:
a.
b.
c.
d.
e.
f.
g.
root
A
D
H
C
E
F
I
Just a minute
Answer:
a.
b.
c.
d.
e.
f.
g.
4
D and E
C
2
H does not have any siblings
The only sibling of D is E
F, G, H, and I
Depth = 3
Total number of
3
nodes = 2 1 = 7
1
3
B
4
C
6
B
4
C
3
B
4
C
5
0
A
3
D
C
4
E
Binary Tree
[0]
[1]
[2]
[3]
[4]
[5]
[6]
Array Representation
If 2i + 2 > n 1, then
the node does have a
right child.
If a node does not have a left child or a right child or both, the
respective left or right child fields of that node point to NULL.
Data
Node
root
52
36
24
68
59
24
72
70
Binary Tree
80
52
.
.
36
68
.
.
59
72
70
Linked Representation
80
Inorder Traversal
Steps for traversing a tree in inorder sequence are as
follows:
1. Traverse the left subtree
2. Visit root
3. Traverse the right subtree
Traversal complete
Preorder Traversal
Steps for traversing a tree in preorder sequence are as
follows:
1. Visit root
2. Traverse the left subtree
3. Traverse the right subtree
Preorder Traversal:
A B D H E
C F
Postorder Traversal
Steps for traversing a tree in postorder sequence are as
follows:
1. Traverse the left subtree
2. Traverse the right subtree
3. Visit the root
Postorder Traversal: H D E
B F
G C A
Just a minute
In _________ traversal method, root is processed before
traversing the left and right subtrees.
Answer:
Preorder
24
44
40
59
55
72
b.
c.
Display Found
Exit
Make currentNode point to its left child
Go to step 2
Just a minute
In a binary search tree, all the values in the left subtree of a
node are _______ than the value of the node.
Answer:
smaller
52
1.
2.
3.
4.
5.
36
68
6.
24
59
70
72
.
80
root
52
1.
2.
3.
4.
5.
36
68
6.
24
59
70
72
.
80
root
currentNode
52
1.
2.
3.
4.
5.
36
68
6.
24
59
70
72
.
80
root
parent = NULL
currentNode
52
1.
2.
3.
4.
5.
36
68
6.
24
59
70
72
.
80
root
parent = NULL
currentNode
52
1.
2.
3.
4.
5.
36
68
6.
24
59
70
72
.
80
root
parent
parent= NULL
currentNode
52
1.
2.
3.
4.
5.
36
68
6.
24
59
70
72
.
80
1.
2.
3.
4.
5.
55 > 52
root
parent
currentNode
52
a.
36
68
6.
24
59
70
72
.
80
1.
2.
3.
4.
5.
55 > 52
root
parent
currentNode
52
a.
36
68
6.
24
59
70
72
.
80
1.
2.
3.
4.
5.
55 > 52
root
parent
currentNode
52
a.
currentNode
36
68
6.
24
59
70
72
.
80
root
parent
52
1.
2.
3.
4.
5.
currentNode
36
68
6.
24
59
70
72
.
80
root
parent
52
.
.
36
2.
3.
4.
5.
currentNode
parent
1.
68
6.
24
59
70
72
.
80
55 < 68
root
52
.
.
36
2.
3.
4.
5.
currentNode
parent
1.
68
6.
24
59
70
72
.
80
55 < 68
root
52
.
.
36
2.
3.
4.
5.
currentNode
parent
1.
68
6.
24
59
72
currentNode
70
80
root
52
1.
2.
3.
4.
5.
parent
36
68
6.
24
59
72
currentNode
70
80
root
52
1.
2.
3.
4.
5.
parent
36
68
6.
parent
24
59
72
currentNode
70
80
1.
2.
3.
4.
5.
55 < 59
root
52
a.
36
68
6.
parent
24
59
72
currentNode
70
80
1.
2.
3.
4.
5.
55 < 59
root
52
a.
36
68
6.
parent
24
currentNode
currentNode = NULL
59
70
72
.
80
root
52
1.
2.
3.
4.
5.
36
68
6.
parent
24
currentNode = NULL
59
70
72
.
80
52
1.
2.
3.
4.
5.
36
68
6.
parent
24
currentNode = NULL
59
70
72
.
80
root
.
.
52
2.
3.
4.
5.
a.
b.
36
68
6.
24
59
72
b.
7.
70
80
root
.
.
52
2.
3.
4.
5.
a.
b.
36
68
6.
24
59
72
b.
7.
70
80
root
.
.
52
2.
3.
4.
5.
a.
b.
36
68
6.
24
59
72
b.
7.
55
70
80
root
.
.
52
2.
3.
4.
5.
a.
b.
36
68
6.
24
59
72
b.
7.
55
70
80
root
.
.
52
2.
3.
4.
5.
a.
b.
36
68
6.
parent
24
72
b.
7.
55
59
70
80
root
.
.
52
2.
3.
4.
5.
a.
b.
36
68
6.
parent
24
72
b.
7.
55
59
70
80
root
.
.
52
2.
3.
4.
5.
a.
b.
36
68
6.
parent
24
72
b.
7.
55
59
70
80
root
.
.
52
2.
3.
4.
5.
a.
b.
36
68
6.
parent
24
72
b.
7.
55
59
70
80
root
.
.
52
2.
3.
4.
5.
a.
b.
36
68
6.
parent
24
72
b.
7.
55
59
70
80
.
.
24
52
.
.
36
68
69
2.
3.
b.
i.
59
1.
72
c.
70
80
.
24
52
.
.
36
68
69
2.
3.
b.
i.
59
1.
72
c.
70
80
.
24
52
.
.
36
68
69
2.
3.
b.
i.
59
1.
72
c.
70
80
.
24
52
.
.
36
68
69
2.
3.
b.
i.
59
1.
72
c.
70
80
.
24
52
parent
36
68
69
2.
3.
b.
i.
59
1.
72
c.
70
80
.
24
52
parent
36
68
.
69
2.
3.
b.
70 > 52
59
1.
72
i.
c.
70
80
.
24
52
parent
36
68
.
69
2.
3.
b.
70 > 52
59
1.
72
i.
c.
70
80
52
parent
currentNode
.
24
36
68
.
69
2.
3.
b.
70 > 52
59
1.
72
i.
c.
70
80
52
parent
currentNode
.
24
36
68
69
2.
3.
b.
i.
59
1.
72
c.
70
80
52
parent
currentNode
.
24
36
parent
68
69
2.
3.
b.
i.
59
1.
72
c.
70
80
52
70 > 68
currentNode
.
24
36
parent
68
69
2.
3.
b.
i.
59
1.
72
c.
70
80
52
70 > 68
currentNode
.
24
36
parent
68
69
2.
3.
b.
i.
59
1.
72
c.
70
80
52
70 > 68
currentNode
36
parent
68
1.
2.
3.
b.
i.
currentNode
24
59
.
69
72
c.
70
80
.
.
36
52
parent
68
1.
2.
3.
b.
i.
currentNode
24
59
.
69
72
c.
70
80
.
.
36
52
parent
68
1.
2.
3.
b.
i.
currentNode
24
59
.
69
72
c.
70
parent
80
.
.
52
70 < 72
36
68
1.
2.
3.
b.
i.
currentNode
24
59
.
69
72
c.
70
parent
80
.
.
52
70 < 72
36
68
1.
2.
3.
b.
i.
currentNode
24
59
72
c.
70
parent
currentNode
69
.
80
.
.
52
.
.
36
24
68
72
3.
b.
c.
70
80
parent
currentNode
69
2.
i.
.
.
59
Nodes located
1.
52
1.
2.
.
3.
36
68
24
59
72
4.
70
b.
80
5.
69
52
1.
2.
.
3.
36
68
24
59
72
4.
70
b.
80
5.
69
currentNode
parent
52
1.
2.
.
3.
36
68
24
59
72
4.
70
b.
80
5.
69
currentNode
parent
52
1.
2.
.
3.
36
68
24
59
72
4.
70
b.
80
5.
69
currentNode
parent
52
1.
2.
.
3.
36
68
24
59
72
4.
70
b.
80
5.
69
currentNode
parent
52
1.
2.
.
3.
36
68
24
59
72
4.
70
b.
80
5.
69
currentNode
parent
52
1.
2.
.
3.
36
68
24
59
72
4.
70
b.
80
5.
69
parent
1.
2.
3.
4.
5.
6.
7.
root
.
.
24
52
.
.
36
68
.
.
59
72
.
.
70
80
75
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
child
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
child
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
child
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
child
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
child
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
child
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
child
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
1.
2.
3.
4.
5.
root
.
.
24
52
.
.
36
68
.
.
59
72
.
.
70
75
80
2.
3.
4.
5.
Delete node 72
root
52
.
parent
36
68
.
currentNode
24
59
72
.
.
70
75
80
2.
3.
4.
5.
Delete node 72
root
52
.
parent
36
68
.
currentNode
24
59
72
.
.
70
75
80
2.
3.
4.
5.
Delete node 72
root
52
.
parent
36
68
.
currentNode
24
59
72
.
.
70
80
Inorder_suc
75
2.
3.
4.
5.
Delete node 72
root
52
.
parent
36
68
.
currentNode
24
59
72
.
.
70
80
Inorder_suc
75
2.
3.
4.
5.
Delete node 72
root
52
.
parent
36
68
.
currentNode
24
59
72
.
.
70
80
Inorder_suc
75
Inorder_suc
2.
3.
4.
5.
Delete node 72
root
52
.
parent
36
68
.
currentNode
24
59
72
75
.
.
70
80
75
Inorder_suc
2.
3.
4.
5.
Delete node 72
root
52
.
parent
36
68
.
currentNode
24
59
75
.
.
70
80
75
Inorder_suc
2.
3.
4.
5.
52
.
parent
36
68
.
currentNode
24
59
75
.
.
70
80
75
Summary
In this session, you learned that:
A tree is a nonlinear data structure that represents a
hierarchical relationship among the various data elements.
A binary tree is a specific type of tree in which each node can
have a maximum of two children.
Binary trees can be implemented by using arrays as well as
linked lists, depending upon requirement.
Traversal of a tree is the process of visiting all the nodes of the
tree once. There are three types of traversals, namely inorder,
preorder, and postorder traversal.
Binary search tree is a binary tree in which the value of the left
child of a node is always less than the value of the node, and
the value of the right child of a node is greater than the value of
the node.
Summary (Contd.)
Inserting a node in a binary search tree requires you to first
locate the appropriate position for the node to be inserted.
You need to check for the following three cases before deleting
a node from a binary search tree.
If the node to be deleted is the leaf node
If the node to be deleted has one child (left or right)
If the node to be deleted has two children