Created
January 24, 2014 17:58
-
-
Save mariusmg2/8602583 to your computer and use it in GitHub Desktop.
Some code from LSD course...
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> | |
| typedef struct node { | |
| int value; | |
| struct node *left, *right; | |
| } *BinaryIntTree; | |
| BinaryIntTree emptyBinaryIntTree = NULL; | |
| BinaryIntTree tree(BinaryIntTree left, int root, BinaryIntTree right) { | |
| BinaryIntTree tmp = malloc(sizeof(struct node)); | |
| tmp->value = root; | |
| tmp->left = left; | |
| tmp->right = right; | |
| return tmp; | |
| } | |
| BinaryIntTree left(BinaryIntTree a) { | |
| if (a == emptyBinaryIntTree) { | |
| printf("You try to access left for an empty tree!\n"); | |
| exit(0); | |
| } | |
| return a->left; | |
| } | |
| BinaryIntTree right(BinaryIntTree a) { | |
| if (a == emptyBinaryIntTree) { | |
| printf("You try to access right for an empty tree!\n"); | |
| exit(0); | |
| } | |
| return a->right; | |
| } | |
| int root(BinaryIntTree a) { | |
| if (a == emptyBinaryIntTree) { | |
| printf("You try to acess the root for an empty tree!\n"); | |
| exit(0); | |
| } | |
| return a->value; | |
| } | |
| void printBinaryIntTree(BinaryIntTree a) { | |
| static int last = 0; | |
| last++; | |
| printf("("); | |
| if (a != emptyBinaryIntTree) { | |
| printBinaryIntTree(a->left); | |
| printf(",%d,", a->value); | |
| printBinaryIntTree(a->right); | |
| } | |
| last--; | |
| printf(")"); | |
| if (last == 0) printf("\n"); | |
| } | |
| void inorder(BinaryIntTree a) { | |
| if (a != emptyBinaryIntTree) { | |
| inorder(left(a)); | |
| printf("%d ", root(a)); | |
| inorder(right(a)); | |
| } | |
| } | |
| void preorder(BinaryIntTree a) { | |
| if (a != emptyBinaryIntTree) { | |
| printf("%d ", root(a)); | |
| preorder(left(a)); | |
| preorder(right(a)); | |
| } | |
| } | |
| void postorder(BinaryIntTree a) { | |
| if (a != emptyBinaryIntTree) { | |
| postorder(left(a)); | |
| postorder(right(a)); | |
| printf("%d ", root(a)); | |
| } | |
| } | |
| BinaryIntTree evenTree(BinaryIntTree itree) { | |
| if (itree != emptyBinaryIntTree) { | |
| if (root(itree) % 2 == 0) { | |
| return tree(evenTree(left(itree)), root(itree), evenTree(right(itree))); | |
| } | |
| else { | |
| return tree(evenTree(left(itree)), 0, evenTree(right(itree))); | |
| } | |
| } | |
| } | |
| int main() { | |
| BinaryIntTree a = tree(tree(emptyBinaryIntTree, 2, emptyBinaryIntTree), 1, tree(tree(emptyBinaryIntTree, 4, emptyBinaryIntTree), 3, tree(emptyBinaryIntTree, 5, emptyBinaryIntTree))); | |
| printBinaryIntTree(a); | |
| printf("\nIn preordine: "); | |
| preorder(a); | |
| printf("\nIn inorder: "); | |
| inorder(a); | |
| printf("\nIn postordine: "); | |
| postorder(a); | |
| printf("\nEven binary tree from a: "); | |
| BinaryIntTree b = evenTree(a); | |
| printBinaryIntTree(b); | |
| getchar(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment