使用C語言構(gòu)建基本的二叉樹數(shù)據(jù)結(jié)構(gòu)
二叉樹結(jié)構(gòu)常用的一些初始化代碼
#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; } /* 向二叉樹排序樹中插入節(jié)點(diǎn)。 */ 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; } } /* 創(chuàng)建一棵二叉樹排序樹。 */ 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]); } } }
根據(jù)前序序列、中序序列構(gòu)建二叉樹
函數(shù)定義
bt rebuildTree(char *pre, char *in, int len);
參數(shù):
* pre:前序遍歷結(jié)果的字符串?dāng)?shù)組
* in:中序遍歷結(jié)果的字符串?dāng)?shù)組
len : 樹的長度
例如:
前序遍歷結(jié)果: a b c d e f g h
中序遍歷結(jié)果: c b e d f a g h
算法思想
- 遞歸思想,遞歸的終止條件是樹的長度len == 0
- 在中序遍歷的數(shù)組中找到前序數(shù)組的第一個(gè)字符,記錄在中序數(shù)組中的位置index.如果找不到,說明前序遍歷數(shù)組和中序遍歷數(shù)組有問題,提示錯(cuò)誤信息,退出程序即可;找到index后,新建一個(gè)二叉樹節(jié)點(diǎn)t,t->item = *pre,然后遞歸的求t的左孩子和有孩子
- 遞歸的左孩子:void rebuildTree(pre + 1, in, index)
- 遞歸的右孩子:void rebuildTree(pre + (index + 1), in + (index + 1), len - (index + 1))
實(shí)現(xiàn)代碼(c語言版)
/** * Description:根據(jù)前序和中序構(gòu)建二叉樹 */ 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("前序遍歷或者中序遍歷數(shù)組有問題!\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; }
根據(jù)中序序列、后序序列構(gòu)建二叉樹
函數(shù)定義
/** * 中序、后序序列構(gòu)建二叉樹 */ 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
遞歸思路:
- 根據(jù)后序遍歷的特點(diǎn),知道后序遍歷最后一個(gè)節(jié)點(diǎn)為根節(jié)點(diǎn),即為A
- 觀察中序遍歷,A左側(cè)CBEDF為A左子樹節(jié)點(diǎn),A后側(cè)HGJI為A右子樹節(jié)點(diǎn)
- 然后遞歸的構(gòu)建A的左子樹和后子樹
實(shí)現(xiàn)代碼(c代碼)
/** * 根據(jù)中序和后序序列構(gòu)建二叉樹 * (ps:昨晚參加阿里筆試,等到最后說可以免筆試直接面試,今天估計(jì)還是要根據(jù)學(xué)校篩選,哈哈,為了這點(diǎn) * 也得參加阿里筆試,不能讓自己的學(xué)校受到鄙視) */ #include <stdio.h> #include <stdlib.h> #include <string.h> int n; typedef struct btree { struct btree *lchild; struct btree *rchild; char data; } btree; /** * 中序、后序序列構(gòu)建二叉樹 */ 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; }
上一篇:與ASCII碼相關(guān)的C語言字符串操作函數(shù)
欄 目:C語言
本文標(biāo)題:使用C語言構(gòu)建基本的二叉樹數(shù)據(jù)結(jié)構(gòu)
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/2865.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?