博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二叉树的建树,按层遍历,结点总数,页结点,深度以及三序非递归遍历二叉树,建立中序线索二叉树...
阅读量:6256 次
发布时间:2019-06-22

本文共 4940 字,大约阅读时间需要 16 分钟。

二叉树的建树,按层遍历,结点总数,页结点以及深度, 以及三序非递归遍历二叉树;

建立一颗二叉树:

#include "handsomecui.h"typedef struct Node{    char data;    Node *left, *right;}Node, *Tree;Tree build(){    char c;    SI(c);    if(c == '#'){        return NULL;    }    else{        Node *root = new Node;        root->data = c;        root->left = build();        root->right = build();        return root;    }}void VisitTree(Node *root){    if(root == NULL){        return;    }        VisitTree(root->left);    VisitTree(root->right);    PI(root->data);}int main(){    Tree tree = build();    VisitTree(tree);    return 0;}

按层遍历:

#include "handsomecui.h"#include
typedef struct Node{ char data; Node *left, *right;}Node, *Tree;Tree build(){ char c; SI(c); if(c == '#'){ return NULL; } else{ Node *root = new Node; root->data = c; root->left = build(); root->right = build(); return root; }}void Floor_Visit(Node *root){ queue
Q; Q.push(root); while(!Q.empty()){ root = Q.front(); Q.pop(); PI(root->data); if(root->left != NULL) Q.push(root->left); if(root->right != NULL) Q.push(root->right); }}int main(){ Tree tree = build(); PI("按层访问的结果为:\n"); Floor_Visit(tree); return 0;}

结点总数,页结点以及深度:

#include "handsomecui.h"typedef struct Node{    char data;    Node *left, *right;}Node, *Tree;Tree build(){    char c;    SI(c);    if(c == '#'){        return NULL;    }    else{        Node *root = new Node;        root->data = c;        root->left = build();        root->right = build();        return root;    }}int Total_Node(Node *root){    if(root == NULL)        return 0;    else        return Total_Node(root->left) + Total_Node(root->right) + 1;}int Child_Node(Node *root){    if(root->left == NULL && root->right == NULL){        return 1;    }    else{        int temp = 0;        if(root->left != NULL)            temp += Child_Node(root->left);        if(root->right != NULL)            temp += Child_Node(root->right);        return temp;    }}int Tree_Depth(Node *root){    if(root == NULL)        return 0;    else        return max(Tree_Depth(root->left), Tree_Depth(root->right)) + 1;}int main(){    Tree tree = build();    PI("这棵二叉树的总结点为");    PI(Total_Node(tree));     PI("\n这棵二叉树的页结点为");    PI(Child_Node(tree));     PI("\n这棵二叉树的深度为");    PI(Tree_Depth(tree));     return 0;}

 三序非递归遍历二叉树:

#include "handsomecui.h"#include
typedef struct Node{ char data; Node *left, *right;}Node, *Tree;Tree build(){ char c; SI(c); if(c == '#'){ return NULL; } else{ Node *root = new Node; root->data = c; root->left = build(); root->right = build(); return root; }}void PreOder(Node *root){ stack
S; while(!S.empty() || root != NULL){ while(root != NULL){ S.push(root); PI(root->data); root = root->left; } if(!S.empty()){ root = S.top(); S.pop(); root = root->right; } }}void InOrder(Tree root){ stack
S; while(!S.empty() || root != NULL){ while(root != NULL){ S.push(root); root = root->left; } if(!S.empty()){ root = S.top(); PI(root->data); S.pop(); root = root->right; } } }void PostOrder(Tree root){ stack
S; S.push(root); Node *pre = NULL; while(!S.empty()){ root = S.top(); if((root->left == NULL && root->right == NULL) || (pre != NULL && (pre == root->left || pre == root->right))){ PI(root->data); S.pop(); pre = root; } else{ if(root->right != NULL){ S.push(root->right); } if(root->left != NULL){ S.push(root = root->left); } } }}int main(){ Tree tree = build(); PI("先序遍历为:"); PreOder(tree); PI("\n中序遍历为:"); InOrder(tree); PI("\n后序遍历为:"); PostOrder(tree); return 0;}

 中序线索二叉树:没有头,还不能遍历。。。

代码:

#include "handsomecui.h"typedef enum{    Link, Thread}PointTag;typedef struct Node{    char data;    Node *left, *right;    PointTag LTag, RTag;}Node, *Tree;Tree build(){    char c;    SI(c);    if(c == '#'){        return NULL;    }    else{        Node *root = new Node;        root->data = c;        root->left = build();        root->right = build();        return root;    }}Node *pre;void InThread(Tree root){    if(root == NULL)        return;    InThread(root->left);    if(root->left == NULL){        root->LTag = Thread;        root->left = pre;    }    if(root->right == NULL){        root->RTag = Thread;        pre->right = root;    }    pre = root;     InThread(root->right);}int main(){    Tree tree = build();    InThread(tree);    return 0;    }

 

转载地址:http://gkxsa.baihongyu.com/

你可能感兴趣的文章
安装Jenkins getting started卡住
查看>>
金软PDF转换(x-PDFConper)
查看>>
喵哈哈村的魔法考试 Round #15 (Div.2) 题解
查看>>
使用架构(XSD)验证XML文件
查看>>
Android开发之httpclient文件上传实现
查看>>
极客头条使用心得
查看>>
CSS解决无空格太长的字母,数字不会自己主动换行的问题
查看>>
日志打印longging模块(控制台和文件同时输出)
查看>>
这些年我们一起搞过的持续集成~Jenkins+Perl and Shell script
查看>>
php新版本号废弃 preg_replace /e 修饰符
查看>>
Android:Unable to resolve target ‘android-8’问题解决
查看>>
cocos2D(七)---- CCScene
查看>>
【DeepLearning】汉字手写体识别
查看>>
2017年中国大学生程序设计竞赛-中南地区赛暨第八届湘潭市大学生计算机程序设计大赛题解&源码(A.高斯消元,D,模拟,E,前缀和,F,LCS,H,Prim算法,I,胡搞,J,树状数组)...
查看>>
PostgreSQL 10首个测试版本发布
查看>>
ORACLE拼日期
查看>>
使用eclipse创建android项目的时候为什么会生成两个项目
查看>>
常见内存错误的几点总结
查看>>
Extjs的各版本下载
查看>>
使用LVS实现负载均衡原理及安装配置详解
查看>>