Ds Mod 3
Ds Mod 3
o It is used to store the data elements combined whenever they are not present
in the contiguous memory locations.
o It is an efficient way of organizing and properly holding the data.
o It reduces the wastage of memory space by providing sufficient memory to
every data element.
1
o Unlike in an array, we have to define the size of the array, and subsequent
memory space is allocated to that array; if we don't want to store the elements
till the range of the array, then the remaining memory gets wasted.
o So to overcome this factor, we will use the non-linear data structure and have
multiple options to traverse from one node to another.
o Data is stored randomly in memory.
o It is comparatively difficult to implement.
o Multiple levels are involved.
o Memory utilization is effective.
TREE
o The tree is a non-linear data structure that is comprised of various nodes. The
nodes in the tree data structure are arranged in hierarchical order.
2
o Unlike Arrays, Stack, Linked Lists, and queues, which are linear data
structures, trees are hierarchical.
BINARY TREE
3
• It is a very important subcategory of simple trees. As the name suggests, we
can easily predict that the binary tree is consists of two children only.
• A binary tree comprises nodes that can have two children, as described by the
word "binary," which means "two numbers." Any node can have a maximum
of 0, 1, or 2 nodes in a binary tree.
• A tree whose elements have at most 2 children is called a binary tree. Since
each element in a binary tree can have only 2 children, we typically name
them the left and right children. They are most commonly used in data
structures for two reasons:
For obtaining nodes and categorizing them, as observed in Binary Search Trees.
4
5. Siblings: The nodes having the same parent node is called siblings of each
other. Same ancestors are not siblings, and only the parent node must be the
same, defined as siblings. For example, if node A has two child nodes, B and
C, B and C are called siblings.
6. Leaf node: The leaf node is the node that does not have any child nodes. It is
the termination point of any tree. It is also known as External nodes.
7. Degree of a node: The degree of a node is defined as the number of children
nodes. The degree of the leaf node is always 0 because it does not have any
children, and the internal node always has atleast one degree, as it contains
atleast one child node.
8. Height of the tree: The tree's height is defined as the distance of the root node
to the leaf node present at the last level is called the height of the tree. In other
words, height is the maximum level upto which the tree is extended.
9. Descendant – A node reachable by repeated proceeding from parent to child.
10. Ancestor – A node reachable by repeated proceeding from child to parent.
11. Internal node – A node with at least one child.
12. External node – A node with no children.
13. Degree of a tree– Number of sub trees of a node.
14. Path – A sequence of nodes and edges connecting a node with a descendant.
15. Path length- Total number of nodes in that path.
16. Level – The level of a node is defined by 1 + (the number of connections
between the node and the root).
17. Height of node – The height of a node is the number of edges on the longest
downward path between that node and a leaf.
18. Depth of a node– The depth of a node is the number of edges from the node
to the tree's root node.
5
19. Depth of a tree-The total number of edges from the root node to the
leaf node in the longest path is known as "Depth of Tree".
20. Forest – A forest is a set of n ≥ 0 disjoint trees.
A full Binary tree is a special type of binary tree in which every parent
node/internal node has either two or no children.
A perfect binary tree is a type of binary tree in which every internal node has
exactly two child nodes and all the leaf nodes are at the same level.
6
A complete binary tree is just like a full binary tree, but with two major differences
3. The last leaf element might not have a right sibling i.e. a complete binary tree
doesn't have to be a full binary tree.
A degenerate or pathological tree is the tree having a single child either left or
right.
7
A skewed binary tree is a pathological/degenerate tree in which the tree is either
dominated by the left nodes or the right nodes. Thus, there are two types of skewed
binary tree: left-skewed binary tree and right-skewed binary tree.
It is a type of binary tree in which the difference between the height of the left and
the right subtree for each node is either 0 or 1.
Eg:
8
Example of Balanced and Unbalanced Binary
A binary tree data structure is represented using two methods. Those methods are as
follows...
1. Array Representation
2. Linked List Representation
9
1. Array Representation of Binary Tree
To represent a binary tree of depth 'n' using array representation, we need one
dimensional array with a maximum size of 2n + 1.
We use a double linked list to represent a binary tree. In a double linked list, every
node consists of three fields. First field for storing left child address, second for
storing actual data and third for storing right child address.
In this linked list representation, a node has the following structure...
10
The above example of the binary tree represented using Linked list representation is
shown as follows...
A binary search tree follows some order to arrange the elements. In a Binary search
tree, the value of left node must be smaller than the parent node, and the value of
right node must be greater than the parent node. This rule is applied recursively to
the left and right subtrees of the root.
11
o As compared to array and linked lists, insertion and deletion operations are
faster in BST.
In a binary search tree, all the nodes in the left subtree of any node contains smaller
values and all the nodes in the right subtree of any node contains larger values as
shown in the following figure...
Example
The following tree is a Binary Search Tree. In this tree, left subtree of every node
contains nodes with smaller values and right subtree of every node contains larger
values.
12
Every binary search tree is a binary tree but every binary tree need not to be
binary search tree.
1. Search
2. Insertion
3. Deletion
4. Traversal
Now, let's understand the searching in binary tree using an example. We are taking
the binary search tree formed above. Suppose we have to find node 20 from the
below tree.
Step1:
Step2:
14
Step3:
Now, let's see the algorithm to search an element in the Binary search tree.
15
2. Insertion Operation in BST
In binary search tree, new node is always inserted as a leaf node. The insertion
operation is performed as follows...
Example 1:
10,12,5,4,20,8,7,15 and 13
16
17
Example 2:
A new key in BST is always inserted at the leaf. To insert an element in BST, we
have to start searching from the root node; if the node to be inserted is less than the
root node, then search for an empty location in the left subtree. Else, search for the
empty location in the right subtree and insert the data. Insert in BST is similar to
searching, as we always have to maintain the rule that the left subtree is smaller than
the root, and right subtree is larger than the root.
Now, let's see the process of inserting a node into BST using an example.
18
3. Deletion Operation in BST
In a binary search tree, the deletion operation is performed with O(log n) time
complexity. Deleting a node from Binary search tree includes following three
cases...
• It is the simplest case to delete a node in BST. Here, we have to replace the
leaf node with NULL and simply free the allocated space.
• We can see the process to delete a leaf node from BST in the below image. In
below image, suppose we have to delete node 90, as the node to be deleted is
a leaf node, so it will be replaced with NULL, and the allocated space will
free.
19
•
We use the following steps to delete a node with one child from BST...
• In this case, we have to replace the target node with its child, and then delete
the child node. It means that after replacing the target node with its child node,
the child node will now contain the value to be deleted. So, we simply have
to replace the child node with NULL and free up the allocated space.
• We can see the process of deleting a node with one child from BST in the
below image. In the below image, suppose we have to delete the node 79, as
the node to be deleted has only one child, so it will be replaced with its child
55.
• So, the replaced node 79 will now be a leaf node that can be easily deleted.
20
•
We use the following steps to delete a node with two children from BST...
This case of deleting a node in BST is a bit complex among other two cases. In such
a case, the steps to be followed are listed as follows -
21
o After that, replace that node with the inorder successor until the target node is
placed at the leaf of tree.
o And at last, replace the node with NULL and free up the allocated space.
The inorder successor is required when the right child of the node is not empty. We
can obtain the inorder successor by finding the minimum element in the right child
of the node.
We can see the process of deleting a node with two children from BST in the below
image. In the below image, suppose we have to delete node 45 that is the root node,
as the node to be deleted has two children, so it will be replaced with its inorder
successor. Now, node 45 will be at the leaf of the tree so that it can be deleted easily.
4. Traversal
22
It is a process to visit all the nodes of a tree and may print their values too. Because,
all nodes are connected via edges (links) we always start from the root (head) node.
That is, we cannot randomly access a node in a tree. There are three ways which we
use to traverse a tree −
• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree or to
print all the values it contains.
1. In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the
right sub-tree. We should always remember that every node may represent a subtree
itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an
ascending order.
23
We start from A, and following in-order traversal, we move to its left subtree B. B is
also traversed in-order. The process goes on until all the nodes are visited. The output
of inorder traversal of this tree will be −
D→B→E→A→F→C→G
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.
2. Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and finally
the right subtree.
We start from A, and following pre-order traversal, we first visit A itself and then
move to its left subtree B. B is also traversed pre-order. The process goes on until
all the nodes are visited. The output of pre-order traversal of this tree will be −
A→B→D→E→C→F→G
24
Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
3. Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we
traverse the left subtree, then the right subtree and finally the root node.
We start from A, and following Post-order traversal, we first visit the left
subtree B. B is also traversed post-order. The process goes on until all the nodes are
visited. The output of post-order traversal of this tree will be −
D→E→B→F→G→C→A
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
25
EXPRESSION TREE
• An expression tree is a special type of binary tree that is used to store
algebraic expressions.
X = (a + b) - (c * d)
26
Use of Expression tree
An expression tree can be constructed with the help of a stack. Repeat the
following steps for every character in the postfix expression:
ab-c*
Since the first two characters are operands, PUSH it onto the stack.
When the - symbol is read, a and b are popped from the stack and is added as the
child of node - .
27
A pointer to the new node is now pushed onto the stack.
Finally, the operator * is read, so we pop c and - from the stack and add it as the
child of node * .
When the operation is completed, the pointer to the root of three remains on the
stack.
28
THREADED BINARY TREE
In the linked representation of binary trees, more than one half of the link fields
contain NULL values which results in wastage of storage space. If a binary tree
consists of n nodes then n+1 link fields contain NULL values. Many nodes present
in this tree hold a NULL value in their left or right child pointer (Denoted by
sketched fields). The space occupied by these null values can be used to store some
kind of valuable information.
• One possible way to utilize this space is to have a special pointer that points
to nodes higher in the tree (i.e. ancestors).
• These special pointers are called threads, and the binary tree having such
pointers is called a threaded binary tree.
• A Threaded Binary Tree is a variant of a normal Binary Tree that facilitates
faster tree traversal and does not require a Stack or Recursion. It decreases
the memory wastage by setting the null pointers of a leaf node to the in-
order predecessor or in-order successor
What is a Threaded Binary tree?
• In a Threaded Binary Tree, the nodes will store the in-order
predecessor/successor instead of storing NULL in the left/right child
pointers.
• So the basic idea of a threaded binary tree is that for the nodes whose right
pointer is null, we store the in-order successor of the node (if-exists), and for
the nodes whose left pointer is null, we store the in-order predecessor of the
node(if-exists).
29
• One thing to note is that the leftmost and the rightmost child pointer of a tree
always points to null as their in-order predecessor and successor do not
exist.
Types of Threaded Binary tree
There are two types of Threaded Binary Trees:
• Single-Threaded Binary Tree
• Double-Threaded Binary Tree
• In one-way threaded binary trees, a thread will appear either in the right or left
link field of a node.
• If it appears in the right link field of a node then it will point to the next node
that will appear on performing in order traversal. Such trees are called Right
threaded binary trees.
• If thread appears in the left field of a node then it will point to the nodes
inorder predecessor. Such trees are called Left threaded binary trees.
30
• Left threaded binary trees are used less often as they don't yield the last
advantages of right threaded binary trees.
• In one-way threaded binary trees, the right link field of last node and left link
field of first node contains a NULL.
• In order to distinguish threads from normal links they are represented by
dotted lines.
The above figure shows the inorder traversal of this binary tree yields D, B, E, A, C,
F. When this tree is represented as a right threaded binary tree, the right link field of
leaf node D which contains a NULL value is replaced with a thread that points to
node B which is the inorder successor of a node D. In the same way other nodes
containing values in the right link field will contain NULL value.
31
Two-way threaded Binary Trees:
In two-way threaded Binary trees, the right link field of a node containing NULL
values is replaced by a thread that points to nodes inorder successor and left field of
a node containing NULL values is replaced by a thread that points to nodes inorder
predecessor.
The above figure shows the inorder traversal of this binary tree yields D, B, E, G, A,
C, F. If we consider the two-way threaded Binary tree, the node E whose left field
contains NULL is replaced by a thread pointing to its inorder predecessor i.e. node
32
B. Similarly, for node G whose right and left linked fields contain NULL values are
replaced by threads such that right link field points to its inorder successor and left
link field points to its inorder predecessor. In the same way, other nodes containing
NULL values in their link fields are filled with threads.
• In the above figure of two-way threaded Binary tree, we noticed that no left
thread is possible for the first node and no right thread is possible for the last
node.
• This is because they don't have any inorder predecessor and successor
respectively. This is indicated by threads pointing nowhere. So in order to
maintain the uniformity of threads, we maintain a special node called
the header node.
• The header node does not contain any data part and its left link field points
to the root node and its right link field points to itself.
• If this header node is included in the two-way threaded Binary tree then this
node becomes the inorder predecessor of the first node and inorder successor
of the last node.
• Now threads of left link fields of the first node and right link fields of the
last node will point to the header node.
33