Created
November 29, 2010 20:30
-
-
Save erenon/720555 to your computer and use it in GitHub Desktop.
mutant and balanced binary tree
This file contains hidden or 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
typedef struct _btree { | |
int value; | |
struct _btree *left; | |
struct _btree *right; | |
} btree; | |
/* TEST ONLY */ | |
btree *create_node(int value) { | |
btree *node; | |
node = (btree *)malloc(sizeof(btree)); | |
if (node == NULL) { exit(1); } | |
node->value = value; | |
node->left = NULL; | |
node->right = NULL; | |
return node; | |
} | |
/* TEST ONLY */ | |
int node_cmp(int a, int b) { | |
if (a > b) { | |
return -1; | |
} else if (a < b) { | |
return 1; | |
} | |
return 0; | |
} | |
/* TEST ONLY */ | |
void add_node(btree *root, btree *node) { | |
if (node_cmp(node->value, root->value) < 0) | |
{ | |
//new value is greater | |
if (root->right == NULL) { | |
root->right = node; | |
} else { | |
add_node(root->right, node); | |
} | |
} | |
else if (node_cmp(node->value, root->value) > 0) | |
{ | |
//new value is smaller | |
if (root->left == NULL) { | |
root->left = node; | |
} else { | |
add_node(root->left, node); | |
} | |
} | |
//do nothing if equal node found | |
} | |
/* TEST ONLY */ | |
void print_inorder(btree *root) { | |
if (root != NULL) { | |
print_inorder(root->left); | |
printf("%d ", root->value); | |
print_inorder(root->right); | |
} | |
} | |
/* TEST ONLY */ | |
void print_preorder(btree *root) { | |
if (root != NULL) { | |
printf("%d ", root->value); | |
print_inorder(root->left); | |
print_inorder(root->right); | |
} | |
} | |
/* TEST ONLY */ | |
void delete(btree *root) { | |
if (root != NULL) { | |
delete(root->left); | |
delete(root->right); | |
free(root); | |
} | |
} | |
int left_side_only(btree *root) { | |
if (root->left == NULL && root->right == NULL) { | |
return 1; | |
} else if (root->right != NULL) { | |
return 0; | |
} else { | |
return left_side_only(root->left); | |
} | |
} | |
int right_side_only(btree *root) { | |
if (root->right == NULL && root->left == NULL) { | |
return 1; | |
} else if (root->left != NULL) { | |
return 0; | |
} else { | |
return right_side_only(root->right); | |
} | |
} | |
int single_side_only(btree *root) { | |
return ( | |
root != NULL && | |
//XOR | |
( | |
(left_side_only(root) && !right_side_only(root)) | |
|| (!left_side_only(root) && right_side_only(root)) | |
) | |
); | |
} | |
int two_or_no_child(btree *root) { | |
if (root->left == NULL && root->right == NULL) { | |
return 1; | |
} else if ( | |
(root->left != NULL && root->right == NULL) | |
|| (root->right != NULL && root->left == NULL) | |
) { | |
return 0; | |
} else { | |
return ( | |
two_or_no_child(root->left) | |
&& two_or_no_child(root->right) | |
); | |
} | |
} | |
/* TEST ONLY */ | |
int main() { | |
btree | |
*mutant=NULL, | |
*simple=NULL, | |
*balanced=NULL, | |
*inbalanced=NULL; | |
mutant = create_node(0); | |
add_node(mutant, create_node(1)); | |
add_node(mutant, create_node(2)); | |
add_node(mutant, create_node(3)); | |
add_node(mutant, create_node(4)); | |
print_inorder(mutant); | |
printf("\nis mutant tree? %d\n", single_side_only(mutant)); | |
delete(mutant); | |
simple = create_node(0); | |
add_node(simple, create_node(4)); | |
add_node(simple, create_node(2)); | |
add_node(simple, create_node(3)); | |
add_node(simple, create_node(1)); | |
print_inorder(simple); | |
printf("\nis mutant tree? %d\n", single_side_only(simple)); | |
delete(simple); | |
balanced = create_node(4); | |
add_node(balanced, create_node(2)); | |
add_node(balanced, create_node(6)); | |
add_node(balanced, create_node(1)); | |
add_node(balanced, create_node(3)); | |
add_node(balanced, create_node(5)); | |
add_node(balanced, create_node(7)); | |
print_preorder(balanced); | |
printf("\nis balanced tree? %d\n", two_or_no_child(balanced)); | |
delete(balanced); | |
inbalanced = create_node(4); | |
add_node(inbalanced, create_node(2)); | |
add_node(inbalanced, create_node(6)); | |
add_node(inbalanced, create_node(1)); | |
add_node(inbalanced, create_node(3)); | |
add_node(inbalanced, create_node(5)); | |
print_preorder(inbalanced); | |
printf("\nis balanced tree? %d\n", two_or_no_child(inbalanced)); | |
delete(inbalanced); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment