#include "stdafx.h"
/*
** 主要功能:非递归的二叉树遍历
** 数据结构:链表实现的二叉树,在内存中分配的连续区域模拟数组实现的栈,栈中存放的是树结点的指针(地址)
**
**
**
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef int key_t;
typedef struct bt_node
{
key_t key;
struct bt_node * left;
struct bt_node * right;
};
typedef bt_node bt_node_t;
typedef struct bt
{
bt_node_t * root;
int nodes;
};
typedef bt bt_t;
typedef struct st
{
void ** base; //指向指针的指针
void ** top; //指向指针的指针
int size;
};
typedef st stack_t;
stack_t * get_stack(int size);
bt_node_t * make_key_node(key_t key);
bt_node_t * make_key_node(key_t key)
{
bt_node_t * result = NULL;
result = (bt_node_t *) malloc(sizeof(bt_node_t));
result->key = key;
result->left = NULL;
result->right = NULL;
return result;
}
void in_order_walk(bt_t * bt_p);
bt_node_t * get(stack_t * stack_p);
int put(bt_node_t * node_p, stack_t * stack_p);
int remove(stack_t * stack_p);
int is_empty_stack(stack_t * stack_p);
stack_t * get_stack(int size)
{
stack_t * stack_p = NULL;
stack_p = (stack_t *)malloc(sizeof(stack_t));
stack_p->base = (void **)malloc(sizeof(void *)*(size+1)); //为存储指针的数组分配空间
stack_p->top = stack_p->base;
memset(stack_p->base, 0, sizeof(void *) * (size+1));
stack_p->size = size;
return stack_p;
}
bt_node_t * get(stack_t * stack_p)
{
return (bt_node_t *)*(stack_p->top-1);
}
int put(bt_node_t * node_p, stack_t * stack_p)
{
*(stack_p->top) = node_p;
stack_p->top ++;
return 1;
}
int remove(stack_t * stack_p)
{
stack_p->top--;
*(stack_p->top) = NULL;
return 1;
}
int is_empty_stack(stack_t * stack_p)
{
return stack_p->base == stack_p->top ? 1 : 0;
}
int in_order_walk(bt_t * bt_p, stack_t * stack_p)
{
bt_node_t * root = NULL;
root = bt_p->root;
if(root == NULL)
{
printf("tree is empty!\n");
return -1;
}
do
{
if(root == NULL) //case 1: 当前结点是NULL,回溯到父结点,执行遍历操作,父结点出栈,然后处理父结点的右孩子
{
root = get(stack_p);
printf("%d\n", root->key);
remove(stack_p);
root = root->right;
}
else if(root != NULL) //case 2: 当前结点不是NULL,将当前结点入栈,然后处理当前结点的左孩子
{
put(root, stack_p);
root = root->left;
}
}while(is_empty_stack(stack_p) != 1 || root != NULL);
return 1;
}
int insert_bt(bt_t * bt_p, int key);
int insert_bt(bt_t * bt_p, int key)
{
if(bt_p == NULL)
{
printf("insert_bt error!\n");
return -1;
}
bt_node_t * tmp_node = NULL;
bt_node_t * x_node = NULL, * y_node = NULL;
x_node = make_key_node(key);
if(bt_p->root == NULL)
{
bt_p->root = x_node;
bt_p->nodes ++;
return 1;
}
tmp_node = bt_p->root;
while(tmp_node != NULL)
{
y_node = tmp_node;
if(key < tmp_node->key)
{
tmp_node = tmp_node->left;
}
else if(key >= tmp_node->key)
{
tmp_node = tmp_node->right;
}
}
if(key < y_node->key)
{
y_node->left = x_node;
}
else if(key >= y_node->key)
{
y_node->right = x_node;
}
bt_p->nodes ++;
return 1;
}
int main()
{
bt_t * bt_p = NULL;
bt_p = (bt_t *)malloc(sizeof(bt_t));
bt_p->root = NULL;
bt_p->nodes = 0;
int i, j;
int k;
srand (time(NULL));
for(i=18; i>0; --i)
{
j = rand();
printf("%d ", j%13);
insert_bt(bt_p, j%13);
}
printf("\n");
stack_t * stack_p = NULL;
stack_p = get_stack(bt_p->nodes);
in_order_walk(bt_p, stack_p);
return 1;
}