二叉树 —— 创建二叉树 先序遍历 、中序遍历、后序遍历(递归方式、非递归方式)
生活随笔
收集整理的這篇文章主要介紹了
二叉树 —— 创建二叉树 先序遍历 、中序遍历、后序遍历(递归方式、非递归方式)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef char DataType;
#define MaxSize 100
typedef struct Node
{DataType data;struct Node *lchild;struct Node *rchild;
}*BiTree,BitNode;
void InitBitTree(BiTree *T);/*樹的初始化*/
void CreaBitTree(BiTree *T);/*按照先序輸入字符序列遞歸創建二叉樹*/
void PreOrderTraverse(BiTree T);/*二叉樹的先序遍歷的遞歸函數聲明*/
void InOrderTraverse(BiTree T);/*二叉樹的中序遍歷的遞歸函數聲明*/
void PostOrderTraverse(BiTree T);/*二叉樹的后序遍歷的遞歸函數聲明*/
void PreOrderTraverse2(BiTree T);/*二叉樹的先序遍歷的非遞歸函數聲明*/
void InOrderTraverse2(BiTree T);/*二叉樹的中序遍歷的非遞歸函數聲明*/
void PostOrderTraverse2(BiTree T);/*二叉樹的后序遍歷的非遞歸函數聲明*/
void main()
{BiTree T,root;InitBitTree(&T);printf("根據輸入二叉樹的先序序列創建二叉樹('#'表示結束):\n");CreaBitTree(&T);printf("二叉樹的先序遍歷:\n");printf("遞歸方式:\t");PreOrderTraverse(T);printf("\n");printf("非遞歸方式:\t");PreOrderTraverse2(T);printf("\n");printf("二叉樹的中序遍歷:\n");printf("遞歸方式:\t");InOrderTraverse(T);printf("\n");printf("非遞歸方式:\t");InOrderTraverse2(T);printf("\n");printf("二叉樹的后序遍歷:\n");printf("遞歸方式:\t");PostOrderTraverse(T);printf("\n");printf("非遞歸方式:\t");PostOrderTraverse2(T);printf("\n");}
void InitBitTree(BiTree *T)
{*T = NULL;
}
void CreaBitTree(BiTree *T)
{DataType ch;scanf(" %c",&ch);if(ch == '#')*T = NULL;else{*T = (BiTree)malloc(sizeof(BitNode));//生成根節點if(!(*T))exit(-1);(*T)->data = ch;CreaBitTree(&((*T)->lchild));//構造左子樹CreaBitTree(&((*T)->rchild));//構造右子樹}
}
void PreOrderTraverse(BiTree T)
{if(T)//如果二叉樹不為空{printf("%2c",T->data);//訪問根節點PreOrderTraverse(T->lchild);//先序遍歷左子樹PreOrderTraverse(T->rchild);//先序遍歷右子樹}
}
void InOrderTraverse(BiTree T)
{if(T)//如果二叉樹不為空{InOrderTraverse(T->lchild);//中序遍歷左子樹printf("%2c",T->data);//訪問根節點InOrderTraverse(T->rchild);//中序遍歷右子樹}
}
void PostOrderTraverse(BiTree T)
{if(T){PostOrderTraverse(T->lchild);//后序遍歷左子樹PostOrderTraverse(T->rchild);//后序遍歷右子樹printf("%2c",T->data);//訪問根節點}
}
void PreOrderTraverse2(BiTree T)
{BiTree stack[MaxSize]; //定義一個棧 用于存放結點的指針int top;//定義棧頂指針BitNode *p; //定義一個節點的指針top = 0; //初始化棧p = T;while(p != NULL || top>0){while(p != NULL) //如果p不是空,訪問根節點,遍歷左子樹{printf("%2c",p->data); //訪問根節點stack[top++] = p; //將p入棧p = p->lchild; //遍歷左子樹}if(top > 0) //如果棧不為空{p = stack[--top]; //棧頂元素出棧p = p->rchild; //遍歷右子樹}}}
void InOrderTraverse2(BiTree T)
{BiTree stack[MaxSize];int top;BitNode *p;top = 0;p = T;while(p != NULL || top > 0){while(p != NULL){stack[top++] = p;p = p->lchild;}if(top > 0){p = stack[--top];printf("%2c",p->data);p = p->rchild;}}
}
void PostOrderTraverse2(BiTree T)
{BiTree stack[MaxSize];int top;BitNode *p,*q;top = 0;p = T,q = NULL;while(p != NULL || top > 0){while(p != NULL){stack[top++] = p;p = p->lchild;}if(top > 0){p = stack[top-1];//取棧頂元素if(p->rchild == NULL || p->rchild ==q) //如果p沒有右孩子結點,或者右孩子結點已經訪問過{printf("%2c",p->data); //訪問根節點q = p;p = NULL;top--;}elsep = p->rchild;}}}
~
~
//創建一個存儲整數的二叉樹 //首先 創建一個跟節點 所有節點的創建方式都相同 //這個函數返回指向新建Node對象的指針,要為新的二叉樹創建根節點,可以使用這個函數 struct Node *createnode(long value) {struct Node *pNode = (struct Node *)malloc(sizeof(struct Node));pNode->item = value; //set the valuepNode->count = 1;pNode->pLeft = pNode->pRight = NULL;return pNode; } long newvalue; printf("Enter the node value:"); //從鍵盤上讀取存儲值后 scanf("%ld",newvalue); //調用函數 在堆上創建一個新的節點 不要忘記使用完畢以后釋放節點內存 struct Node *pRoot = createnode(newvalue); //二叉樹是應用遞歸的一個領域,插入節點的過程涉及到以相同的方式查看一系列節點,這里使用遞歸的一個強烈的暗示。 //用下面的函數在樹中添加一個已有的節點 add a new node to a tree struct Node *addnod(long value,struct Node* pNode) {if(pNode == NULL)return createnode(value);//把根節點作為第二個變元傳送時//如果value等于當前節點的值,就不需要創建新節點,只遞增當前節點中的計數器,并返回該節點if(value == pNode->item){++pNode -> count;return pNode;}//如果value小于當前節點的值,就需要查看左子節點,如果左節點的指針是NULL,就創建一個包含value的新節點,使之成為左子節點。如果左節點存在,就遞歸調用addnod()函數,把指向左子節點的指針作為第二個變元。if(value < pNode->item){if(pNode->left == NULL){pNode->pLeft = createnode(value);return pNode->pLeft;}else{return addnode(value,pNode->pLeft);}}else{if(pNode->pRight == NULL){pNode->pRight = createnode(value);return pNode->pRight}else{return addnode(value,pNode->pRight);}} }//無論調用遞歸函數時執行了什么,該函數都返回一個插入值的節點的指針,這可能時一個新節點,也可以是一個其值已經存在于樹中的節點 do {printf("Enter the node value:");scanf(" %ld",&newvalue);if(pRoot == NULL)pRoot = createnode(newvalue);elseaddnode(newvalue,pRoot);printf("\nDo you want to enter another(y or n)");scanf(" %c",&answer); }while(totlwer(answer) == 'y');
?
[root@J01051386 Test_20180418]# ./a.out
根據輸入二叉樹的先序序列創建二叉樹('#'表示結束):
e#
r#
G#
#
二叉樹的先序遍歷:
遞歸方式:?? ? e r G
非遞歸方式:?? ? e r G
二叉樹的中序遍歷:
遞歸方式:?? ? e r G
非遞歸方式:?? ? e r G
二叉樹的后序遍歷:
遞歸方式:?? ? G r e
非遞歸方式:?? ? G r e
//創建一個存儲整數的二叉樹 //首先 創建一個跟節點 所有節點的創建方式都相同 //這個函數返回指向新建Node對象的指針,要為新的二叉樹創建根節點,可以使用這個函數 struct Node *createnode(long value) {struct Node *pNode = (struct Node *)malloc(sizeof(struct Node));pNode->item = value; //set the valuepNode->count = 1;pNode->pLeft = pNode->pRight = NULL;return pNode; } long newvalue; printf("Enter the node value:"); //從鍵盤上讀取存儲值后 scanf("%ld",newvalue); //調用函數 在堆上創建一個新的節點 不要忘記使用完畢以后釋放節點內存 struct Node *pRoot = createnode(newvalue); //二叉樹是應用遞歸的一個領域,插入節點的過程涉及到以相同的方式查看一系列節點,這里使用遞歸的一個強烈的暗示。 //用下面的函數在樹中添加一個已有的節點 add a new node to a tree struct Node *addnod(long value,struct Node* pNode) {if(pNode == NULL)return createnode(value);//把根節點作為第二個變元傳送時//如果value等于當前節點的值,就不需要創建新節點,只遞增當前節點中的計數器,并返回該節點if(value == pNode->item){++pNode -> count;return pNode;}//如果value小于當前節點的值,就需要查看左子節點,如果左節點的指針是NULL,就創建一個包含value的新節點,使之成為左子節點。如果左節點存在,就遞歸調用addnod()函數,把指向左子節點的指針作為第二個變元。if(value < pNode->item){if(pNode->left == NULL){pNode->pLeft = createnode(value);return pNode->pLeft;}else{return addnode(value,pNode->pLeft);}}else{if(pNode->pRight == NULL){pNode->pRight = createnode(value);return pNode->pRight}else{return addnode(value,pNode->pRight);}} }//無論調用遞歸函數時執行了什么,該函數都返回一個插入值的節點的指針,這可能時一個新節點,也可以是一個其值已經存在于樹中的節點 do {printf("Enter the node value:");scanf(" %ld",&newvalue);if(pRoot == NULL)pRoot = createnode(newvalue);elseaddnode(newvalue,pRoot);printf("\nDo you want to enter another(y or n)");scanf(" %c",&answer); }while(totlwer(answer) == 'y');
總結
以上是生活随笔為你收集整理的二叉树 —— 创建二叉树 先序遍历 、中序遍历、后序遍历(递归方式、非递归方式)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 结构体的嵌套 自身嵌套 相互嵌套
- 下一篇: 函数 —— fgets()