From f41d01589c5da454cb271c6920fc66dd563c644e Mon Sep 17 00:00:00 2001 From: Wei Yang Date: Mon, 12 Nov 2018 10:08:38 +0800 Subject: [PATCH] binary tree in array --- c-cpp/23_binarytree/binarytree.c | 83 ++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 c-cpp/23_binarytree/binarytree.c diff --git a/c-cpp/23_binarytree/binarytree.c b/c-cpp/23_binarytree/binarytree.c new file mode 100644 index 00000000..4143cd17 --- /dev/null +++ b/c-cpp/23_binarytree/binarytree.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include + +/* Implement binary tree in array */ + +#define MAX_TREE_NODES (1 << 8) + +struct node { + int data; +}; + +struct binary_tree { + union { + unsigned long nodes; + struct node *n[MAX_TREE_NODES]; + }; +}; + +void init_binary_tree(struct binary_tree *tree) +{ + int i; + + for(i = 0; i < MAX_TREE_NODES; i++) { + tree->n[i] = NULL; + } +} + +struct node* create_node(int data) +{ + struct node* n; + + n = malloc(sizeof(struct node)); + + if (n) + n->data = data; + + return n; +} + +void fake_a_tree(struct binary_tree* tree) +{ + /* data is in ordered */ + int i, data[10] = {7, 4, 9, 2, 6, 8, 10, 1, 3, 5}; + + init_binary_tree(tree); + + /* root start at 1 */ + for (i = 0; i < 10; i++) + tree->n[i+1] = create_node(data[i]); + + tree->nodes = 10; +} + +void _in_order(struct binary_tree* tree, int index) +{ + if (!tree->n[index]) + return; + + /* left child at (index << 1) */ + _in_order(tree, index << 1); + + printf("[%2d]: %4d\n", index, tree->n[index]->data); + + /* right child at (index << 1) + 1 */ + _in_order(tree, (index << 1) + 1); +} + +void in_order(struct binary_tree* tree) +{ + _in_order(tree, 1); +} + +int main() +{ + struct binary_tree tree; + + fake_a_tree(&tree); + in_order(&tree); + return 0; +}