SlideShare a Scribd company logo
CSC303: Data Structures and Algorithms
Audience: B. Tech. (CSE), Semester III
Instructor: Dr. Mamata Dalui
The study materials/presentations used in this course are solely meant for academic
purposes and collected from different available course materials, slides, books, etc.
Assistant Professor
Department of Computer Science and Engineering
National Institute of Technology Durgapur
Trees (Part II)
Lemma 1 [Maximum number of nodes]: The maximum number of
nodes at level l of a binary tree is 2l, for l 0.
Proof: The proof of this lemma can be done by induction on l.
The only node at level l = 0 is the root node. Hence, the maximum
number of nodes at level l = 0 is 2l =20 =1.
Let assume the above holds true for all i, 0 ≤ i < l. That is, the
maximum number of nodes at level i is 2i. As any node in the
binary tree has degree 2, so for each node at level i, there may be at
most 2 nodes at level i+1. Therefore, the maximum number of nodes
at level i+1 is 2 × 2i = 2i+1.
So, the formula is true for i+1, if it is true for i. Hence, the proof.
Properties of Binary Trees
Lemma 2 [Maximum number of nodes]: The maximum number of nodes in a binary
tree of height h is 2h - 1.
Proof: A binary tree can have maximum number of nodes when all the levels are
having maximum number of nodes.
Therefore, the maximum number of nodes in a binary tree of height h is
However, by definition, height h = kmax+1.
So, maximum number of nodes nmax = 2h-1.
Also, given the maximum number of nodes nmax , the height h can be calculated as
ℎ = ⌈𝑙𝑜𝑔2 𝑛𝑚𝑎𝑥+1 ⌉
Properties of Binary Trees
𝑛 = 2𝑖
𝑘𝑚𝑎𝑥
𝑖=0
=
2𝑘𝑚𝑎𝑥 +1
− 1
2 − 1
=2𝑘𝑚𝑎𝑥 +1
−1 , where kmax is the maximum level of the tree.
Lemma 3 [Minimum number of nodes]: The minimum number of nodes in
a binary tree of height h is h.
Proof: A binary tree can have minimum number of nodes when all the
levels are having minimum number of nodes. The minimum number of
nodes appears when each parent has only one child and occurs for a skewed
tree. Therefore, a skewed binary tree of height h with only one path
contains h number of nodes. Hence, nmin = h.
Properties of Binary Trees
Lemma 4 [Relation between number of leaf nodes and degree-2 nodes]: For any nonempty
binary tree, if n is the number of nodes and e is the number of edges, then n = e + 1.
Proof: Let n be the number of vertices in a binary tree.
If n=1, then the number of edges=0.
If n=2 then the number of edges=1.
If n=3 then the number of edges=2.
If n=n’ then the number of edges = n’-1.
Suppose, the above is true for any number of nodes n’. Hence, n’=e’+1.
Now, we will try to prove that this formula holds true for n’+1.
Thus, n’+1 = (e’+1) + 1
or, n’+1 = ((n’ – 1) + 1)+ 1, where e’= n’-1
or, n’ + 1 = ((n’ + 1) – 1) + 1
Hence, it implies that if the formula is true for any n, then it is also true for n+1. Therefore, n =
e + 1.
Properties of Binary Trees
Lemma 5 [Relation between number of leaf nodes and degree-2 nodes]: For
any nonempty binary tree, if n0 is the number of leaf nodes and n2 is the
number of nodes with degree 2, then n0 = n2 + 1.
Proof: Suppose, there are a total of n number of nodes in the binary tree, and ni
is the number of nodes with degree i, 0 ≤ i ≤ 2.
Therefore, n = n0+n1+n2, since for a binary tree no other kinds of nodes are
possible.
If e is the total number of edges in the binary tree, then
e = n0×0+n1×1+n2×2
or, e = n1+2n2
Also, we have, n = e+1 (from Lemma 4)
So, we can write, n = 1+ n1+2n2
Hence, n0+n1+n2 = 1+n1+2n2
or, n0 = n2 + 1
Hence the proof.
Properties of Binary Trees
Array representation
If a complete binary tree with n nodes is represented sequentially,
then for any node with index i, 0 < i  n-1, we have
1. parent(i) is at (i-1)/2 if i  0.
If i = 0, i is at the root and has no parent.
2. LeftChild(i) is at 2i +1 if 2i +1  n-1.
If 2i  n-1, then i has no left child.
3. RightChild(i) is at 2i+2 if 2i+2  n-1.
If 2i +1  n-1, then i has no right child
[0] [1] [2] [3] [4] [5] [6]
A B C — D — E
Level 0
Level 1 Level 2
A
B
D
C
E
0
1 2
3 4 5 6
Representation of Binary Trees
Array representation
• Wastage of spaces: In the worst case, a skewed tree of height(depth) k requires 2k-1
spaces. Of these, only k spaces will actually be used.
• Insertion or deletion of nodes from the middle of a tree requires the movement of
potentially many nodes to reflect the change in the level of these nodes.
Representation of Binary Trees
A B C D E F G
BT[0] BT[1] BT[2] BT[3] BT[4] BT[5] BT[6]
A
B C
D E F G
0
1 2
3 4 5 6
Array representation
• Wastage of spaces: In the worst case, a skewed tree of height (depth) k requires 2k-1
spaces. Of these, only k spaces will actually be used.
• Insertion or deletion of nodes from the middle of a tree requires the movement of
potentially many nodes to reflect the change in the level of these nodes.
Representation of Binary Trees
A B - C - - -
BT[0] BT[1] BT[2] BT[3] BT[4] BT[5] BT[6]
A
B
C
0
1
3
Linked representation
• Overcomes the shortcomings of array representation.
• The node structure is as shown below. The Data part stores the information and the
two pointers lchild and rchild stores the addresses of the left child and right child.
• Given the pointer to the root node, any other node can be accessed.
Representation of Binary Trees
lchild data rchild
Structure of a node
Linked representation
Representation of Binary Trees
A
B C
D E F G
Linked representation of a complete binary tree
struct node
{
int data;
struct node *lchild, *rchild;
};
typedef struct node NODE;
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
Representation of Binary Trees
Pros and Cons of Array (Sequential) Representation of Binary Trees
• Any node can be accessed from any other node just by calculating the corresponding
index. From execution point of view, this is efficient.
• Only data is stored, but no pointers to successor or predecessor. Successor or predecessor
of a node is implied by their indexing.
• Programming languages that doesn’t support dynamic memory allocation (such as
FORTRAN, BASIC, etc.) array representation is the only way to store a tree.
• Except complete binary tree, for all other binary trees most of the entries in the array
representation remain empty.
• Only static representation is allowed. The tree size is limited by the size of the array.
• Insertion of nodes into the tree and deletion of nodes from the tree are inefficient as they
involve a considerable amount of data movement incurring excessive processing time.
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
Operations on Binary Trees
The primary operations on a binary tree are -
• Creation – to create a binary tree
• Insertion – to include a new node into an existing (may be empty)
binary tree
• Deletion – to delete a node from a non-empty binary tree
• Search – to search for a particular node in the binary tree
• Traversal – to traverse the nodes of the binary tree
• Merging – to merge two binary trees into a single
Implementation of Binary Trees
Creation of Binary Tree
Algorithm CreateBinaryTtree
Input: Item is the data of the node with pointer ptr
Output: A binary tree with two subtrees of node pointed by
ptr
Data structure: Linked list structure of binary tree
CreateBinaryTree(ptr, item)
1. if ptr!= NULL then
2. ptr->data = item
3. Do the Node has left subtree (Give choice = Y/N)?
4. if (choice = = Y) then
5. lcptr=GetNode(NODE)
6. ptr->lchild = lcptr
7. CreateBinaryTree(lcptr, NewItemLC); /*Create
subtree with the item NewItemLC */
8. else
9. lcptr = NULL
10. prt ->lchild = NULL
11. CreateBinaryTree(lcptr, NULL); //Empty left subtree
12. endif
13. Do the Node has right subtree (Give choice =
Y/N)?
14. if (choice = = Y) then
15. rcptr=GetNode(NODE)
16. ptr->rchild = rcptr
17. CreateBinaryTree(rcptr, NewItemRC); /*Create
subtree with the item NewItemRC */
18. else
19. rcptr = NULL
20. prt ->rchild = NULL
21. CreateBinaryTree(rcptr, NULL); /*Empty
right subtree*/
22. endif
23. endif
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
CreateBinaryTree(ptr, A)
Operations on Binary Trees
CreateBinaryTree(ptr, item)
1. if ptr!= NULL then
2. ptr->data = item
3. Do the Node has left subtree (Give choice = Y/N)?
4. if (choice = = Y) then
5. lcptr=GetNode(NODE)
6. ptr->lchild = lcptr
7. CreateBinaryTree(lcptr, NewItemLC); /*Create subtree with
the item NewItemLC */
8. else
9. lcptr = NULL
10. prt ->lchild = NULL
11. CreateBinaryTree(lcptr, NULL); //Empty left subtree
12. endif
13. Do the Node has right subtree (Give choice = Y/N)?
14. if (choice = = Y) then
15. rcptr=GetNode(NODE)
16. ptr->rchild = rcptr
17. CreateBinaryTree(rcptr, NewItemRC); /*Create subtree with
the item NewItemRC */
18. else
19. rcptr = NULL
20. prt ->rchild = NULL
21. CreateBinaryTree(rcptr, NULL); /*Empty right subtree*/
22. endif
23. endif
ptr
A
ptr
lcptr
ptr->lchild ptr->rchild
Creation of Binary Tree
Choice of left subtree=Y
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
CreateBinaryTree(ptr, A)
Operations on Binary Trees
CreateBinaryTree(ptr, item)
1. if ptr!= NULL then
2. ptr->data = item
3. Do the Node has left subtree (Give choice = Y/N)?
4. if (choice = = Y) then
5. lcptr=GetNode(NODE)
6. ptr->lchild = lcptr
7. CreateBinaryTree(lcptr, NewItemLC); /*Create subtree with
the item NewItemLC */
8. else
9. lcptr = NULL
10. prt ->lchild = NULL
11. CreateBinaryTree(lcptr, NULL); //Empty left subtree
12. endif
13. Do the Node has right subtree (Give choice = Y/N)?
14. if (choice = = Y) then
15. rcptr=GetNode(NODE)
16. ptr->rchild = rcptr
17. CreateBinaryTree(rcptr, NewItemRC); /*Create subtree with
the item NewItemRC */
18. else
19. rcptr = NULL
20. prt ->rchild = NULL
21. CreateBinaryTree(rcptr, NULL); /*Empty right subtree*/
22. endif
23. endif
ptr
A
ptr
lcptr
CreateBinaryTree(lcptr, B)
ptr->lchild ptr->rchild
Creation of Binary Tree
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
CreateBinaryTree(ptr, A)
Operations on Binary Trees
CreateBinaryTree(ptr, item)
1. if ptr!= NULL then
2. ptr->data = item
3. Do the Node has left subtree (Give choice = Y/N)?
4. if (choice = = Y) then
5. lcptr=GetNode(NODE)
6. ptr->lchild = lcptr
7. CreateBinaryTree(lcptr, NewItemLC); /*Create subtree with
the item NewItemLC */
8. else
9. lcptr = NULL
10. prt ->lchild = NULL
11. CreateBinaryTree(lcptr, NULL); //Empty left subtree
12. endif
13. Do the Node has right subtree (Give choice = Y/N)?
14. if (choice = = Y) then
15. rcptr=GetNode(NODE)
16. ptr->rchild = rcptr
17. CreateBinaryTree(rcptr, NewItemRC); /*Create subtree with
the item NewItemRC */
18. else
19. rcptr = NULL
20. prt ->rchild = NULL
21. CreateBinaryTree(rcptr, NULL); /*Empty right subtree*/
22. endif
23. endif
ptr
A
ptr
B
lcptr
CreateBinaryTree(lcptr, B)
ptr->lchild ptr->rchild
Creation of Binary Tree
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
CreateBinaryTree(ptr, A)
Operations on Binary Trees
CreateBinaryTree(ptr, item)
1. if ptr!= NULL then
2. ptr->data = item
3. Do the Node has left subtree (Give choice = Y/N)?
4. if (choice = = Y) then
5. lcptr=GetNode(NODE)
6. ptr->lchild = lcptr
7. CreateBinaryTree(lcptr, NewItemLC); /*Create subtree with
the item NewItemLC */
8. else
9. lcptr = NULL
10. prt ->lchild = NULL
11. CreateBinaryTree(lcptr, NULL); //Empty left subtree
12. endif
13. Do the Node has right subtree (Give choice = Y/N)?
14. if (choice = = Y) then
15. rcptr=GetNode(NODE)
16. ptr->rchild = rcptr
17. CreateBinaryTree(rcptr, NewItemRC); /*Create subtree with
the item NewItemRC */
18. else
19. rcptr = NULL
20. prt ->rchild = NULL
21. CreateBinaryTree(rcptr, NULL); /*Empty right subtree*/
22. endif
23. endif
ptr
A
ptr
B
lcptr
ptr->lchild ptr->rchild
Choice of left subtree=N
Creation of Binary Tree
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
CreateBinaryTree(ptr, A)
Operations on Binary Trees
CreateBinaryTree(ptr, item)
1. if ptr!= NULL then
2. ptr->data = item
3. Do the Node has left subtree (Give choice = Y/N)?
4. if (choice = = Y) then
5. lcptr=GetNode(NODE)
6. ptr->lchild = lcptr
7. CreateBinaryTree(lcptr, NewItemLC); /*Create subtree with
the item NewItemLC */
8. else
9. lcptr = NULL
10. prt ->lchild = NULL
11. CreateBinaryTree(lcptr, NULL); //Empty left subtree
12. endif
13. Do the Node has right subtree (Give choice = Y/N)?
14. if (choice = = Y) then
15. rcptr=GetNode(NODE)
16. ptr->rchild = rcptr
17. CreateBinaryTree(rcptr, NewItemRC); /*Create subtree with
the item NewItemRC */
18. else
19. rcptr = NULL
20. prt ->rchild = NULL
21. CreateBinaryTree(rcptr, NULL); /*Empty right subtree*/
22. endif
23. endif
ptr
A
ptr
B
lcptr
ptr->lchild ptr->rchild
Choice of right subtree=Y
Creation of Binary Tree
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
CreateBinaryTree(ptr, A)
Operations on Binary Trees
CreateBinaryTree(ptr, item)
1. if ptr!= NULL then
2. ptr->data = item
3. Do the Node has left subtree (Give choice = Y/N)?
4. if (choice = = Y) then
5. lcptr=GetNode(NODE)
6. ptr->lchild = lcptr
7. CreateBinaryTree(lcptr, NewItemLC); /*Create subtree with
the item NewItemLC */
8. else
9. lcptr = NULL
10. prt ->lchild = NULL
11. CreateBinaryTree(lcptr, NULL); //Empty left subtree
12. endif
13. Do the Node has right subtree (Give choice = Y/N)?
14. if (choice = = Y) then
15. rcptr=GetNode(NODE)
16. ptr->rchild = rcptr
17. CreateBinaryTree(rcptr, NewItemRC); /*Create subtree with
the item NewItemRC */
18. else
19. rcptr = NULL
20. prt ->rchild = NULL
21. CreateBinaryTree(rcptr, NULL); /*Empty right subtree*/
22. endif
23. endif
ptr
A
ptr
B
lcptr
ptr->lchild ptr->rchild
Choice of right subtree=Y
rcptr
Creation of Binary Tree
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
CreateBinaryTree(ptr, A)
Operations on Binary Trees
CreateBinaryTree(ptr, item)
1. if ptr!= NULL then
2. ptr->data = item
3. Do the Node has left subtree (Give choice = Y/N)?
4. if (choice = = Y) then
5. lcptr=GetNode(NODE)
6. ptr->lchild = lcptr
7. CreateBinaryTree(lcptr, NewItemLC); /*Create subtree with
the item NewItemLC */
8. else
9. lcptr = NULL
10. prt ->lchild = NULL
11. CreateBinaryTree(lcptr, NULL); //Empty left subtree
12. endif
13. Do the Node has right subtree (Give choice = Y/N)?
14. if (choice = = Y) then
15. rcptr=GetNode(NODE)
16. ptr->rchild = rcptr
17. CreateBinaryTree(rcptr, NewItemRC); /*Create subtree with
the item NewItemRC */
18. else
19. rcptr = NULL
20. prt ->rchild = NULL
21. CreateBinaryTree(rcptr, NULL); /*Empty right subtree*/
22. endif
23. endif
ptr
A
ptr
B
lcptr
ptr->lchild ptr->rchild
Choice of rightsubtree=Y
C
rcptr
CreateBinaryTree(rcptr, C)
Creation of Binary Tree
Insertion of Nodes into Binary Tree
Algorithm InsertBinaryTree
Input: Key is the data of the node after which the new node
will be inserted and item is the data of the new node to be
inserted
Output: A node having data content Item inserted as an
external node after the node having the data Key
Data structure: Linked list structure of binary tree, Root
being the pointer to root node
InsertBinaryTree(key, item)
1. ptr=SearchNode(root, key)
2. if (ptr = = NULL) then
3. print “Search is unsuccessful: No insertion”
4. exit
5. endif
6. if (ptr->lchild = =NULL) or (ptr ->rchild = =NULL) then
7. Read choice to insert as left(L) or right(R) child
8. if (choice = =L) then
9. if (ptr->lchild = =NULL) then
10. new=GetNode(NODE)
11. new->data =item
12. new->lchild = new->rchild = NULL
13. ptr->lchild = new
14. else
15. print “Insertion is not possible as left child”
16. exit
17. endif
18. else
19. if (ptr->rchild = =NULL)
20. new = GetNode(NODE)
21. new->data = item
22. new->lchild = new-> rchild = NULL
23. ptr->rchild = new
24. else
25. print “Insertion is not possible as right child”
26. exit
27. endif
28. endif
29. else
30. print “The key node already has child”
31. endif
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
Operations on Binary Trees
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
InsertBinaryTree(D, H)
root
ptr
Operations on Binary Trees
Insertion of Nodes into Binary Tree if (choice = =L) then
if (ptr->lchild = =NULL) then
new=GetNode(NODE)
new->data =item
new->lchild = new->rchild = NULL
ptr->lchild = new
A
B C
D E F G
new
H
new
H
new
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
InsertBinaryTree(D, H)
root
ptr
Operations on Binary Trees
Insertion of Nodes into Binary Tree if (choice = =L) then
if (ptr->lchild = =NULL) then
new=GetNode(NODE)
new->data =item
new->lchild = new->rchild = NULL
ptr->lchild = new
A
B C
D E F G
new
H
new
H
Search Node within a Binary Tree
Algorithm SearchNode
Input: Key is the data of the node to be searched, temp is
the pointer to the node from where search begins
Output: Pointer to the node having data content Key
Data structure: Linked list structure of binary tree, Root
being the pointer to root node
SearchNode(temp, key)
1. ptr=temp
2. if (ptr->data != key) then
3. if (ptr->lchild!=NULL) then
4. SearchNode(ptr->lchild, key)
5. else
6. return(0)
7. endif
8. if (ptr->rchild!=NULL) then
9. SearchNode(ptr->rchild, key)
10. else
11. return(0)
12. endif
13. else
14. return(ptr)
15. endif
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
Operations on Binary Trees
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
SearchNode(root, D)
root
ptr
Operations on Binary Trees
Searching of Nodes in Binary Tree
SearchNode(temp, key)
1. ptr=temp
2. if (ptr->data != key) then
3. if (ptr->lchild!=NULL) then
4. SearchNode(ptr->lchild, key)
5. else
6. return(0)
7. endif
8. if (ptr->rchild!=NULL) then
9. SearchNode(ptr->rchild, key)
10. else
11. return(0)
12. endif
13. else
14. return(ptr)
15. endif
A
B C
D E F G
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
SearchNode(root, D)
root
ptr
Operations on Binary Trees
SearchNode(temp, key)
1. ptr=temp
2. if (ptr->data != key)
3. if (ptr->lchild!=NULL)
4. SearchNode(ptr->lchild, key)
5. else
6. return(0)
7. endif
8. if (ptr->rchild!=NULL)
9. SearchNode(ptr->rchild, key)
10. else
11. return(0)
12. endif
13. else
14. return(ptr)
15. endif
A
B C
D E F G
Searching of Nodes in Binary Tree
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
SearchNode(root, D)
root
ptr
Operations on Binary Trees
SearchNode(temp, key)
1. ptr=temp
2. if (ptr->data != key)
3. if (ptr->lchild!=NULL)
4. SearchNode(ptr->lchild, key)
5. else
6. return(0)
7. endif
8. if (ptr->rchild!=NULL)
9. SearchNode(ptr->rchild, key)
10. else
11. return(0)
12. endif
13. else
14. return(ptr)
15. endif
A
B C
D E F G
Searching of Nodes in Binary Tree
Deletion of Nodes from Binary Tree
Algorithm DeleteBinaryTree
Input: Item is the data of the node which is to be
deleted, root being the pointer to the root node
Output: A binary tree not having the a node with the
data Item
Data structure: Linked list structure of binary tree,
Root being the pointer to root node
DeleteBinaryTree(root, item)
1. ptr=root
2. if (ptr = = NULL) then
3. print “Tree is empty”
4. exit
5. endif
6. parent = SearchParent(root, item)
7. if (parent !=NULL) then
8. ptr1=parent->lchild
9. ptr2=parent->rchild
10. if (ptr1->data = = item) then
11. if(ptr1->lchild= =NULL) and if(ptr1->rchild= =NULL)
then
12. parent->lchild = NULL
13. else
14. print “Node is an internal node: Deletion is not
possible”
15. endif
16. else
17. if(ptr2->lchild= =NULL) and if(ptr2->rchild= =NULL)
then
18. parent->rchild = NULL
19. else
20. print “Node is an internal node: Deletion is not
possible”
21. endif
22. endif
23. else
24. print “Node with Item doesn’t exist”
25. endif
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
Operations on Binary Trees
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
DeleteBinaryTree(root, D)
root
ptr
Operations on Binary Trees
Deletion of Nodes from Binary Tree
6. parent = SearchParent(root, item)
7. if (parent !=NULL) then
8. ptr1=parent->child
9. ptr2=parent->rchild
10. if ptr1->data == item) then
11. if(ptr1->lchild==NULL) and if(ptr1->rchild==NULL) then
12. parent->lchild = NULL
13. else
14. print “Node is an internal node: Deletion not possible”
15. endif
A
B C
D E F G
parent
Search Parent of a Node within a Binary Tree
Algorithm SearchParent
Input: Key is the data of the node to be searched, ptr is the
pointer to the node from where search begins
Output: Pointer to the parent of the node having data
content Key
Data structure: Linked list structure of binary tree, Root
being the pointer to root node
SearchParent(ptr, key)
1. parent=ptr
2. if (ptr->data != key) then
3. ptr1=ptr->lchild
4. ptr2=ptr->rchild
5. if (ptr1!= NULL)
6. SearchParent(ptr1, key) //search left subtree
7. else
8. parent=NULL //Item not found in left subtree
9. endif
10. if (ptr2!=NULL)
11. SearchParent(ptr2, key) //search right subtree
12. else
13. parent=NULL //Item not found in right subtree
14. endif
15. else
16. return(parent) //return pointer to parent node
17. endif
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
Operations on Binary Trees
CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
Thank You
Questions?

