Skip to content

Commit 6d5ba9e

Browse files
Merge pull request #593 from sanu-020/main
Some Tree programs added in C
2 parents 2a603d4 + 9ba1993 commit 6d5ba9e

File tree

4 files changed

+584
-0
lines changed

4 files changed

+584
-0
lines changed

Trees/BST.C

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct Node
5+
{
6+
struct Node *lchild;
7+
int data;
8+
struct Node *rchild;
9+
} *root = NULL;
10+
11+
void Insert(int key)
12+
{
13+
struct Node *t = root;
14+
struct Node *r, *p;
15+
16+
if (root == NULL)
17+
{
18+
p = (struct Node *)malloc(sizeof(struct Node));
19+
p->data = key;
20+
p->lchild = p->rchild = NULL;
21+
root = p;
22+
return;
23+
}
24+
while (t != NULL)
25+
{
26+
r = t;
27+
if (key < t->data)
28+
t = t->lchild;
29+
else if (key > t->data)
30+
t = t->rchild;
31+
else
32+
return;
33+
}
34+
p = (struct Node *)malloc(sizeof(struct Node));
35+
p->data = key;
36+
p->lchild = p->rchild = NULL;
37+
38+
if (key < r->data)
39+
r->lchild = p;
40+
else
41+
r->rchild = p;
42+
}
43+
44+
void Inorder(struct Node *p)
45+
{
46+
if (p)
47+
{
48+
Inorder(p->lchild);
49+
printf("%d ", p->data);
50+
Inorder(p->rchild);
51+
}
52+
}
53+
54+
struct Node *Search(int key)
55+
{
56+
struct Node *t = root;
57+
58+
while (t != NULL)
59+
{
60+
if (key == t->data)
61+
return t;
62+
else if (key < t->data)
63+
t = t->lchild;
64+
else
65+
t = t->rchild;
66+
}
67+
return NULL;
68+
}
69+
70+
struct Node *RInsert(struct Node *p, int key)
71+
{
72+
struct Node *t = NULL;
73+
if (p == NULL)
74+
{
75+
t = (struct Node *)malloc(sizeof(struct Node));
76+
t->data = key;
77+
t->lchild = t->rchild = NULL;
78+
return t;
79+
}
80+
if (key < p->data)
81+
p->lchild = RInsert(p->lchild, key);
82+
else if (key > p->data)
83+
p->rchild = RInsert(p->rchild, key);
84+
return p;
85+
}
86+
87+
int Height(struct Node *p)
88+
{
89+
int x, y;
90+
if (p == NULL)
91+
return 0;
92+
x = Height(p->lchild);
93+
y = Height(p->rchild);
94+
return x > y ? x + 1 : y + 1;
95+
}
96+
97+
struct Node *InPre(struct Node *p)
98+
{
99+
while (p && p->rchild != NULL)
100+
p = p->rchild;
101+
return p;
102+
}
103+
104+
struct Node *InSucc(struct Node *p)
105+
{
106+
while (p && p->lchild != NULL)
107+
p = p->lchild;
108+
return p;
109+
}
110+
111+
struct Node *Delete(struct Node *p, int key)
112+
{
113+
struct Node *q = NULL;
114+
if (p == NULL)
115+
return NULL;
116+
if (p->lchild == NULL && p->rchild == NULL)
117+
{
118+
if (p == root)
119+
root = NULL;
120+
free(p);
121+
return NULL;
122+
}
123+
124+
if (key < p->data)
125+
p->lchild = Delete(p->lchild, key);
126+
else if (key > p->data)
127+
p->rchild = Delete(p->rchild, key);
128+
else
129+
{
130+
if (Height(p->lchild) > Height(p->rchild))
131+
{
132+
q = InPre(p->lchild);
133+
p->data = q->data;
134+
p->lchild = Delete(p->lchild, q->data);
135+
}
136+
else
137+
{
138+
q = InSucc(p->rchild);
139+
p->data = q->data;
140+
p->rchild = Delete(p->rchild, q->data);
141+
}
142+
}
143+
return p;
144+
}
145+
146+
int main()
147+
{
148+
struct Node *temp;
149+
150+
root = RInsert(root, 10);
151+
RInsert(root, 10);
152+
RInsert(root, 40);
153+
RInsert(root, 20);
154+
RInsert(root, 30);
155+
RInsert(root, 25);
156+
RInsert(root, 35);
157+
RInsert(root, 55);
158+
159+
Delete(root, 30);
160+
161+
Inorder(root);
162+
printf("\n");
163+
164+
temp = Search(20);
165+
if (temp != NULL)
166+
printf("Element %d is found\n", temp->data);
167+
else
168+
{
169+
printf("Element is not found\n");
170+
}
171+
172+
return 0;
173+
}

