Last active
December 31, 2015 20:39
-
-
Save zetavg/8041694 to your computer and use it in GitHub Desktop.
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 <stdlib.h> | |
#include <stdio.h> | |
#include <math.h> | |
typedef struct bTreeNode *bTreePointer; | |
typedef struct bTreeNode { | |
char data; | |
bTreePointer leftChild, rightChild; | |
} bTreeNode; | |
bTreePointer newBTreeNode() | |
{ | |
bTreePointer a = malloc(sizeof(bTreeNode)); | |
a->data = 0; | |
return a; | |
} | |
void printBTree_visit(bTreePointer tree, int n, char arr[1000], int *s) | |
{ | |
if (*s < n) *s = n; | |
arr[n] = tree->data; | |
if (tree->leftChild) { | |
arr[n*2] = tree->leftChild->data; | |
printBTree_visit(tree->leftChild, n*2, arr, s); | |
} | |
if (tree->rightChild) { | |
arr[n*2+1] = tree->rightChild->data; | |
printBTree_visit(tree->rightChild, n*2+1, arr, s); | |
} | |
} | |
void printBTree(bTreePointer tree) | |
{ | |
printf("%c\n", tree->data); | |
char nodeData[1000]; | |
int size = 1; | |
int i; | |
for (i=0; i<1000; i++) { | |
nodeData[i] = 0; | |
} | |
printBTree_visit(tree, 1, nodeData, &size); | |
int depth; | |
if (size == 1) { | |
depth = 1; | |
} else { | |
depth = ceil(log(size)/log(2)); | |
} | |
printf("size: %d, depth: %d.\n\n", size, depth); | |
int blank[1000]; | |
blank[0] = 1; | |
blank[1] = 3; | |
for (i=2; i<=depth; i++) { | |
blank[i] = blank[i-1]*2 + 1; | |
} | |
int j, b; | |
for (i=1; i<=depth; i++) { | |
if (i > 1) { | |
for (b=0; b<(blank[depth-i]-1); b++) printf(" "); | |
for (j=pow(2,i-1); j<pow(2,i-1)*2; j++) { | |
if (j%2 == 0) { | |
if (nodeData[j]) printf(" /"); | |
else printf(" "); | |
} else { | |
if (nodeData[j]) printf("\\ "); | |
else printf(" "); | |
} | |
for (b=0; b<(blank[depth-i+1]-2); b++) printf(" "); | |
} | |
printf("\n"); | |
} | |
for (b=0; b<(blank[depth-i]); b++) printf(" "); | |
for (j=pow(2,i-1); j<pow(2,i-1)*2; j++) { | |
if (nodeData[j]) printf("%c", nodeData[j]); | |
else printf(" "); | |
for (b=0; b<(blank[depth-i+1]); b++) printf(" "); | |
} | |
printf("\n"); | |
} | |
printf("\nEND;\n"); | |
} | |
int main() | |
{ | |
bTreeNode a; | |
bTreePointer myTree = newBTreeNode(); | |
myTree->data = 'a'; | |
myTree->leftChild = newBTreeNode(); | |
myTree->leftChild->data = 'b'; | |
myTree->rightChild = newBTreeNode(); | |
myTree->rightChild->data = 'c'; | |
myTree->leftChild->leftChild = newBTreeNode(); | |
myTree->leftChild->leftChild->data = 'd'; | |
myTree->leftChild->rightChild = newBTreeNode(); | |
myTree->leftChild->rightChild->data = 'e'; | |
myTree->rightChild->leftChild = newBTreeNode(); | |
myTree->rightChild->leftChild->data = 'f'; | |
myTree->rightChild->rightChild = newBTreeNode(); | |
myTree->rightChild->rightChild->data = 'g'; | |
myTree->leftChild->leftChild->leftChild = newBTreeNode(); | |
myTree->leftChild->leftChild->leftChild->data = 'h'; | |
myTree->leftChild->leftChild->rightChild = newBTreeNode(); | |
myTree->leftChild->leftChild->rightChild->data = 'i'; | |
printBTree(myTree); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment