-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList2BinaryTree.c
184 lines (152 loc) · 3.89 KB
/
LinkedList2BinaryTree.c
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include<stdio.h>
#include<stdlib.h>
#define MAX_QUEUE_SIZE 200
typedef struct node node;
/**
* Creating a structure for a node of Binary Tree
**/
struct node {
int data;
node *left;
node *right;
};
/**
* Helper function to create new node in the tree
**/
node* newNode(int value) {
node* newNode = (node*)malloc(sizeof(node));
newNode->data=value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
/**
* Creating a structure for a node of Linked List
**/
typedef struct ListNode {
struct ListNode* next;
int data;
}ListNode;
/**
* Helper function to create new node in the Lenked List
**/
ListNode* getNewListNode(int data) {
ListNode *newListNode = (ListNode*) malloc(sizeof(ListNode));
newListNode->data = data;
newListNode->next = NULL;
return newListNode;
}
/**
* This method is to insert a Node at the beginning of the linked list
**/
void insert_begin(ListNode** head, int data) {
ListNode* newListNode = getNewListNode(data);
if(!(*head)) {
(*head) = newListNode;
}
else {
newListNode->next = (*head);
(*head) = newListNode;
}
return;
}
/**
* This method is to display the linked list
**/
void display_list(ListNode* head){
ListNode* current_ListNode = head;
printf("Linked List:\n");
while(current_ListNode){
printf("[%d]-->", current_ListNode->data);
current_ListNode = current_ListNode->next;
}
printf("NULL\n");
}
/**
* function to create new queue
**/
node **createQueue(int *front, int *rear) {
node **queue = (node**)malloc(sizeof(node*)*MAX_QUEUE_SIZE);
*front = 0;
*rear = 0;
return queue;
}
/**
* function to add a node in the queue
**/
void enQueue(node **queue, int *rear, node *root) {
queue[*rear] = root;
(*rear)++;
}
/**
* function to delete a node from the queue
**/
node* deQueue(node **queue, int *front) {
(*front)++;
return queue[*front-1];
}
/**
* function to print the tree in inorder
**/
void printInorder(node *root) {
if(root==NULL) return;
printInorder(root->left);
printf("%d ", root->data);
printInorder(root->right);
}
/**
* function to convert a linked list into a binary tree (not BST)
**/
void convertLinkedList2BinaryTree(ListNode* head, node** root) {
if(head==NULL) {
*root=NULL;
return;
}
int front,rear;
*root = newNode(head->data);
node **queue = createQueue(&front,&rear);
enQueue(queue, &rear, *root);
head = head->next;
while(head!=NULL) {
node *temp = deQueue(queue, &front);
temp->left = newNode(head->data);
enQueue(queue, &rear, temp->left);
head=head->next;
if(head!=NULL) {
temp->right = newNode(head->data);
enQueue(queue, &rear, temp->right);
head=head->next;
}
}
// printInorder(*root);
}
/**
* Main Driving Function
**/
void main() {
// Crating a linked list first
ListNode *head = NULL;
insert_begin(&head, 10);
insert_begin(&head, 30);
insert_begin(&head, 40);
insert_begin(&head, 60);
insert_begin(&head, 20);
insert_begin(&head, 80);
insert_begin(&head, 50);
insert_begin(&head, 12);
display_list(head);
// [12]-->[50]-->[80]-->[20]-->[60]-->[40]-->[30]-->[10]-->NULL
// Creating a tree node (empty)
node *a;
convertLinkedList2BinaryTree(head, &a);
printf("\nThe Inorder Traversal Of the Converted Tree is: ");
printInorder(a);
// 12
// / \
// 50 80
// / \ / \
// 20 60 40 30
// /
// 10
// The Inorder Traversal Of the Converted Tree is: 10 20 50 60 12 40 80 30
}