Trees/Binary_tree_Array.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <stdio.h>
2+
3+
4+
int complete_node = 15;
5+
6+
char tree[] = {'\0', 'D', 'A', 'F', 'E', 'B', 'R', 'T', 'G', 'Q', '\0', '\0', 'V', '\0', 'J', 'L'};
7+
8+
// funtion to get parent
9+
int get_parent(int index)
10+
{
11+
if(tree[index]!='\0' && index>1 && index<=complete_node) //root has no parent
12+
return index/2;
13+
return -1;
14+
}
15+
16+
int get_right_child(int index)
17+
{
18+
// node is not null
19+
// and the result must lie within the number of nodes for a complete binary tree
20+
if(tree[index]!='\0' && ((2*index)+1)<=complete_node)
21+
return (2*index)+1;
22+
// right child doesn't exist
23+
return -1;
24+
}
25+
26+
int get_left_child(int index)
27+
{
28+
// node is not null
29+
// and the result must lie within the number of nodes for a complete binary tree
30+
if(tree[index]!='\0' && (2*index)<=complete_node)
31+
return 2*index;
32+
// left child doesn't exist
33+
return -1;
34+
}
35+
36+
void preorder(int index)
37+
{
38+
// checking for valid index and null node
39+
if(index>0 && tree[index]!='\0')
40+
{
41+
printf(" %c ",tree[index]); // visiting root
42+
preorder(get_left_child(index)); //visiting left subtree
43+
preorder(get_right_child(index)); //visiting right subtree
44+
}
45+
}
46+
47+
void postorder(int index)
48+
{
49+
// checking for valid index and null node
50+
if(index>0 && tree[index]!='\0')
51+
{
52+
postorder(get_left_child(index)); //visiting left subtree
53+
postorder(get_right_child(index)); //visiting right subtree
54+
printf(" %c ",tree[index]); //visiting root
55+
}
56+
}
57+
58+
void inorder(int index)
59+
{
60+
// checking for valid index and null node
61+
if(index>0 && tree[index]!='\0')
62+
{
63+
inorder(get_left_child(index)); //visiting left subtree
64+
printf(" %c ",tree[index]); //visiting root
65+
inorder(get_right_child(index)); // visiting right subtree
66+
}
67+
}
68+
69+
int is_leaf(int index)
70+
{
71+
// to check of the indices of the left and right children are valid or not
72+
if(!get_left_child(index) && !get_right_child(index))
73+
return 1;
74+
// to check if both the children of the node are null or not
75+
if(tree[get_left_child(index)]=='\0' && tree[get_right_child(index)]=='\0')
76+
return 1;
77+
return 0; // node is not a leaf
78+
}
79+
80+
int get_max(int a, int b)
81+
{
82+
return (a>b) ? a : b;
83+
}
84+
85+
int get_height(int index)
86+
{
87+
// if the node is a leaf the the height will be 0
88+
// the height will be 0 also for the invalid cases
89+
if(tree[index]=='\0' || index<=0 || is_leaf(index))
90+
return 0;
91+
// height of node i is 1+ maximum among the height of left subtree and the height of right subtree
92+
return(get_max(get_height(get_left_child(index)), get_height(get_right_child(index)))+1);
93+
}

0 commit comments

Comments
 (0)