(c语言)二叉树中序线索(数据结构十七)
生活随笔
收集整理的這篇文章主要介紹了
(c语言)二叉树中序线索(数据结构十七)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.數(shù)據(jù)類型定義
3.線索二叉樹代碼
在代碼中為了清楚的表示一些錯誤和函數(shù)運行狀態(tài),我們預先定義一些變量來表示這些狀態(tài)。在head.h頭文件中有如下定義:
//定義數(shù)據(jù)結(jié)構(gòu)中要用到的一些變量和類型 #ifndef HEAD_H #define HEAD_H#include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <math.h>#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 //分配內(nèi)存出錯typedef int Status; //函數(shù)返回值類型 typedef int ElemType; //用戶定義的數(shù)據(jù)類型#endif 2.樹的頭文件 BiTree.h代碼如下:
#ifndef BITREE_H #define BITREE_H#include "head.h"//Link為指針 Thread為線索 typedef enum PointerTag {Link,Thread};typedef struct BiNode{ElemType data;struct BiNode *left,*right;int LTag,RTag; //左右標志 }BiNode,*pBiNode;Status InsertRight(pBiNode &root,ElemType e); Status InsertLeft(pBiNode &root,ElemType e);Status InitBiTree(pBiNode &tree){tree=(pBiNode)malloc(sizeof(BiNode));if(!tree) return OVERFLOW;tree->data=-999999;tree->left=NULL;tree->right=NULL;return OK; } Status BiTreeEmpty(pBiNode root){if(root==NULL) return ERROR;return root->left==root->right && root->data==-999999; }Status HasNoNode(pBiNode root){if(root==NULL) return ERROR;return root->left==root->right ; }Status CreatTreeNode(pBiNode &node,ElemType e){node=(pBiNode)malloc(sizeof(BiNode));if(!node) return OVERFLOW;node->data=e;node->left=NULL;node->right=NULL;return OK; } Status InsertRight(pBiNode &root,ElemType e){if(root->right==NULL){if(e>root->data){pBiNode p;CreatTreeNode(p,e);root->right=p;return OK;}else{pBiNode p;CreatTreeNode(p,e);root->left=p;return OK;}}else{e>root->data? InsertRight(root->right,e):InsertLeft(root,e);}} Status InsertLeft(pBiNode &root,ElemType e){if(root->left==NULL){if(e>root->data){pBiNode p;CreatTreeNode(p,e);root->right=p;return OK;}else{pBiNode p;CreatTreeNode(p,e);root->left=p;return OK;}}else{e<=root->data?InsertLeft(root->left,e):InsertRight(root,e);}}Status InsertTree(pBiNode &root,ElemType e){if(BiTreeEmpty(root)){root->data=e;return true;}if(e>root->data){InsertRight(root,e);}else{InsertLeft(root,e);} }Status CreateBiTree(pBiNode &root,ElemType *a,int n){for (int i=0;i<n;i++){InsertTree(root,a[i]);}return true; }Status print(ElemType e ){printf("%d ",e);return true;}Status PreOrderTraverse(pBiNode root,Status(*p)(int)){if(root){(*p)(root->data);PreOrderTraverse(root->left,p);PreOrderTraverse(root->right,p);}return OK; }Status MiddleOrderTraverse(pBiNode root,Status(*p)(int)){if(root){MiddleOrderTraverse(root->left,p);(*p)(root->data);MiddleOrderTraverse(root->right,p);}return OK; }Status AfterOrderTraverse(pBiNode root,Status(*p)(int)){if(root){AfterOrderTraverse(root->left,p);AfterOrderTraverse(root->right,p);(*p)(root->data);}return OK; }Status ClearBiTree(pBiNode &root){if(root){ClearBiTree(root->left);ClearBiTree(root->right);free(root);root==NULL;}return OK; }#endif
3.線索二叉樹代碼
#include "BiTree.h"//中序線索 void InOrder( pBiNode root,pBiNode &pre){if (root!=NULL){InOrder(root->left,pre);if (root->left==NULL){root->left=pre;root->LTag=1;}if (pre!=NULL && pre->right==NULL){pre->right=root;pre->RTag=1;}pre=root;InOrder(root->right,pre);} } //線索 void CreateInOrder(pBiNode& root){pBiNode pre=NULL;if(root!=NULL){InOrder(root,pre);pre->right=NULL;pre->RTag=1;} }//獲取頭指針 pBiNode FirstNode(pBiNode root){while (root->LTag!=1){root=root->left;}return root; } //獲取下一個節(jié)點 pBiNode NextNode(pBiNode root){if (root->RTag!=1){return FirstNode(root->right);}else{return root->right;} } //遍歷線索 void InOrder(pBiNode root){for (pBiNode p=FirstNode(root);p!=NULL;p=NextNode(p)){printf("%d ",p->data);} }void main(){ElemType a[14]={100,50,200,40,30,45,60,55,61,200,150,300,250,400};pBiNode root;InitBiTree(root);CreateBiTree(root,a,14);printf("前序:");PreOrderTraverse(root,print);printf("\n中序:");MiddleOrderTraverse(root,print);printf("\n后序:");AfterOrderTraverse(root,print);CreateInOrder(root);printf("\n線索二叉樹中序:");InOrder(root);printf("\n");ClearBiTree(root);} 4.測試結(jié)果
前序:100 50 40 30 45 60 55 61 200 150 300 250 400 中序:30 40 45 50 55 60 61 100 150 200 250 300 400 后序:30 45 40 55 61 60 50 150 250 400 300 200 100 線索二叉樹中序:30 40 45 50 55 60 61 100 150 200 250 300 400
轉(zhuǎn)載于:https://www.cnblogs.com/whzhaochao/p/5023492.html
總結(jié)
以上是生活随笔為你收集整理的(c语言)二叉树中序线索(数据结构十七)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从状态转移看:载波侦听多路访问/冲突避免
- 下一篇: 大家注意:升级 win8.1 火狐浏览器