More Related Content

PPT
358 33 powerpoint-slides_10-trees_chapter-10
PPT
PPTX
Basics of Binary Tree and Binary Search Tree.pptx
PPTX
Binary Trees.pptx module 122img 787554yau
PPTX
NON-LINEAR DATA STRUCTURE-TREES.pptx
PPTX
Unit 6 tree
PPT
Lecture 5 tree.pptx
PPTX
Unit – vi tree
358 33 powerpoint-slides_10-trees_chapter-10
Basics of Binary Tree and Binary Search Tree.pptx
Binary Trees.pptx module 122img 787554yau
NON-LINEAR DATA STRUCTURE-TREES.pptx
Unit 6 tree
Lecture 5 tree.pptx
Unit – vi tree

Similar to tree basics to advanced all concepts part 2 (20)

PDF
CS-102 Course_ Binary Tree Lectures .pdf
PPT
Unit iii(dsc++)
PPTX
Introduction to Tree_Data Structure.pptx
PDF
Unit 4.1 (tree)
PDF
Tree terminology and introduction to binary tree
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PPTX
Data structures 3
PPTX
PPTX
Tree all information about tree concept are available .
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PDF
Chapter 5_Trees.pdf
PPTX
Trees in Data Structure
PPTX
Lecture 21_Trees - I.pptx
PPTX
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
PPTX
Data structure using c module 2
PPT
data_structures_and_applications_-_module-4.ppt
PPTX
Tree.pptx
PPTX
Binary tree
PPTX
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
PDF
unit-2-dsa-tree-2024-1 (1) (1).pdf data structure
CS-102 Course_ Binary Tree Lectures .pdf
Unit iii(dsc++)
Introduction to Tree_Data Structure.pptx
Unit 4.1 (tree)
Tree terminology and introduction to binary tree
UNIT III Non Linear Data Structures - Trees.pptx
Data structures 3
Tree all information about tree concept are available .
UNIT III Non Linear Data Structures - Trees.pptx
Chapter 5_Trees.pdf
Trees in Data Structure
Lecture 21_Trees - I.pptx
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
Data structure using c module 2
data_structures_and_applications_-_module-4.ppt
Tree.pptx
Binary tree
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
unit-2-dsa-tree-2024-1 (1) (1).pdf data structure
Ad

