0% found this document useful (0 votes)
70 views11 pages

Lab 10 Data Structure 15 December

1. The document describes node deletion, search, and traversal tasks for a binary search tree data structure. It includes code to delete nodes, search for a node, perform pre-order and post-order tree traversals, and save and load tree traversals to and from a file. 2. To delete a node, the code uses if statements to check for nodes with no children, one child, or two children and removes the node accordingly. For search, it compares the search key to node values and traverses left or right. 3. Pre-order prints the root then left then right subtrees. Post-order prints left then right then root. Saving and loading traversals to a

Uploaded by

Amna Ejaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views11 pages

Lab 10 Data Structure 15 December

1. The document describes node deletion, search, and traversal tasks for a binary search tree data structure. It includes code to delete nodes, search for a node, perform pre-order and post-order tree traversals, and save and load tree traversals to and from a file. 2. To delete a node, the code uses if statements to check for nodes with no children, one child, or two children and removes the node accordingly. For search, it compares the search key to node values and traverses left or right. 3. Pre-order prints the root then left then right subtrees. Post-order prints left then right then root. Saving and loading traversals to a

Uploaded by

Amna Ejaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

SUBMITTED BY: MAHREEN RABBANI

Roll no: FA19-BCE-087


SUBMITTED TO: DR OMAR AHMED
LAB: 10
In-Lab Tasks:
1. Node deletion

Program that find the smallest node.

Deletion a node:

In deletion node we have to tackle three cases that is if the tree have no child one child or two child for
each of this we have to write program for this I use if statement that would check if root is null or not if
the data we want to delete is greater than root data then it delete the element the right data of tree
else if the data we want to delete is less then it will delete the left element. For one child I use if
statement that will check if the left and right is null it means the tree have one child and it would be
deleted for one child I also use if statement that would check both left and right data and for two child I
also use if statement that would check both left and right if both have data it mean it have two child.
2. Node search
in this task we have write program that would search specific element for this I use if statement that
would compare the root element and the element we want to search if the root element is equal to the
search element then we would return the element again I use if statement that would check if the key
element is greater than root data then it will search in the right of search tree else it will search in right
of search tree and in this way the element is found.

Program:

Output:

SEARCH 46
Next I search for 100 as 100 is not in tree it display message element not found.

3. Pre-Order and Post-Order printing

In pre-order the root element is first printed then the element on left and the element right of root. For this
I use if statement that check the root is empty or not then I print statement that print root element then the
element on left and then or right.
Output:

POST ORDER:

In post order the element on the left of tree is print first then the right element is printed and the root
element is print last therefore I use if statement that check if the root is empty or not and in else
statement I print the left element then the right element and then the root element is printed.

Post order:
OUTPUT:
POST LAB TASKS
SAVE POST ORDER:
In this task we have save the post order therefore I use if statement
that would check if the root is empty or not and then I call save
function that would first save the left and then the right and the root
element.

SAVE PRE ORDER:


In this task we have save the pre order therefore I use if statement that
would check if the root is empty or not and then I call save function
that would first save the left then the right and then the root element.
SAVE IN ORDER:
In this task we have save the post order therefore I use if statement
that would check if the root is empty or not and then I call save
function that would first save the root element then the left and then
the right.
LOAD TREE:
In this task we have to write program that would load tree for this I use
while statement that would read the data and after that I create a node
using malloc and after that I use if statement that check if the root is
null or have data.

_________________________________________________________

You might also like