Skip to content

Instantly share code, notes, and snippets.

@RdlP
Created June 6, 2017 19:00
Show Gist options
  • Save RdlP/8f0bfd4f14639f6af68fd7941b3c1776 to your computer and use it in GitHub Desktop.
Save RdlP/8f0bfd4f14639f6af68fd7941b3c1776 to your computer and use it in GitHub Desktop.
void print_tree_iterative(Node *root){
Stack *nodes = (Stack*)malloc(sizeof(Stack));
init_Stack(nodes);
push(nodes, root);
while (!empty(nodes)){
Node * node = pop(nodes);
if (node->right){
memcpy(node->right->code, node->code, node->depth);
node->right->code[node->depth] = 1;
push(nodes, node->right);
}
if (node->left){
memcpy(node->left->code, node->code, node->depth);
node->left->code[node->depth] = 0;
push(nodes, node->left);
}
if (!node->left && !node->right){
printf("0x%02X (%c): ", node->symbol, node->symbol);
printArr(node->code, node->depth);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment