應用C說話構建根本的二叉樹數據構造。本站提示廣大學習愛好者:(應用C說話構建根本的二叉樹數據構造)文章只能為提供參考,不一定能成為您想要的結果。以下是應用C說話構建根本的二叉樹數據構造正文
二叉樹構造經常使用的一些初始化代碼
#include
#include
typedef struct Node{
int data;
Node *leftchild;
Node *rightchild;
}Node;
/*
初始化一棵二叉樹排序樹。
*/
void InitBinaryTree(Node**root,int elem)
{
*root=(Node*)malloc(sizeof(Node));
if(!(*root))
{
printf("Memory allocation for root failed.\n");
return;
}
(*root)->data=elem;
(*root)->leftchild=NULL;
(*root)->rightchild=NULL;
}
/*
向二叉樹排序樹中拔出節點。
*/
void InsertNode(Node *root,int elem)
{
Node *newnode=NULL;
Node *p=root,*last_p=NULL;
newnode=(Node*)malloc(sizeof(Node));
if(!newnode)
{
printf("Memory allocation for newnode failed.\n");
return;
}
newnode->data=elem;
newnode->leftchild=NULL;
newnode->rightchild=NULL;
while(NULL!=p)
{
last_p=p;
if(newnode->datadata)
{
p=p->leftchild;
}
else if(newnode->data>p->data)
{
p=p->rightchild;
}
else
{
printf("Node to be inserted has existed.\n");
free(newnode);
return;
}
}
p=last_p;
if(newnode->datadata)
{
p->leftchild=newnode;
}
else
{
p->rightchild=newnode;
}
}
/*
創立一棵二叉樹排序樹。
*/
void CreatBinarySearchTree(Node **root,int data[],int num)
{
int i;
for(i=0;i
{
if(NULL==*root)
{
InitBinaryTree(root,data[i]);
}
else
{
InsertNode(*root,data[i]);
}
}
}
依據前序序列、中序序列構建二叉樹
函數界說
bt rebuildTree(char *pre, char *in, int len);
參數:
* pre:前序遍歷成果的字符串數組
* in:中序遍歷成果的字符串數組
len : 樹的長度
例如:
前序遍歷成果: a b c d e f g h
中序遍歷成果: c b e d f a g h
算法思惟
完成代碼(c說話版)
/**
* Description:依據前序和中序構建二叉樹
*/
bt rebuildTree(char *pre, char *in, int len)
{
bt t;
if(len <= 0)
{
//遞歸終止
t = NULL;
}else
{
//遞歸主體
int index = 0;
while(index < len && *(pre) != *(in + index))
{
index ++;
}
if(index >= len)
{
printf("前序遍歷或許中序遍歷數組有成績!\n");
exit(-1);
}
t = (struct bintree *)malloc(sizeof(struct bintree));
t->item = *pre;
t->lchild = rebuildTree(pre + 1, in, index);
t->rchild = rebuildTree(pre + (index + 1), in + (index + 1), len - (index + 1));
}
return t;
}
依據中序序列、後序序列構建二叉樹
函數界說
/** * 中序、後序序列構建二叉樹 */ btree* rebuildTree(char *order, char *post, int len);
算法思惟
中序序列:C、B、E、D、F、A、H、G、J、I
後序序列:C、E、F、D、B、H、J、I、G、A
遞歸思緒:
完成代碼(c代碼)
/**
* 依據中序和後序序列構建二叉樹
* (ps:昨晚加入阿裡口試,比及最初說可以避免口試直接面試,明天估量照樣要依據黉捨挑選,哈哈,為了這點
* 也得加入阿裡口試,不克不及讓本身的黉捨遭到小看)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int n;
typedef struct btree {
struct btree *lchild;
struct btree *rchild;
char data;
} btree;
/**
* 中序、後序序列構建二叉樹
*/
btree* rebuildTree(char *order, char *post, int len)
{
btree *t;
if (len <= 0) {
return NULL;
} else {
int index = 0;
while (index < len && *(post + len - 1) != *(order + index)) {
index ++;
}
t = (btree *)malloc(sizeof(btree));
t->data = *(order + index);
t->lchild = rebuildTree(order, post, index);
t->rchild = rebuildTree(order + index + 1, post + index, len - (index + 1));
}
return t;
}
/**
* 前序遍歷二叉樹
*/
void preTraverse(btree *t)
{
if (t) {
printf("%c ", t->data);
preTraverse(t->lchild);
preTraverse(t->rchild);
}
}
int main(void)
{
int i;
char *post, *order;
btree *t;
while (scanf("%d", &n) != EOF) {
post = (char *)malloc(n);
order = (char *)malloc(n);
getchar();
for (i = 0; i < n; i ++)
scanf("%c", order + i);
getchar();
for (i = 0; i < n; i ++)
scanf("%c", post + i);
t = rebuildTree(order, post, n);
preTraverse(t);
printf("\n");
free(post);
free(order);
}
return 0;
}