Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor hash tweaks, new binary tree interface (incomplete)
- Loading branch information
1 parent
0ec2b4e
commit 7a2a932
Showing
8 changed files
with
192 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef TREE_H_ | ||
#define TREE_H_ | ||
|
||
typedef int (*bintree_cmp)(void *, void *); | ||
|
||
struct btnode { | ||
struct btnode *left; | ||
struct btnode *right; | ||
void *data; | ||
}; | ||
|
||
struct binary_tree { | ||
struct btnode *root; | ||
bintree_cmp cmp_fn; | ||
}; | ||
|
||
struct binary_tree *init_bintree(bintree_cmp cmp_fn); | ||
|
||
void destroy_bintree(struct binary_tree *bt); | ||
|
||
void *bintree_search(struct binary_tree *bt, void *data); | ||
|
||
int bintree_insert(struct binary_tree *bt, void *data); | ||
|
||
#endif /* TREE_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include "tree.h" | ||
|
||
|
||
int intcmp(void *a, void *b) { | ||
return *(int *)a - *(int *)b; | ||
} | ||
|
||
void inorder(struct btnode *b) | ||
{ | ||
if(b == NULL) | ||
return; | ||
inorder(b->left); | ||
printf("Node at %p: %d (left -> %p, right -> %p)\n", | ||
b, *(int *)(b->data), b->left, b->right); | ||
inorder(b->right); | ||
} | ||
|
||
void postorder(struct btnode *b) | ||
{ | ||
if(b == NULL) | ||
return; | ||
postorder(b->left); | ||
postorder(b->right); | ||
printf("Node at %p: %d (left -> %p, right -> %p)\n", | ||
b, *(int *)(b->data), b->left, b->right); | ||
} | ||
|
||
void preorder(struct btnode *b) | ||
{ | ||
if(b == NULL) | ||
return; | ||
printf("Node at %p: %d (left -> %p, right -> %p)\n", | ||
b, *(int *)(b->data), b->left, b->right); | ||
preorder(b->left); | ||
preorder(b->right); | ||
} | ||
|
||
void inorder_dot(struct btnode *b) | ||
{ | ||
if(b == NULL) | ||
return; | ||
inorder_dot(b->left); | ||
if(b->left) | ||
printf("\t%d -> %d\n", *(int *)b->data, *(int *)b->left->data); | ||
if(b->right) | ||
printf("\t%d -> %d\n", *(int *)b->data, *(int *)b->right->data); | ||
inorder_dot(b->right); | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int a[] = {5, 31, 2, 9, 24, 12, 7, 19, 30, 35, 2, 4, 0}; | ||
int *p = a; | ||
struct binary_tree *bt; | ||
|
||
bt = init_bintree(&intcmp); | ||
|
||
while(*p) | ||
bintree_insert(bt, p++); | ||
|
||
puts("digraph tree {"); | ||
inorder_dot(bt->root); | ||
puts("}\n"); | ||
|
||
destroy_bintree(bt); | ||
|
||
return 0; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
add_library(bintree SHARED tree.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include "tree.h" | ||
|
||
|
||
struct binary_tree *init_bintree(bintree_cmp cmp_fn) | ||
{ | ||
struct binary_tree *b = malloc(sizeof(struct binary_tree)); | ||
if(b != NULL) { | ||
b->cmp_fn = cmp_fn; | ||
b->root = NULL; | ||
} | ||
return b; | ||
} | ||
|
||
static void freetree(struct btnode *p) | ||
{ | ||
if(p == NULL) | ||
return; | ||
freetree(p->left); | ||
freetree(p->right); | ||
free(p); | ||
} | ||
|
||
void destroy_bintree(struct binary_tree *bt) | ||
{ | ||
struct btnode *n = bt->root; | ||
freetree(bt->root); | ||
//free(bt->root); | ||
free(bt); | ||
} | ||
|
||
static struct btnode **btn_srch(struct binary_tree *bt, void *data) | ||
{ | ||
struct btnode **n = &(bt->root); | ||
int res; | ||
|
||
while(*n != NULL) { | ||
res = bt->cmp_fn(data, (*n)->data); | ||
if(res < 0) { | ||
n = &(*n)->left; | ||
} else if (res > 0) { | ||
n = &(*n)->right; | ||
} else { | ||
break; | ||
} | ||
} | ||
return n; | ||
} | ||
|
||
void *bintree_search(struct binary_tree *bt, void *data) | ||
{ | ||
struct btnode **n = btn_srch(bt, data); | ||
if(*n != NULL) | ||
return (*n)->data; | ||
else | ||
return *n; | ||
} | ||
|
||
int bintree_insert(struct binary_tree *bt, void *data) | ||
{ | ||
struct btnode **n = btn_srch(bt, data); | ||
if(*n == NULL) { | ||
*n = malloc(sizeof(struct btnode)); | ||
if(*n == NULL) | ||
return -1; | ||
(*n)->left = (*n)->right = NULL; | ||
(*n)->data = data; | ||
return 0; | ||
} | ||
return 1; | ||
} | ||
|
||
|