forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7119f47
commit f41d015
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
#include <string.h> | ||
#include <time.h> | ||
|
||
/* 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; | ||
} |