Assignment 2
Assignment 2
#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
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
setal.c X
Ly LG
SET
ENU fENGNE
ale Edit View Search Tools seta1.c (HAtharva 20
Documents Help
Open v Save ndo
setal.c %
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
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) ;
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
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