這篇文章主要介紹了使用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
算法思想
遞歸思想,遞歸的終止條件是樹的長度len == 0
在中序遍歷的數組中找到前序數組的第一個字符,記錄在中序數組中的位置index.如果找不到,說明前序遍歷數組和中序遍歷數組有問題,提示錯誤信息,退出程序即可;找到index后,新建一個二叉樹節點t,t->item = *pre,然后遞歸的求t的左孩子和有孩子
遞歸的左孩子:void rebuildTree(pre + 1, in, index)
遞歸的右孩子:void rebuildTree(pre + (index + 1), in + (index + 1), len - (index + 1))
實現代碼(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
遞歸思路:
根據后序遍歷的特點,知道后序遍歷最后一個節點為根節點,即為A
觀察中序遍歷,A左側CBEDF為A左子樹節點,A后側HGJI為A右子樹節點
然后遞歸的構建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;
- }
新聞熱點
疑難解答