-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab6.c
112 lines (94 loc) · 1.63 KB
/
lab6.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
/*
Oscar Flores
CPTR 131
Spring 2015
Lab6
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct Tree{
int data;
struct Tree *left;
struct Tree *right;
}Tree;
Tree *Insert(Tree *node, int y)
{
if(node == NULL)
{
Tree *x;
x = (Tree *)malloc(sizeof(Tree));
x->data = y;
x->left = x->right = NULL;
return x;
}
if(y > (node->data)) //using > or < gets rid of the problem of having to check if somethings been used already
{
node->right = Insert(node->right,y);
}
else if(y < (node->data))
{
node->left = Insert(node->left, y);
}
return node;
}
//start here from here until it says "end here", functions were found at http://www.geeksforgeeks.org/618/
void printInorder(Tree *node)
{
if(node == NULL)
{
return;
}
printInorder(node->left);
printf("%d, ", node->data);
printInorder(node->right);
}
void printpreorder(Tree *node)
{
if(node == NULL)
{
return;
}
printf("%d, ", node->data);
printpreorder(node->left);
printpreorder(node->right);
}
void printpostorder(Tree *node) //
{
if(node == NULL)
{
return;
}
printpostorder(node->left);
printpostorder(node->right);
printf("%d, ", node->data);
}
//end here
void printnumbers(Tree *node)
{
printInorder(node);
printf("\n");
printpreorder(node);
printf("\n");
printpostorder(node);
}
void free_memory(Tree *node)
{
if(node == NULL)
{
return;
}
}
int main()
{
Tree *root = NULL;
int x;
while(x > 0) //for some reason this still returns the negative number along with the rest
{
printf("enter a number(negative to exit): ");
scanf("%d", &x);
root = Insert(root, x);
printnumbers(root);
printf("\n");
}
return 0;
}