DS UNIT 2 notes
DS UNIT 2 notes
2) Edge
• The connecting link between any
two nodes is called as EDGE.
• A tree with n nodes will have n-1
edges
3.Parent node
▪ The node which is a predecessor
of any node is called as PARENT
NODE.
▪ Parent node can also be defined as
"The node which has child /
children".
4.Child node
▪ The node which is descendant of any
node is called as CHILD Node.
▪ i.e the node which has a link from its
parent node is called as child node.
▪ Any parent node can have any number of
child nodes.
5.Siblings
▪ Nodes which belong to same
Parent are called
as SIBLINGS
6.Leaf node
▪ The node which does not have a child is
called as LEAF Node
▪ Leaf node is also called as 'Terminal' node
or external node
7.Internal Nodes
▪ Nodes other than leaf nodes are called
as Internal Nodes.
▪ The root node is also said to be
Internal Node if the tree has more than
one node.
▪ Internal nodes are also called as 'Non-
Terminal' nodes.
8.Degree
10.Depth of a tree
▪ The total number of edges from root node to a particular node is called
as DEPTH of that Node.
▪ In a tree, depth of the root node is '0'
11.Height of a tree
• The total number of edges from leaf node to a particular node in the
longest path is called as height of a tree
• Height of the root node is said to be height of the tree.
• Height of all leaf nodes is '0'.
1. General tree
In the general tree, a node can have either 0 or
maximum n number of nodes. There is no
restriction imposed on the degree
FBT: Every node other than the CBT: Every level, except the last, is
leaves has two children. completely filled
4.Extended Binary Tree
▪ A binary tree can be converted into extended binary-tree by adding dummy
nodes to existing nodes wherever required.
▪ The binary tree to which external nodes are added is called as Extended
Binary Tree.
Representation of Trees
▪A binary tree is represented using two methods
1. Array Representation
1.Array Representation of BT
▪ In array representation of a binary tree, we use one-dimensional array (1-D
Array) to represent a binary tree.
Advantage:
▪ Easy access of the nodes
Dis-advantage:
▪ wastage of memory
2.Linked List Representation of BT
▪ A double linked list to represent a binary tree in which , every node consists
of three fields.
2.Linked List Representation of BT
▪ The structure defining a node of binary tree in C is as follows.
Struct node
{
struct node *lc ; // points to the left child
int data; // data field
struct node *rc; // points to the right child
}
Tree Traversals
• Traversal is a technique for visiting all of a tree's nodes and printing their
values.
• Traversing a tree involves iterating over all nodes in some manner
• We always start from the root (head) node since all nodes are connected by
edges (links).
• print the left most node of the left sub-tree i.e. 23.
• print the root of the left sub-tree i.e. 211.
• print the right child i.e. 89.
• print the root node of the tree i.e. 18.
• Then, move to the right sub-tree of the binary tree and print the left most node i.e.
10.
• print the root of the right sub-tree i.e. 20.
• print the right child i.e. 32.
• hence, the printing sequence will be 23, 211, 89, 18, 10, 20, 32.
int main()
//program to implement in-order traversal in C {
language struct node* root = createNode(40);
#include <stdio.h> root->left = createNode(30);
#include <stdlib.h> root->right = createNode(50);
struct node { root->left->left = createNode(25);
int element; root->left->right = createNode(35);
struct node* left; root->left->left->left = createNode(15);
struct node* right; root->left->left->right = createNode(28);
}; root->right->left = createNode(45);
/*To create a new node*/ root->right->right = createNode(60);
struct node* createNode(int val) root->right->right->left = createNode(55);
{ root->right->right->right = createNode(70);
struct node* Node = (struct node*)malloc(size printf("\n The Inorder traversal of given binary t
of(struct node)); ree is -\n");
Node->element = val; traverseInorder(root);
Node->left = NULL; return 0;
Node->right = NULL; }
return (Node);
}
/*function to traverse the nodes of binary tree in
Inorder*/
void traverseInorder(struct node* root)
{
if (root == NULL)
return;
traverseInorder(root->left);
printf(" %d ", root->element);
traverseInorder(root->right);
}
2. Pre-order Traversal :Root, Left , Right
▪ In preorder traversal, first, root node is visited, then left sub-tree
and after that right sub-tree is visited.
Algorithm: Preorder
Step 1: Repeat Steps 2 to 4 while TREE != NULL
Step 2: Write TREE -> DATA
Step 3: PREORDER(TREE -> LEFT)
Step 4: PREORDER(TREE -> RIGHT)
[END OF LOOP]
Step 5: END
C C push(C)
D CD push(D)
- pop(),perform + and push(-)
*
Threaded Binary Tree(TBT)
▪In a Threaded Binary Tree(TBT), NULL pointers are replaced
by references of other nodes in the tree.
▪These extra references are called as threads
Threaded Binary tree(TBT) contd..
▪ Threaded Binary Tree is also a binary tree in which all left child
pointers that are NULL points to its in-order predecessor, and all
right child pointers that are NULL points to its in-order successor.
Heap Data Structure –(Balanced Binary Tree)
▪ Heap is a special case of balanced binary tree data structure
where the root-node (key) is compared with its children and
arranged accordingly.
▪ Min-Heap
▪ Max-Heap
Operations on Heap
The following operations are performed on the heap
1. Insertion (max heap tree and min heap tree)
2. Deletion(max heap tree and min heap tree)
Operations on Heap- Insertion on Max Heap Tree
▪ To create the max heap tree, we need to consider the following two
cases:
▪ First, we have to insert the element in such a way that the property
of the complete binary tree must be maintained.
▪ Secondly, value(parent node) >= the either of its child.
Alg
▪ Step 1 - Insert the newNode as last leaf from left to right.
▪ Step 2 - Compare newNode value with its Parent node.
▪ Step 3 - If (newNode value > its parent) → swap both of them.
▪ Step 4 - Repeat step 2 and step 3 until newNode less than its parent node
(or) newNode reaches to root.
Operations on Heap- Insertion on Max Heap Tree
Create a max heap tree 44, 33, 77, 11, 55, 88, 66
ALG:
▪ Step 1 - Insert the newNode as last leaf from left to right.
▪ Step 2 - Compare newNode value with its Parent node.
▪ Step 3 - If (newNode value < its parent) → swap both of them.
▪ Step 4 - Repeat step 2 and step 3 until newNode less than its parent node
(or) newNode reaches to root.
Operations on Heap- Insertion on Min Heap Tree
Create a min heap tree 10, 40, 50, 5
Insert 0
Insert 50
Insert 40
Insert 5 Insert 5
Swap as node<parent Swap as node<parent
Deleting on Max Heap Tree
Step 1 − Remove root node.
Step 2 − Move the last element of last level to root.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If (parent < child), → swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.
Remove root node and make 14 as root Compare 14 with its children.14< 48 -> swap
Compare 14 with its children.14< 33 -> swap Compare 14 with its children.14< 26 -> swap
Deleting on Max Heap Tree
Deleting on Min Heap Tree
Step 1 − Remove root node.
Step 2 − Move the last element of last level to root.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If (parent > child), → swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.
Ex.