Skip to content

Instantly share code, notes, and snippets.

@bhatiaabhinav
Created June 30, 2014 07:59
Show Gist options
  • Save bhatiaabhinav/3e5192f600ae3c17f7f2 to your computer and use it in GitHub Desktop.
Save bhatiaabhinav/3e5192f600ae3c17f7f2 to your computer and use it in GitHub Desktop.
A Simple Application which demonstrates how to pretty print a tree
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct _Node;
typedef struct _Node Node;
struct _Node
{
int value;
Node* leftTree;
Node* rightTree;
Node* parent;
};
typedef Node* Tree;
Node* createNode(int val)
{
Node* pNode = (Node*)malloc(sizeof(Node));
pNode->value = val;
pNode->leftTree = NULL;
pNode->rightTree = NULL;
pNode->parent = NULL;
return pNode;
}
Tree AddValue(Tree t, int val)
{
if (t == NULL)
{
return createNode(val);
}
Node* pNewNode = createNode(val);
if (pNewNode->value <= t->value)
{
if (t->leftTree == NULL)
{
t->leftTree = createNode(val);
t->leftTree->parent = t;
}
else AddValue(t->leftTree, val);
}
else
{
if (t->rightTree == NULL)
{
t->rightTree = createNode(val);
t->rightTree->parent = t;
}
else AddValue(t->rightTree, val);
}
return t;
}
int GetHeight(Tree tree)
{
if (tree == NULL) return -1;
int height = 0;
if (tree->leftTree != NULL) height = 1 + GetHeight(tree->leftTree);
if (tree->rightTree != NULL)
{
int rightHeight = 1 + GetHeight(tree->rightTree);
height = rightHeight > height ? rightHeight : height;
}
return height;
}
int NumOfDigits(int n)
{
char str[10];
sprintf(str, "%d\0", n);
return strlen(str);
}
int GetWidth(Tree tree)
{
if (tree == NULL) return 0;
return NumOfDigits(tree->value) + GetWidth(tree->leftTree) + GetWidth(tree->rightTree);
}
void PrettyPrintRootOnLine(char** lines, int curHeight, Node* root, int start)
{
if (root == NULL) return;
char* line = lines[curHeight];
int wordWidth = NumOfDigits(root->value);
int wordStart = start + GetWidth(root->leftTree);
int wordEnd = wordStart + wordWidth - 1;
int end = wordEnd + GetWidth(root->rightTree);
int i;
int tempVal = root->value;
for (i = wordEnd; i >= wordStart; i--)
{
line[i] = tempVal % 10 + '0';
tempVal /= 10;
}
PrettyPrintRootOnLine(lines, curHeight + 1, root->leftTree, start);
PrettyPrintRootOnLine(lines, curHeight + 1, root->rightTree, wordEnd + 1);
}
void PrettyPrint(Tree tree)
{
int height = GetHeight(tree);
int width = GetWidth(tree);
char** lines;
lines = (char**)malloc((height + 1) * sizeof(char*));
int i;
for (i = 0; i <= height; i++)
{
lines[i] = (char*)malloc((width + 1) * sizeof(char));
int charNo;
for (charNo = 0; charNo < width; charNo++)
{
lines[i][charNo] = ' ';
}
lines[i][width] = '\0';
}
PrettyPrintRootOnLine(lines, 0, tree, 0);
for (i = 0; i <= height; i++) printf("%s\n", lines[i]);
}
int main()
{
Tree t = NULL;
t = AddValue(t, 5);
AddValue(t, 3);
AddValue(t, 20);
AddValue(t, 2);
AddValue(t, 1);
AddValue(t, 16);
AddValue(t, 18);
AddValue(t, 9);
AddValue(t, 14);
PrettyPrint(t);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment