-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree_linked_list.c
More file actions
165 lines (128 loc) · 3.58 KB
/
Copy pathbinary_tree_linked_list.c
File metadata and controls
165 lines (128 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// implementation of the binary tree using linked list
#include<stdio.h>
#include<stdlib.h>
// linked list node
struct listNode{
int data;
struct listNode* next;
};
// traversing linked list
void linkedListTraversal(struct listNode *ptr)
{
while (ptr != NULL)
{
printf("Element: %d\n", ptr->data);
ptr = ptr->next;
}
}
//Insert a node first position of the linked list
struct listNode * insertAtFirst(struct listNode *head, int data){
struct listNode * ptr = (struct listNode *) malloc(sizeof(struct listNode));
ptr->data = data;
ptr->next = head;
return ptr;
}
// Tree node structure
struct treeNode
{
int data;
struct treeNode* right;
struct treeNode* left;
};
// creating a new node
struct treeNode*create(int data)
{
struct treeNode* Tnewnode;
Tnewnode=(struct treeNode*)malloc(sizeof(struct treeNode));
Tnewnode->data=data;
Tnewnode->left=NULL;
Tnewnode->right=NULL;
return Tnewnode;
};
// preorder traversal
void preOrder(struct treeNode* root)
{
if(root!=NULL)
{
printf("%d ",root->data);
preOrder(root->left);
preOrder(root->right);
}
}
// postorder traversal
void postOrder(struct treeNode* root)
{
if(root!=NULL)
{
postOrder(root->left);
postOrder(root->right);
printf("%d ",root->data);
}
}
// Inorder traversal
void inOrder(struct treeNode* root)
{
if(root!=NULL)
{
inOrder(root->left);
printf("%d ",root->data);
inOrder(root->right);
}
}
int main(){
int choice;
struct listNode *head;
struct listNode *second;
struct listNode *third;
struct listNode *fourth;
// Allocate memory for nodes in the linked list in Heap
head = (struct listNode *)malloc(sizeof(struct listNode));
second = (struct listNode *)malloc(sizeof(struct listNode));
third = (struct listNode *)malloc(sizeof(struct listNode));
fourth = (struct listNode *)malloc(sizeof(struct listNode));
// Link first and second nodes
head->data = 7;
head->next = second;
// Link second and third nodes
second->data = 11;
second->next = third;
// Link third and fourth nodes
third->data = 41;
third->next = fourth;
// Terminate the list at the third node
fourth->data = 66;
fourth->next = NULL;
struct treeNode* root=create(head->data);
struct treeNode* root1=create(second->data);
struct treeNode* root2=create(third->data);
struct treeNode* root3=create(fourth->data);
do
{
printf("\nChoose the Choices Below :\n 1.Inserting a element in the tree\n 2.PreOrder Traversal \n 3.PostOrder Traversal \n 4. InOrder Traversal \n 5.Exit\n ");
scanf("%d",&choice);
switch (choice)
{
case 1:
root -> left= root1;
root ->right=root2;
root1 ->left=root3;
break;
case 2:
preOrder(root);
break;
case 3:
postOrder(root);
break;
case 4:
inOrder(root);
break;
case 5:
exit(0);
break;
default:
printf("Wrong Choices \n");
break;
}
} while (choice!=5);
return 0;
}