使用C语言构建基本的二叉树数据结构
发布时间:2020-12-16 03:17:21 所属栏目:百科 来源:网络整理
导读:二叉树结构常用的一些初始化代码 #include#includetypedef struct Node{ int data; Node *leftchild; Node *rightchild;}Node;/* 初始化一棵二叉树排序树。*/void InitBinaryTree(Node**root,int elem){ *root=(Node*)malloc(sizeof(Node)); if(!(*root)) {
二叉树结构常用的一些初始化代码 #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);
算法思想
实现代码(c语言版)
/** * Description:根据前序和中序构建二叉树 */ bt rebuildTree(char *pre,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,index); t->rchild = rebuildTree(pre + (index + 1),len - (index + 1)); } return t; }
/** * 中序、后序序列构建二叉树 */ btree* rebuildTree(char *order,char *post,int len);
后序序列:C、E、F、D、B、H、J、I、G、A 递归思路:
/** * 根据中序和后序序列构建二叉树 * (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,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,n); preTraverse(t); printf("n"); free(post); free(order); } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |