aboutsummaryrefslogtreecommitdiff
path: root/sem3/algo/mm6
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2020-10-12 16:26:42 +0200
committerJulian T <julian@jtle.dk>2020-10-12 16:54:48 +0200
commit4e05a55e373bd315e721d534a1711fec4c0054c5 (patch)
tree4fc0141760c2730ed79aaa4c7aa60ea45cc66cc0 /sem3/algo/mm6
parentb7f9cf43c8a9ab3400cbb30d5e1cadb0c6c2cf23 (diff)
Moved to linux kernel inspired clang-format
Diffstat (limited to 'sem3/algo/mm6')
-rw-r--r--sem3/algo/mm6/main.c40
-rw-r--r--sem3/algo/mm6/tree.c215
-rw-r--r--sem3/algo/mm6/tree.h17
3 files changed, 133 insertions, 139 deletions
diff --git a/sem3/algo/mm6/main.c b/sem3/algo/mm6/main.c
index a93571e..be1b2a4 100644
--- a/sem3/algo/mm6/main.c
+++ b/sem3/algo/mm6/main.c
@@ -4,35 +4,33 @@
int main()
{
- tree_t t;
- t.root = 0;
+ tree_t t;
+ t.root = 0;
- node_t *a = tree_insert(&t, 1, "Hej");
+ node_t *a = tree_insert(&t, 1, "Hej");
- node_t *b = tree_insert(&t, 3, "med");
+ node_t *b = tree_insert(&t, 3, "med");
- tree_insert(&t, 11, "dig");
- tree_insert(&t, 9, "dig");
- tree_insert(&t, 12, "dig");
+ tree_insert(&t, 11, "dig");
+ tree_insert(&t, 9, "dig");
+ tree_insert(&t, 12, "dig");
- tree_insert(&t, 10, "hvordan");
+ tree_insert(&t, 10, "hvordan");
- tree_insert(&t, 8, "det");
+ tree_insert(&t, 8, "det");
- tree_insert(&t, 4, "branch");
+ tree_insert(&t, 4, "branch");
- tree_insert(&t, 5, "2");
+ tree_insert(&t, 5, "2");
+ tree_insert(&t, 0, "Og den sidste");
- tree_insert(&t, 0, "Og den sidste");
-
- tree_insert(&t, 2, "Cool nok");
- tree_print(&t);
-
- printf("%s\n", tree_search(&t, 10));
- printf("%s\n", tree_search(&t, 11));
- printf("%s\n", tree_search(&t, 1));
- printf("%s\n", tree_search(&t, 0));
- printf("%s\n", tree_search(&t, 99));
+ tree_insert(&t, 2, "Cool nok");
+ tree_print(&t);
+ printf("%s\n", tree_search(&t, 10));
+ printf("%s\n", tree_search(&t, 11));
+ printf("%s\n", tree_search(&t, 1));
+ printf("%s\n", tree_search(&t, 0));
+ printf("%s\n", tree_search(&t, 99));
}
diff --git a/sem3/algo/mm6/tree.c b/sem3/algo/mm6/tree.c
index 4d58309..89b6bbe 100644
--- a/sem3/algo/mm6/tree.c
+++ b/sem3/algo/mm6/tree.c
@@ -6,161 +6,158 @@
static node_t *create_node(unsigned int index, char *val)
{
- node_t *node = malloc( sizeof(node_t) );
- memset(node, 0, sizeof(node_t));
+ node_t *node = malloc(sizeof(node_t));
+ memset(node, 0, sizeof(node_t));
- node->value = val;
- node->index = index;
+ node->value = val;
+ node->index = index;
}
/* Comments are based of dir = CHILD_LEFT */
void node_rotate(tree_t *tree, node_t *x, int dir)
{
- /* Node below which we should rotate with */
- node_t *y = x->children[!dir];
-
- /* Move y's left to x's right */
- x->children[!dir] = y->children[dir];
-
- /* Update parent */
- if ( y->children[dir] ) {
- y->children[dir]->p = x;
- }
-
- /* Switch around x and y */
- y->p = x->p;
-
- /* Check if y is new root if not update x's parent */
- if ( !y->p ) {
- tree->root = y;
- } else if ( x == x->p->children[CHILD_LEFT]) {
- x->p->children[CHILD_LEFT] = y;
- } else {
- x->p->children[CHILD_RIGHT] = y;
- }
-
- /* Update y's ref */
- y->children[dir] = x;
- x->p = y;
-
+ /* Node below which we should rotate with */
+ node_t *y = x->children[!dir];
+
+ /* Move y's left to x's right */
+ x->children[!dir] = y->children[dir];
+
+ /* Update parent */
+ if (y->children[dir]) {
+ y->children[dir]->p = x;
+ }
+
+ /* Switch around x and y */
+ y->p = x->p;
+
+ /* Check if y is new root if not update x's parent */
+ if (!y->p) {
+ tree->root = y;
+ } else if (x == x->p->children[CHILD_LEFT]) {
+ x->p->children[CHILD_LEFT] = y;
+ } else {
+ x->p->children[CHILD_RIGHT] = y;
+ }
+
+ /* Update y's ref */
+ y->children[dir] = x;
+ x->p = y;
}
static void insert_fixup(tree_t *tree, node_t *z)
{
- while ( z->p && z->p->black == false ) {
- /* Set the right direction */
- int dir = (z->p == z->p->p->children[CHILD_LEFT]) ? CHILD_RIGHT : CHILD_LEFT;
-
- /* Get uncle */
- node_t *y = z->p->p->children[dir];
- if ( y && !y->black ) {
- /* Case 1 */
- z->p->black = true;
- y->black = true;
- z->p->p->black = false;
-
- z = z->p->p;
- } else if ( z == z->p->children[dir] ) {
- /* Case 2 */
- z = z->p;
- node_rotate(tree, z, !dir);
- } else {
- /* Case 3 */
- z->p->black = true;
- z->p->p->black = false;
- node_rotate(tree, z->p->p, dir);
- }
- }
-
- tree->root->black = true;
-
+ while (z->p && z->p->black == false) {
+ /* Set the right direction */
+ int dir =
+ (z->p == z->p->p->children[CHILD_LEFT]) ? CHILD_RIGHT : CHILD_LEFT;
+
+ /* Get uncle */
+ node_t *y = z->p->p->children[dir];
+ if (y && !y->black) {
+ /* Case 1 */
+ z->p->black = true;
+ y->black = true;
+ z->p->p->black = false;
+
+ z = z->p->p;
+ } else if (z == z->p->children[dir]) {
+ /* Case 2 */
+ z = z->p;
+ node_rotate(tree, z, !dir);
+ } else {
+ /* Case 3 */
+ z->p->black = true;
+ z->p->p->black = false;
+ node_rotate(tree, z->p->p, dir);
+ }
+ }
+
+ tree->root->black = true;
}
static node_t *insert_recur(node_t *node, unsigned int index, char *val)
{
- int dir = node->index < index ? CHILD_RIGHT : CHILD_LEFT;
+ int dir = node->index < index ? CHILD_RIGHT : CHILD_LEFT;
- if ( node->children[dir] ) {
- return insert_recur(node->children[dir], index, val);
- }
+ if (node->children[dir]) {
+ return insert_recur(node->children[dir], index, val);
+ }
- /* Found stop */
- node_t *new = create_node(index, val);
- new->p = node;
- node->children[dir] = new;
+ /* Found stop */
+ node_t *new = create_node(index, val);
+ new->p = node;
+ node->children[dir] = new;
- return new;
+ return new;
}
node_t *tree_insert_pleb(tree_t *tree, unsigned int index, char *val)
{
- if ( !tree->root ) {
- tree->root = create_node(index, val);
- return tree->root;
- }
+ if (!tree->root) {
+ tree->root = create_node(index, val);
+ return tree->root;
+ }
- return insert_recur(tree->root, index, val);
+ return insert_recur(tree->root, index, val);
}
node_t *tree_insert(tree_t *tree, unsigned int index, char *val)
{
- if ( !tree->root ) {
- tree->root = create_node(index, val);
- tree->root->black = true;
- return tree->root;
- }
+ if (!tree->root) {
+ tree->root = create_node(index, val);
+ tree->root->black = true;
+ return tree->root;
+ }
- node_t *new = insert_recur(tree->root, index, val);
- insert_fixup(tree, new);
+ node_t *new = insert_recur(tree->root, index, val);
+ insert_fixup(tree, new);
- return new;
+ return new;
}
char *tree_search(tree_t *tree, unsigned int index)
{
- node_t *z = tree->root;
- while ( z->index != index ) {
- z = z->children[z->index < index ? CHILD_RIGHT : CHILD_LEFT];
- if ( !z ) {
- return NULL;
- }
- }
-
- return z->value;
+ node_t *z = tree->root;
+ while (z->index != index) {
+ z = z->children[z->index < index ? CHILD_RIGHT : CHILD_LEFT];
+ if (!z) {
+ return NULL;
+ }
+ }
+
+ return z->value;
}
static inline void print_indent(int num)
{
- for ( int i = 0; i < num; i++ ) {
- printf(" ");
- }
+ for (int i = 0; i < num; i++) {
+ printf(" ");
+ }
}
static inline void print_text(node_t *node, bool bold)
{
- if ( bold ) {
- printf("\033[1m%d\033[22m\n", node->index);
- } else {
- printf("%d\n", node->index);
- }
-
+ if (bold) {
+ printf("\033[1m%d\033[22m\n", node->index);
+ } else {
+ printf("%d\n", node->index);
+ }
}
static void print_recur(node_t *node, int ident)
{
- if ( !node ) {
- return;
- }
- print_recur(node->children[CHILD_RIGHT], ident+1);
-
- print_indent(ident);
- print_text(node, node->black);
-
- print_recur(node->children[CHILD_LEFT], ident+1);
+ if (!node) {
+ return;
+ }
+ print_recur(node->children[CHILD_RIGHT], ident + 1);
+
+ print_indent(ident);
+ print_text(node, node->black);
+
+ print_recur(node->children[CHILD_LEFT], ident + 1);
}
void tree_print(tree_t *tree)
{
- print_recur(tree->root, 0);
+ print_recur(tree->root, 0);
}
-
diff --git a/sem3/algo/mm6/tree.h b/sem3/algo/mm6/tree.h
index 0d1c5c6..d94304f 100644
--- a/sem3/algo/mm6/tree.h
+++ b/sem3/algo/mm6/tree.h
@@ -6,16 +6,16 @@
#define CHILD_LEFT 0
#define CHILD_RIGHT 1
-typedef struct node_struct{
- struct node_struct *p;
- struct node_struct *children[2];
- unsigned int index;
- bool black;
- char *value;
+typedef struct node_struct {
+ struct node_struct *p;
+ struct node_struct *children[2];
+ unsigned int index;
+ bool black;
+ char *value;
} node_t;
typedef struct {
- node_t *root;
+ node_t *root;
} tree_t;
void tree_print(tree_t *tree);
@@ -27,5 +27,4 @@ void node_rotate(tree_t *tree, node_t *x, int dir);
char *tree_search(tree_t *tree, unsigned int index);
-
-#endif
+#endif