Recently uploaded (20)

PPT
Total quality management ppt for engineering students
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
Abrasive, erosive and cavitation wear.pdf
PDF
737-MAX_SRG.pdf student reference guides
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PPTX
introduction to high performance computing
PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PPTX
communication and presentation skills 01
PPT
Occupational Health and Safety Management System
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PDF
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PPTX
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
Total quality management ppt for engineering students
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Fundamentals of safety and accident prevention -final (1).pptx
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Abrasive, erosive and cavitation wear.pdf
737-MAX_SRG.pdf student reference guides
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
introduction to high performance computing
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
communication and presentation skills 01
Occupational Health and Safety Management System
Categorization of Factors Affecting Classification Algorithms Selection
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
R24 SURVEYING LAB MANUAL for civil enggi
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Safety Seminar civil to be ensured for safe working.
Exploratory_Data_Analysis_Fundamentals.pdf
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
Ad

tree basics to advanced all concepts part 2

  • 1. CSC303: Data Structures and Algorithms Audience: B. Tech. (CSE), Semester III Instructor: Dr. Mamata Dalui The study materials/presentations used in this course are solely meant for academic purposes and collected from different available course materials, slides, books, etc. Assistant Professor Department of Computer Science and Engineering National Institute of Technology Durgapur
  • 3. Lemma 1 [Maximum number of nodes]: The maximum number of nodes at level l of a binary tree is 2l, for l 0. Proof: The proof of this lemma can be done by induction on l. The only node at level l = 0 is the root node. Hence, the maximum number of nodes at level l = 0 is 2l =20 =1. Let assume the above holds true for all i, 0 ≤ i < l. That is, the maximum number of nodes at level i is 2i. As any node in the binary tree has degree 2, so for each node at level i, there may be at most 2 nodes at level i+1. Therefore, the maximum number of nodes at level i+1 is 2 × 2i = 2i+1. So, the formula is true for i+1, if it is true for i. Hence, the proof. Properties of Binary Trees
  • 4. Lemma 2 [Maximum number of nodes]: The maximum number of nodes in a binary tree of height h is 2h - 1. Proof: A binary tree can have maximum number of nodes when all the levels are having maximum number of nodes. Therefore, the maximum number of nodes in a binary tree of height h is However, by definition, height h = kmax+1. So, maximum number of nodes nmax = 2h-1. Also, given the maximum number of nodes nmax , the height h can be calculated as ℎ = ⌈𝑙𝑜𝑔2 𝑛𝑚𝑎𝑥+1 ⌉ Properties of Binary Trees 𝑛 = 2𝑖 𝑘𝑚𝑎𝑥 𝑖=0 = 2𝑘𝑚𝑎𝑥 +1 − 1 2 − 1 =2𝑘𝑚𝑎𝑥 +1 −1 , where kmax is the maximum level of the tree.
  • 5. Lemma 3 [Minimum number of nodes]: The minimum number of nodes in a binary tree of height h is h. Proof: A binary tree can have minimum number of nodes when all the levels are having minimum number of nodes. The minimum number of nodes appears when each parent has only one child and occurs for a skewed tree. Therefore, a skewed binary tree of height h with only one path contains h number of nodes. Hence, nmin = h. Properties of Binary Trees
  • 6. Lemma 4 [Relation between number of leaf nodes and degree-2 nodes]: For any nonempty binary tree, if n is the number of nodes and e is the number of edges, then n = e + 1. Proof: Let n be the number of vertices in a binary tree. If n=1, then the number of edges=0. If n=2 then the number of edges=1. If n=3 then the number of edges=2. If n=n’ then the number of edges = n’-1. Suppose, the above is true for any number of nodes n’. Hence, n’=e’+1. Now, we will try to prove that this formula holds true for n’+1. Thus, n’+1 = (e’+1) + 1 or, n’+1 = ((n’ – 1) + 1)+ 1, where e’= n’-1 or, n’ + 1 = ((n’ + 1) – 1) + 1 Hence, it implies that if the formula is true for any n, then it is also true for n+1. Therefore, n = e + 1. Properties of Binary Trees
  • 7. Lemma 5 [Relation between number of leaf nodes and degree-2 nodes]: For any nonempty binary tree, if n0 is the number of leaf nodes and n2 is the number of nodes with degree 2, then n0 = n2 + 1. Proof: Suppose, there are a total of n number of nodes in the binary tree, and ni is the number of nodes with degree i, 0 ≤ i ≤ 2. Therefore, n = n0+n1+n2, since for a binary tree no other kinds of nodes are possible. If e is the total number of edges in the binary tree, then e = n0×0+n1×1+n2×2 or, e = n1+2n2 Also, we have, n = e+1 (from Lemma 4) So, we can write, n = 1+ n1+2n2 Hence, n0+n1+n2 = 1+n1+2n2 or, n0 = n2 + 1 Hence the proof. Properties of Binary Trees
  • 8. Array representation If a complete binary tree with n nodes is represented sequentially, then for any node with index i, 0 < i  n-1, we have 1. parent(i) is at (i-1)/2 if i  0. If i = 0, i is at the root and has no parent. 2. LeftChild(i) is at 2i +1 if 2i +1  n-1. If 2i  n-1, then i has no left child. 3. RightChild(i) is at 2i+2 if 2i+2  n-1. If 2i +1  n-1, then i has no right child [0] [1] [2] [3] [4] [5] [6] A B C — D — E Level 0 Level 1 Level 2 A B D C E 0 1 2 3 4 5 6 Representation of Binary Trees
  • 9. Array representation • Wastage of spaces: In the worst case, a skewed tree of height(depth) k requires 2k-1 spaces. Of these, only k spaces will actually be used. • Insertion or deletion of nodes from the middle of a tree requires the movement of potentially many nodes to reflect the change in the level of these nodes. Representation of Binary Trees A B C D E F G BT[0] BT[1] BT[2] BT[3] BT[4] BT[5] BT[6] A B C D E F G 0 1 2 3 4 5 6
  • 10. Array representation • Wastage of spaces: In the worst case, a skewed tree of height (depth) k requires 2k-1 spaces. Of these, only k spaces will actually be used. • Insertion or deletion of nodes from the middle of a tree requires the movement of potentially many nodes to reflect the change in the level of these nodes. Representation of Binary Trees A B - C - - - BT[0] BT[1] BT[2] BT[3] BT[4] BT[5] BT[6] A B C 0 1 3
  • 11. Linked representation • Overcomes the shortcomings of array representation. • The node structure is as shown below. The Data part stores the information and the two pointers lchild and rchild stores the addresses of the left child and right child. • Given the pointer to the root node, any other node can be accessed. Representation of Binary Trees lchild data rchild Structure of a node
  • 12. Linked representation Representation of Binary Trees A B C D E F G Linked representation of a complete binary tree struct node { int data; struct node *lchild, *rchild; }; typedef struct node NODE;
  • 13. CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) Representation of Binary Trees Pros and Cons of Array (Sequential) Representation of Binary Trees • Any node can be accessed from any other node just by calculating the corresponding index. From execution point of view, this is efficient. • Only data is stored, but no pointers to successor or predecessor. Successor or predecessor of a node is implied by their indexing. • Programming languages that doesn’t support dynamic memory allocation (such as FORTRAN, BASIC, etc.) array representation is the only way to store a tree. • Except complete binary tree, for all other binary trees most of the entries in the array representation remain empty. • Only static representation is allowed. The tree size is limited by the size of the array. • Insertion of nodes into the tree and deletion of nodes from the tree are inefficient as they involve a considerable amount of data movement incurring excessive processing time.
  • 14. CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) Operations on Binary Trees The primary operations on a binary tree are - • Creation – to create a binary tree • Insertion – to include a new node into an existing (may be empty) binary tree • Deletion – to delete a node from a non-empty binary tree • Search – to search for a particular node in the binary tree • Traversal – to traverse the nodes of the binary tree • Merging – to merge two binary trees into a single
  • 15. Implementation of Binary Trees Creation of Binary Tree Algorithm CreateBinaryTtree Input: Item is the data of the node with pointer ptr Output: A binary tree with two subtrees of node pointed by ptr Data structure: Linked list structure of binary tree CreateBinaryTree(ptr, item) 1. if ptr!= NULL then 2. ptr->data = item 3. Do the Node has left subtree (Give choice = Y/N)? 4. if (choice = = Y) then 5. lcptr=GetNode(NODE) 6. ptr->lchild = lcptr 7. CreateBinaryTree(lcptr, NewItemLC); /*Create subtree with the item NewItemLC */ 8. else 9. lcptr = NULL 10. prt ->lchild = NULL 11. CreateBinaryTree(lcptr, NULL); //Empty left subtree 12. endif 13. Do the Node has right subtree (Give choice = Y/N)? 14. if (choice = = Y) then 15. rcptr=GetNode(NODE) 16. ptr->rchild = rcptr 17. CreateBinaryTree(rcptr, NewItemRC); /*Create subtree with the item NewItemRC */ 18. else 19. rcptr = NULL 20. prt ->rchild = NULL 21. CreateBinaryTree(rcptr, NULL); /*Empty right subtree*/ 22. endif 23. endif CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd)
  • 16. CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) CreateBinaryTree(ptr, A) Operations on Binary Trees CreateBinaryTree(ptr, item) 1. if ptr!= NULL then 2. ptr->data = item 3. Do the Node has left subtree (Give choice = Y/N)? 4. if (choice = = Y) then 5. lcptr=GetNode(NODE) 6. ptr->lchild = lcptr 7. CreateBinaryTree(lcptr, NewItemLC); /*Create subtree with the item NewItemLC */ 8. else 9. lcptr = NULL 10. prt ->lchild = NULL 11. CreateBinaryTree(lcptr, NULL); //Empty left subtree 12. endif 13. Do the Node has right subtree (Give choice = Y/N)? 14. if (choice = = Y) then 15. rcptr=GetNode(NODE) 16. ptr->rchild = rcptr 17. CreateBinaryTree(rcptr, NewItemRC); /*Create subtree with the item NewItemRC */ 18. else 19. rcptr = NULL 20. prt ->rchild = NULL 21. CreateBinaryTree(rcptr, NULL); /*Empty right subtree*/ 22. endif 23. endif ptr A ptr lcptr ptr->lchild ptr->rchild Creation of Binary Tree Choice of left subtree=Y
  • 17. CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) CreateBinaryTree(ptr, A) Operations on Binary Trees CreateBinaryTree(ptr, item) 1. if ptr!= NULL then 2. ptr->data = item 3. Do the Node has left subtree (Give choice = Y/N)? 4. if (choice = = Y) then 5. lcptr=GetNode(NODE) 6. ptr->lchild = lcptr 7. CreateBinaryTree(lcptr, NewItemLC); /*Create subtree with the item NewItemLC */ 8. else 9. lcptr = NULL 10. prt ->lchild = NULL 11. CreateBinaryTree(lcptr, NULL); //Empty left subtree 12. endif 13. Do the Node has right subtree (Give choice = Y/N)? 14. if (choice = = Y) then 15. rcptr=GetNode(NODE) 16. ptr->rchild = rcptr 17. CreateBinaryTree(rcptr, NewItemRC); /*Create subtree with the item NewItemRC */ 18. else 19. rcptr = NULL 20. prt ->rchild = NULL 21. CreateBinaryTree(rcptr, NULL); /*Empty right subtree*/ 22. endif 23. endif ptr A ptr lcptr CreateBinaryTree(lcptr, B) ptr->lchild ptr->rchild Creation of Binary Tree
  • 18. CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) CreateBinaryTree(ptr, A) Operations on Binary Trees CreateBinaryTree(ptr, item) 1. if ptr!= NULL then 2. ptr->data = item 3. Do the Node has left subtree (Give choice = Y/N)? 4. if (choice = = Y) then 5. lcptr=GetNode(NODE) 6. ptr->lchild = lcptr 7. CreateBinaryTree(lcptr, NewItemLC); /*Create subtree with the item NewItemLC */ 8. else 9. lcptr = NULL 10. prt ->lchild = NULL 11. CreateBinaryTree(lcptr, NULL); //Empty left subtree 12. endif 13. Do the Node has right subtree (Give choice = Y/N)? 14. if (choice = = Y) then 15. rcptr=GetNode(NODE) 16. ptr->rchild = rcptr 17. CreateBinaryTree(rcptr, NewItemRC); /*Create subtree with the item NewItemRC */ 18. else 19. rcptr = NULL 20. prt ->rchild = NULL 21. CreateBinaryTree(rcptr, NULL); /*Empty right subtree*/ 22. endif 23. endif ptr A ptr B lcptr CreateBinaryTree(lcptr, B) ptr->lchild ptr->rchild Creation of Binary Tree
  • 19. CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) CreateBinaryTree(ptr, A) Operations on Binary Trees CreateBinaryTree(ptr, item) 1. if ptr!= NULL then 2. ptr->data = item 3. Do the Node has left subtree (Give choice = Y/N)? 4. if (choice = = Y) then 5. lcptr=GetNode(NODE) 6. ptr->lchild = lcptr 7. CreateBinaryTree(lcptr, NewItemLC); /*Create subtree with the item NewItemLC */ 8. else 9. lcptr = NULL 10. prt ->lchild = NULL 11. CreateBinaryTree(lcptr, NULL); //Empty left subtree 12. endif 13. Do the Node has right subtree (Give choice = Y/N)? 14. if (choice = = Y) then 15. rcptr=GetNode(NODE) 16. ptr->rchild = rcptr 17. CreateBinaryTree(rcptr, NewItemRC); /*Create subtree with the item NewItemRC */ 18. else 19. rcptr = NULL 20. prt ->rchild = NULL 21. CreateBinaryTree(rcptr, NULL); /*Empty right subtree*/ 22. endif 23. endif ptr A ptr B lcptr ptr->lchild ptr->rchild Choice of left subtree=N Creation of Binary Tree
  • 20. CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) CreateBinaryTree(ptr, A) Operations on Binary Trees CreateBinaryTree(ptr, item) 1. if ptr!= NULL then 2. ptr->data = item 3. Do the Node has left subtree (Give choice = Y/N)? 4. if (choice = = Y) then 5. lcptr=GetNode(NODE) 6. ptr->lchild = lcptr 7. CreateBinaryTree(lcptr, NewItemLC); /*Create subtree with the item NewItemLC */ 8. else 9. lcptr = NULL 10. prt ->lchild = NULL 11. CreateBinaryTree(lcptr, NULL); //Empty left subtree 12. endif 13. Do the Node has right subtree (Give choice = Y/N)? 14. if (choice = = Y) then 15. rcptr=GetNode(NODE) 16. ptr->rchild = rcptr 17. CreateBinaryTree(rcptr, NewItemRC); /*Create subtree with the item NewItemRC */ 18. else 19. rcptr = NULL 20. prt ->rchild = NULL 21. CreateBinaryTree(rcptr, NULL); /*Empty right subtree*/ 22. endif 23. endif ptr A ptr B lcptr ptr->lchild ptr->rchild Choice of right subtree=Y Creation of Binary Tree
  • 21. CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) CreateBinaryTree(ptr, A) Operations on Binary Trees CreateBinaryTree(ptr, item) 1. if ptr!= NULL then 2. ptr->data = item 3. Do the Node has left subtree (Give choice = Y/N)? 4. if (choice = = Y) then 5. lcptr=GetNode(NODE) 6. ptr->lchild = lcptr 7. CreateBinaryTree(lcptr, NewItemLC); /*Create subtree with the item NewItemLC */ 8. else 9. lcptr = NULL 10. prt ->lchild = NULL 11. CreateBinaryTree(lcptr, NULL); //Empty left subtree 12. endif 13. Do the Node has right subtree (Give choice = Y/N)? 14. if (choice = = Y) then 15. rcptr=GetNode(NODE) 16. ptr->rchild = rcptr 17. CreateBinaryTree(rcptr, NewItemRC); /*Create subtree with the item NewItemRC */ 18. else 19. rcptr = NULL 20. prt ->rchild = NULL 21. CreateBinaryTree(rcptr, NULL); /*Empty right subtree*/ 22. endif 23. endif ptr A ptr B lcptr ptr->lchild ptr->rchild Choice of right subtree=Y rcptr Creation of Binary Tree
  • 22. CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) CreateBinaryTree(ptr, A) Operations on Binary Trees CreateBinaryTree(ptr, item) 1. if ptr!= NULL then 2. ptr->data = item 3. Do the Node has left subtree (Give choice = Y/N)? 4. if (choice = = Y) then 5. lcptr=GetNode(NODE) 6. ptr->lchild = lcptr 7. CreateBinaryTree(lcptr, NewItemLC); /*Create subtree with the item NewItemLC */ 8. else 9. lcptr = NULL 10. prt ->lchild = NULL 11. CreateBinaryTree(lcptr, NULL); //Empty left subtree 12. endif 13. Do the Node has right subtree (Give choice = Y/N)? 14. if (choice = = Y) then 15. rcptr=GetNode(NODE) 16. ptr->rchild = rcptr 17. CreateBinaryTree(rcptr, NewItemRC); /*Create subtree with the item NewItemRC */ 18. else 19. rcptr = NULL 20. prt ->rchild = NULL 21. CreateBinaryTree(rcptr, NULL); /*Empty right subtree*/ 22. endif 23. endif ptr A ptr B lcptr ptr->lchild ptr->rchild Choice of rightsubtree=Y C rcptr CreateBinaryTree(rcptr, C) Creation of Binary Tree
  • 23. Insertion of Nodes into Binary Tree Algorithm InsertBinaryTree Input: Key is the data of the node after which the new node will be inserted and item is the data of the new node to be inserted Output: A node having data content Item inserted as an external node after the node having the data Key Data structure: Linked list structure of binary tree, Root being the pointer to root node InsertBinaryTree(key, item) 1. ptr=SearchNode(root, key) 2. if (ptr = = NULL) then 3. print “Search is unsuccessful: No insertion” 4. exit 5. endif 6. if (ptr->lchild = =NULL) or (ptr ->rchild = =NULL) then 7. Read choice to insert as left(L) or right(R) child 8. if (choice = =L) then 9. if (ptr->lchild = =NULL) then 10. new=GetNode(NODE) 11. new->data =item 12. new->lchild = new->rchild = NULL 13. ptr->lchild = new 14. else 15. print “Insertion is not possible as left child” 16. exit 17. endif 18. else 19. if (ptr->rchild = =NULL) 20. new = GetNode(NODE) 21. new->data = item 22. new->lchild = new-> rchild = NULL 23. ptr->rchild = new 24. else 25. print “Insertion is not possible as right child” 26. exit 27. endif 28. endif 29. else 30. print “The key node already has child” 31. endif CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) Operations on Binary Trees
  • 24. CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) InsertBinaryTree(D, H) root ptr Operations on Binary Trees Insertion of Nodes into Binary Tree if (choice = =L) then if (ptr->lchild = =NULL) then new=GetNode(NODE) new->data =item new->lchild = new->rchild = NULL ptr->lchild = new A B C D E F G new H new H new
  • 25. CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) InsertBinaryTree(D, H) root ptr Operations on Binary Trees Insertion of Nodes into Binary Tree if (choice = =L) then if (ptr->lchild = =NULL) then new=GetNode(NODE) new->data =item new->lchild = new->rchild = NULL ptr->lchild = new A B C D E F G new H new H
  • 26. Search Node within a Binary Tree Algorithm SearchNode Input: Key is the data of the node to be searched, temp is the pointer to the node from where search begins Output: Pointer to the node having data content Key Data structure: Linked list structure of binary tree, Root being the pointer to root node SearchNode(temp, key) 1. ptr=temp 2. if (ptr->data != key) then 3. if (ptr->lchild!=NULL) then 4. SearchNode(ptr->lchild, key) 5. else 6. return(0) 7. endif 8. if (ptr->rchild!=NULL) then 9. SearchNode(ptr->rchild, key) 10. else 11. return(0) 12. endif 13. else 14. return(ptr) 15. endif CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) Operations on Binary Trees
  • 27. CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) SearchNode(root, D) root ptr Operations on Binary Trees Searching of Nodes in Binary Tree SearchNode(temp, key) 1. ptr=temp 2. if (ptr->data != key) then 3. if (ptr->lchild!=NULL) then 4. SearchNode(ptr->lchild, key) 5. else 6. return(0) 7. endif 8. if (ptr->rchild!=NULL) then 9. SearchNode(ptr->rchild, key) 10. else 11. return(0) 12. endif 13. else 14. return(ptr) 15. endif A B C D E F G
  • 28. CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) SearchNode(root, D) root ptr Operations on Binary Trees SearchNode(temp, key) 1. ptr=temp 2. if (ptr->data != key) 3. if (ptr->lchild!=NULL) 4. SearchNode(ptr->lchild, key) 5. else 6. return(0) 7. endif 8. if (ptr->rchild!=NULL) 9. SearchNode(ptr->rchild, key) 10. else 11. return(0) 12. endif 13. else 14. return(ptr) 15. endif A B C D E F G Searching of Nodes in Binary Tree
  • 29. CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) SearchNode(root, D) root ptr Operations on Binary Trees SearchNode(temp, key) 1. ptr=temp 2. if (ptr->data != key) 3. if (ptr->lchild!=NULL) 4. SearchNode(ptr->lchild, key) 5. else 6. return(0) 7. endif 8. if (ptr->rchild!=NULL) 9. SearchNode(ptr->rchild, key) 10. else 11. return(0) 12. endif 13. else 14. return(ptr) 15. endif A B C D E F G Searching of Nodes in Binary Tree
  • 30. Deletion of Nodes from Binary Tree Algorithm DeleteBinaryTree Input: Item is the data of the node which is to be deleted, root being the pointer to the root node Output: A binary tree not having the a node with the data Item Data structure: Linked list structure of binary tree, Root being the pointer to root node DeleteBinaryTree(root, item) 1. ptr=root 2. if (ptr = = NULL) then 3. print “Tree is empty” 4. exit 5. endif 6. parent = SearchParent(root, item) 7. if (parent !=NULL) then 8. ptr1=parent->lchild 9. ptr2=parent->rchild 10. if (ptr1->data = = item) then 11. if(ptr1->lchild= =NULL) and if(ptr1->rchild= =NULL) then 12. parent->lchild = NULL 13. else 14. print “Node is an internal node: Deletion is not possible” 15. endif 16. else 17. if(ptr2->lchild= =NULL) and if(ptr2->rchild= =NULL) then 18. parent->rchild = NULL 19. else 20. print “Node is an internal node: Deletion is not possible” 21. endif 22. endif 23. else 24. print “Node with Item doesn’t exist” 25. endif CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) Operations on Binary Trees
  • 31. CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) DeleteBinaryTree(root, D) root ptr Operations on Binary Trees Deletion of Nodes from Binary Tree 6. parent = SearchParent(root, item) 7. if (parent !=NULL) then 8. ptr1=parent->child 9. ptr2=parent->rchild 10. if ptr1->data == item) then 11. if(ptr1->lchild==NULL) and if(ptr1->rchild==NULL) then 12. parent->lchild = NULL 13. else 14. print “Node is an internal node: Deletion not possible” 15. endif A B C D E F G parent
  • 32. Search Parent of a Node within a Binary Tree Algorithm SearchParent Input: Key is the data of the node to be searched, ptr is the pointer to the node from where search begins Output: Pointer to the parent of the node having data content Key Data structure: Linked list structure of binary tree, Root being the pointer to root node SearchParent(ptr, key) 1. parent=ptr 2. if (ptr->data != key) then 3. ptr1=ptr->lchild 4. ptr2=ptr->rchild 5. if (ptr1!= NULL) 6. SearchParent(ptr1, key) //search left subtree 7. else 8. parent=NULL //Item not found in left subtree 9. endif 10. if (ptr2!=NULL) 11. SearchParent(ptr2, key) //search right subtree 12. else 13. parent=NULL //Item not found in right subtree 14. endif 15. else 16. return(parent) //return pointer to parent node 17. endif CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) Operations on Binary Trees
  • 33. CSC303 Dept of CSE, NIT Durgapur 2020-21(Odd) Thank You Questions?