Professional Documents
Culture Documents
Objectives
Directory structure
Consider a scenario where you are required to represent the
directory structure of your operating system.
The directory structure contains various folders and files. A
folder may further contain any number of sub folders and
files.
In such a case, it is not possible to represent the structure
linearly because all the items have a hierarchical
relationship among themselves.
In such a case, it would be good if you have a data structure
that enables you to store your data in a nonlinear fashion.
A tree are
Trees is a used
nonlinear data structure
in applications that represent
in which a between
the relation
hierarchical
data elements relationship among
needs to be the various
represented in adata elements.
hierarchy.
B C D
E F G H I J K
L M
Eachtopmost
The elementnode
in a tree
in a tree
is referred
is called
to root.
as a node.
root
B C D
E F G H I J K
L M node
B C D
E F G H I J K
L M node
Leafusnode:
Let It refers
discuss to aterms
various nodethat
withare
nomost
children.
frequently used
with trees.
A
Nodes E, F, G, H, I, J,
L, and M are leaf
B C D nodes.
E F G H I J K
L M
Subtree: A
Children ofportion
a node:ofThe
a tree, which
roots can
of the be viewed
subtrees of aas a are
node
separate
called thetree in itself
children is called
of the node.a subtree.
A subtree can also contain just one node called the leaf node.
A
E, F, with
Tree G, and
rootHB,
are
children of nodes
containing node B.E,BF,
B C D is the
G, andparent
H is aof these of
subtree
nodes.
node A.
E F G H I J K
L M
DegreeA of
Edge: linka from
node:the
It refers
parenttotothe number
a child nodeofissubtrees
referredof
toaas
node
an in a tree.
edge.
A Degree of node C is 1
Degree of node D is 2
B C D Degree of node A is 3
Edge Degree of node B is 4
E F G H I J K
L M
E F G H I J K
L M
Internal
Level of node: It refers
a node: to to
It refers anythenode between
distance the rootofand a
(in number
leaf node.
nodes) of a node from the root. Root always lies at level 0.
As you move down the tree, the level increases by one.
Level 0
A
Nodes B, C, D, and K
are internal nodes. Level 1
B C D
E F G H I J K Level 2
L M Level 3
Level 0
A
Level 1
B C D
E F G H I J K Level 2
L M Level 3
D E F G
H I
Answer:
a. 4
b. D and E
c. C
d. 2
e. H does not have any siblings
f. The only sibling of D is E
g. F, G, H, and I
Binary
Strictly tree is atree:
binary specific type of tree in which each node can
have at most
A binary two
tree in children namely
which every node, left child
except forand
the right child.
leaf nodes,
hasare
There non-empty
variousleft and of
types right children.
binary trees:
Strictly binary tree
Full binary tree
Complete binary tree
A
Depth = 3
Total number of
3
nodes = 2 1 = 7
B C
D E F G
A A
B C B C
D E F D E G
Data
Node
root root
. 52 .
52
36 68 . 36 . 68 .
24 59 72 24 59 . 72 .
70 80 . 70 80
B is
The left subtree of node A is not
not NULL.
NULL.
Therefore, move to node D B to traverse the left
B.
subtree of A.
root
B C
D E F G
I
H
B C
D E F G
I
H
D
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
B C
D E F G
I
H
D
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A F
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A F
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A F C
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A F C
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A F C
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A F C I
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A F C I
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
Visit node G.
root
B C
D E F G
I
H
D H B E A F C I G
Ver. 1.0 Session 13
Data Structures and Algorithms
Inorder Traversal (Contd.)
root
B C
Traversal complete
D E F G
I
H
D H B E A F C I G
Ver. 1.0 Session 13
Data Structures and Algorithms
Preorder Traversal
B C
D E F G
I
H
Preorder Traversal: A B D H E C F G I
B C
D E F G
I
H
Postorder Traversal: H D E B F I G C A
Answer:
Preorder
Which datayou
Therefore, structure
need to willimplement
you use toa store the id of the
data structure that
customers?
provides the advantages of both arrays as well as linked
lists.Can you implement an array?
A binarySearch operation
search in an array is
tree combines thefast.
advantages of both
arrays andHowever,
linkedinsertion
lists. and deletion in an array is complex in nature.
In this case, the total number of customer ids to be stored is very
large. Therefore, insertion and deletion will be very time
consuming.
Can you implement a linked list?
Insert and delete operation in a linked is fast.
However, linked lists allow only sequential search.
If you need to access a particular customer id, which is located
near the end of the list, then it would require you to visit all the
preceding nodes, which again can be very time consuming.
The following
Binary search is an is
tree example
a binaryoftree
a binary search
in which tree.
every node
satisfies the following conditions:
All values in the left subtree of a node are less than the value
of the node. 52
All values in the right subtree of a node are greater than the
value of the
36 node. 68
24 44 59 72
40 55
Search
To operation
search refers value,
for a specific to the you
process
needoftosearching for a
perform the
specified steps:
following value in the tree.
1. Make currentNode point to the root node
2. If currentNode is null:
a. Display Not Found
b. Exit
3. Compare the value to be searched with the value of currentNode.
Depending on the result of the comparison, there can be three
possibilities:
a. If the value is equal to the value of currentNode:
i. Display Found
ii. Exit
b. If the value is less than the value of currentNode:
i. Make currentNode point to its left child
ii. Go to step 2
c. If the value is greater than the value of currentNode:
i. Make currentNode point to its right child
ii. Go to step 2
Answer:
smaller
Beforeaninserting
Write a node
algorithm in a the
to locate binary search
position of tree,
a newyou firstto be
node
need to check
inserted whether
in a binary the tree
search tree.is empty or not.
If the tree is empty, make the new node as root.
If the tree is not empty, you need to locate the appropriate
position for the new node to be inserted.
This requires you to locate the parent of the new node to be
inserted.
Once the parent is located, the new node is inserted as the
left child or right child of the parent.
To locate the parent of the new node to be inserted, you
need to implement a search operation in the tree.
24 59 . 72 .
. 70 80
24 59 . 72 .
. 70 80
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
. 70 80
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
. 70 80
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
. 70 80
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
. 70 80
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
. 70 80
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
. 70 80
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
. 70 80
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
. 70 80
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
. 70 80
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
. 70 80
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
currentNode
. 70 80
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
currentNode
. 70 80
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
currentNode
. 70 80
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
currentNode
. 70 80
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
currentNode
currentNode = NULL
. 70 80
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
currentNode = NULL
. 70 80
24 59 . 72 .
currentNode = NULL
. 70 80
24 59 . 72 . b. Exit
24 59 . 72 . b. Exit
24 59 . 72 . b. Exit
24 59 . 72 . b. Exit
24 59 . 72 . b. Exit
24 59 . 72 . b. Exit
24 59 . 72 . b. Exit
24 . 59 . 72 . b. Exit
24 59 . 72 . b. Exit
Deletean
Write operation
algorithminto
a locate
binary the
search tree refers
position to theto
of the node
process from
deleted of deleting thesearch
a binary specified
tree.node from the tree.
Before implementing a delete operation, you first need to
locate the position of the node to be deleted and its parent.
To locate the position of the node to be deleted and its
parent, you need to implement a search operation.
69
69
69
69
69
69
69
69
69
69
69
69
69
69
69
69
currentNode
69
currentNode
69
node from the binary tree. 2. If currentNode is the root node: // If parent is
// NULL
69
parent
69
currentNode
Ver. 1.0 Session 13
Data Structures and Algorithms
Deleting Nodes from a Binary Search Tree (Contd.)
parent
69
currentNode
Ver. 1.0 Session 13
Data Structures and Algorithms
Deleting Nodes from a Binary Search Tree (Contd.)
parent
69
currentNode
Ver. 1.0 Session 13
Data Structures and Algorithms
Deleting Nodes from a Binary Search Tree (Contd.)
parent
69
currentNode
Ver. 1.0 Session 13
Data Structures and Algorithms
Deleting Nodes from a Binary Search Tree (Contd.)
parent
69
currentNode
Ver. 1.0 Session 13
Data Structures and Algorithms
Deleting Nodes from a Binary Search Tree (Contd.)
parent
69
Delete operation complete
currentNode
Ver. 1.0 Session 13
Data Structures and Algorithms
Deleting Nodes from a Binary Search Tree (Contd.)
75
75
75
Inorder_suc
Ver. 1.0 Session 13
Data Structures and Algorithms
Deleting Nodes from a Binary Search Tree (Contd.)
24 59 . 72
75 . 4. If the node marked Inorder_suc is a leaf node:
a. Delete the node marked Inorder_suc
by using the algorithm for Case I.
75
Inorder_suc
Ver. 1.0 Session 13
Data Structures and Algorithms
Deleting Nodes from a Binary Search Tree (Contd.)
75
Inorder_suc
Ver. 1.0 Session 13
Data Structures and Algorithms
Deleting Nodes from a Binary Search Tree (Contd.)
75
Delete operation complete
Inorder_suc
Ver. 1.0 Session 13
Data Structures and Algorithms
Activity: Implementing a Binary Search Tree
Problem Statement:
Write a program to implement insert and traverse operations
on a binary search tree that contains the words in a dictionary.