0% found this document useful (0 votes)
2 views

Assignment 2

The document contains C code for implementing a binary search tree (BST) with functions for inserting nodes, calculating height, printing levels, counting nodes at a specific level, and copying the tree. It includes a main function that demonstrates these functionalities by creating a BST, printing its levels, and comparing two BSTs for equality. The code is structured with proper memory allocation and recursive functions to manage the tree operations.

Uploaded by

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

Assignment 2

The document contains C code for implementing a binary search tree (BST) with functions for inserting nodes, calculating height, printing levels, counting nodes at a specific level, and copying the tree. It includes a main function that demonstrates these functionalities by creating a BST, printing its levels, and comparing two BSTs for equality. The code is structured with proper memory allocation and recursive functions to manage the tree operations.

Uploaded by

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

Fle VM VirtualBox

Machine View Input


Devices Help
Applications Places System
File Edit View
Search Tools
FOpen v
Documents Help setal.c(-/Atharva 20/ASS 2DSA)"g
Save
Undo
setal.c X

#include<stdio.h>
#include<stdlib. h>
struct Node {
int data;
struct Node* left;
8 };
struct Node* right;
9
10
11 struct Node* newNode (int data) {
L2 struct Node* node = (struct Node*) malloc(sizeof(struct Node));
13 node->data = data;
14 node->left = node->right = NULL;
15 return node;
16 }
17
1a struct Node* insert (struct Node* root, int data) {
19 if (root == NULL) return newNode (data) ;
20
21 if (data < root->data)
22 root->left = insert ( root->left, data);
23 else if (data > root ->data)
root->right = insert (root->right, data);

......
...

LG
fENGINE SET
tAENL
dora 14 (Running] - Oracle VM
VirtualBox
Machine View Input Devices Help
Applications Places System
e Edit View Search Tools Stude
Documents Help setal.c (-/Atharva 20/ASS 2 DSA) -
gedit
Open v Save Undo
setal.c X

root->right = insert ( root ->right, data);


return root;
8
29
30 int height (structNode* root) {
31 if (root == NULL)
32 return -1;
33
34 int leftHeight = height (root ->left);
35 int rightHeight = height (root ->right );
36
37 return (leftHeight > rightHeight ? leftHeight : rightHeight) + l;
38
39 }
|40
41 void printLevel (struct Node* root, int level) {
42 if (root == NULL)
43 return;

if (level == 0)
45 root->data);
printf ("%d "
else { level 1)
Drintlevelroot->left

(LG
SET
FUATRON L177WSB
Fedora 14 (Running]- Oracle VM VirtualBox
A
File Machine View Input Devices Help
Applications Places System
Student
File Edit View Search Tools setal.c (-Atharva 20/ASS 2 DSA) - gedit
Documents Help
D Open v Save Undo

setal.c X

if (level == O)
printf ("%d " , root->data):
47 else {
48 printLevel( root ->left, level - 1);
49 printLevel ( root ->right, level 1) ;
50
52 }
53
54 int countNodesAtLevel (struct Node* root, int level) {
55 if (root == NULL)
56 return 0;
57
58 if (level ==O)
59 return 1;
60 else level
countNodesAt Level (root ->left, level - 1) + countNodeSAtLevel( root->right,
1);
61 return
62
63
root) {
65 void printLevels (struct Node*
int h = height (root) ;
66 h+ 1);
67 printf ("Total levels in the tree: %d\n", Thb Width: 8 v Ln 71, Col 29
CV
int i:

CLG
fENGINE

12
edora 14 [Running) - Oracle VM VirtualBox
Machine View Input Devices Help
Applications Places System
Stud

le Edit View Search Tools


setal.c (-/Atharva 20/ASS 2 DSA) - ged
Documents Help
Open Save Undo

setal.c X

printf("Total levels in the tree:dn", h +1);


int i;
for(i= 0;i <= h; i++) {
printf("Level %d: " i);
printLevel(root, i):
72 printf("" (Node count: %d)\n", countNodesAtLevel (root , i));
3
74 }
75
76 int main() {
77 struct Node* root = NULL;
78
79
80 root = insert (root, 50);
root = insert (root,30);
81 insert (root,20);
82 root =
root = insert (root, 40);
83 insert (root,70);
84 root =
85 root = insert (root, 60);
86 root = insert ( root, 80);
87
88 printLevels (root) ;
89
90 return 0:

Ly LG
SET
ENU fENGNE
ale Edit View Search Tools seta1.c (HAtharva 20
Documents Help
Open v Save ndo

setal.c %

Tor1 = 0;1 <= h; 1++)


printf ("Level %d: ", i);
71 printLevel (root, i);
72 printf (" (Node count: %d) \n",
73 countNodesAtLevel (ro
74 }
75
76 int main() {
77 struct Node* root = NULL;
78
79
80 root = insert (root, 50);
81 root = insert(root, 30);
82 root = insert ( root, 20) ;
83 root = insert (root, 40);
84 root = insert (root, 70);
85 root = insert (root, 60);
86 root = insert (root, 80);
87
88
89 printLevels(root);
90
91 return 0;
92
93 }

LG
MENU fENGINE SET
AUTO
File Edit View Search Tools setb 1.c (-/Atharv
Documents Help
Open Save Undo

setal.c X setbl.c X

#include<stdio.h>
#include<stdlib. h>
typedef struct Node {
int data;
struct Node*left;
struct Node*right;
8 }Node;
9
10 Node* createNode (int data) {
11 Node* newNode = (Node*)malloc (sizeof (Node) );
12 newNode->data = data;
13 newNode->left = NULL;
14 newNode->right = NULL;
15 return newNode;
16 }
17
18 Node* insert (Node* root, int data) {
19 if ( root = NULL) {
20 return createNode(data);
21 }
22 if (data < root ->data) {
23 root ->left = insert (root ->left, data);
24 }else{
root->right = insertroot->riaht data):

CLG
MENUfENGINE SET
AUTO
Systemm

le Edit View Search Tools


Documents Help setbl.c(Atharva 20/ASS
Open v sa Save 5Undo
setal.c Xsetbl.c X

26
root->right = insert (root ->right, data);
}
27 return root:
28 }
29
30 Node* copy (Node* root) {
31 if(root == NULL) {
32 return NULL;
33 }
34 Node* newRoot = createNode( root->data);
35 newRoot->left = copy( root->left);
36 newRoot ->right = copy (root->right);
37 return newRoot;
38
39
40 int compare(Node* rootl,Node* root2){
41 if (rootl == NULL && root2 = NULL) {
42 return 1;
43
44 if (rootl == NULL || root2 = NULL) {
45 return 0;
46
47
48 return (rootl->data == root2->data) && compare ( rootl ->left,

LG
AUTO MENLEENGINE SET
FUSTRON i77WSB
E fedora 14 [Running) - Oracle VM VirtualBox
File Machine View lnput Devices Help
Applications Places System
Student
Fri Aug 7, 5:03 PM
File Edit VieN Search Tools
setbl.c (-/Atharva 20/ASS 2 DSA) - gedit
Documents Help
Open Save Undo

setal.c x setbl.c X

return (rootl->data == root2->data) && compare(rootl->left, root2->left) && compare (root1->right, root2
>right) ;

Void inorder(Node* root)


if (root != NULL) {
inorder(root ->left);
printf ("8sd ",root->data) ;
inorder (root ->right);

int main() {
Node* bstl = NULL;
bstl = insert (bst1, 5);
insert (bstl, 3) ;
insert (bstl, 7);
insert (bstl, 2);
insert (bstl, 4);
insert (bstl, 6);
insert ( bstl, 8);
Ln 68, Col 11 INS
Cv Tab Width: 8
printf("Oriainal BST (in-order) ")
OO7OOIRight Ctri
17203
dx f ENG .08-20924

GLG Ftoy
hm

matrix I iniia

oath
otution
retces pe
kisnot
aninterned
Aereoyere
destina
isanintermedia
distillKIistIk

12

Step 6. last ast1


Step 7 Heapitý (A, top tast), e, recrente heap
Step 8. T ast
goto Sten s

eorithi Heapity(A, top, tast)


key A[top)

AiT A11 be Ipeits to the Jurger child


FUATRON177WSB
Fedora 14 [Running] - Oracle VM VirtualBox
File Machine View Input Devices Help
F Applications Places
System
Student
File Edit View Search
Tools setbl.c (-Atharva 20/ASS 2 DSA) - gedit
Documents Help
Open v s Save
Undo
setal.C % setbl.c *

66
insert(bstl, 2);
insert (bstl, 4);
67 insert (bstl, 6);
68 insert (bstl, 8);
69
70 printf ("Original BST (in-order): ");
71 inorder(bstl);
72 printf("\n");
73
74 Node* bst2 = copy (bstl) ;
75 printf("Copied BST (in -order): ");
I76 inorder (bst2 ) ;
77 printf ("\n");
78
79
80 if (compare(bstl, bst2) ) {
81 printf ("The two BSTs are equal. \n ");
82 } else {
83 printf ( "The teo BSTS are not equal.\n");
84 }
85
|86
87
88 return 0;
89 }

(LG
SET
MENU jENGINE

12

You might also like