forked from devgoyal04/Hacktoberfest-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtree.cpp
72 lines (64 loc) · 1.65 KB
/
btree.cpp
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
#include <stdio.h>
#include <stdlib.h>
struct node
{
struct node *left_child,*right_child,*parent;
int key;
};
struct node* create_node(struct node* parent) // Creates a node and returns it's pointer
{
struct node *temp = (struct node*)malloc(sizeof(struct node));
temp->left_child = NULL;
temp->right_child = NULL;
temp->parent = parent;
return temp;
};
void insert(struct node* root, int key) // Insert a key to the given binary tree conforming to it's properties.
{
if(root->left_child == NULL && (key <= root->key))
{
struct node *temp = create_node(root);
root->left_child = temp;
temp->key = key;
return;
}
else
if(root->right_child == NULL && (key >= root->key))
{
struct node* temp = create_node(root);
root->right_child = temp;
temp->key = key;
return;
}
if(root->left_child!= NULL && (key < root->key))
return insert(root->left_child, key);
else
return insert(root->right_child, key);
}
int findMin( struct node* root) //Will return the minimum value (does not delete that node)
{
if(root->left_child == NULL)
return root->key;
else
findMin(root->left_child);
}
void deleteNode( struct node* n)
{
}
int main()
{
struct node tree_root;
tree_root.parent = NULL;
tree_root.key = 20;
FILE *data;
data = fopen("~/datastructures-and-algorithms/data","r+");
while (!feof (data))
{
int num;
fscanf (data, "%d ", &num);
insert(&tree_root,num);
}
fclose(data);
int k = findMin(&tree_root);
printf("Minumum is %d\n",k );
}