blob: 1d1792328e9ff5e75e7a7f76c130694f8c19fbab (
plain)
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
|
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#include "list.h"
struct node
{
struct node* nextS;
struct node* nextB;
int data;
};
struct node* root;
struct node** getSmallest(struct node** _root)
{
struct node** current = _root;
while((*current)->nextS)
current = &((*current)->nextS);
return current;
}
struct node removeNode(int _data)
{
struct node **current = &root;
struct node *removeNode = NULL;
struct node **newRoot = NULL;
struct node returnNode;
while((*current)->data != _data)
current = (*current)->data < _data ? &(*current)->nextB : &(*current)->nextS;
printf("node to remove:%i\n", (*current)->data);
removeNode = *current;
if(removeNode->nextB)
{
newRoot = getSmallest(&removeNode->nextB);
*current = *newRoot;
*newRoot = (*newRoot)->nextB;
(*current)->nextS = removeNode->nextS;
(*current)->nextB = removeNode->nextB;
}
else
*current = (*current)->nextS;;
returnNode = *removeNode;
free(removeNode);
return returnNode;
}
void insert(int _data)
{
struct node** current = &root;
struct node* new = malloc(sizeof(*new));
new->nextS = NULL;
new->nextB = NULL;
new->data=_data;
while (*current)
current = (*current)->data > _data ? &((*current)->nextS) : &((*current)->nextB);
*current = new;
}
void draw(void)
{
struct node* current = root;
struct node* stackTop = NULL;
struct stack* stackpointer = NULL;
int depth = 0;
stack_init(&stackpointer);
start:
while (current)
{
stack_add( (void*) current , &stackpointer);
int i;
for(i = 0; i < depth; i++)
printf(" ");
printf("|:%i\n", current->data);
current = current->nextS;
depth++;
}
nextinstack:
stackTop = (struct node*)stack_get(&stackpointer);
if(!stackTop)
return;
current = stackTop->nextB;
if(current)
goto start;
depth --;
goto nextinstack;
}
void balance()
{
struct node *current = root;
struct node *stackTop = NULL;
struct stack *stackpointer = NULL;
struct list *listroot = NULL;
stack_init(&stackpointer);
list_init(&listroot);
//Create Inorder List
start:
while(current)
{
stack_add( (void*) current, &stackpointer);
current = current->nextS;
}
stackTop = (struct stack*) stack_pop(&stackpointer);
stackTop ? current = stackTop->nextB :;
if(current)
{
list_add(current->data, &listroot);
goto start;
}
//TODO Balance
}
int main(int argc, char* argv[])
{
root = NULL;
insert(7);
insert(5);
insert(10);
insert(11);
insert(1);
//draw();
balance();
printf("balanced");
//removeNode(7);
//draw();
printf("%i is the smallest obejct\n", (*getSmallest(&root))->data);
return 0;
}
|