二叉树的构建及各种遍历回顾

直接上代码

 1 #ifndef TREE_H2 #define TREE_H3 4 #include <iostream>5 #include <malloc.h>6 7 typedef int ElemType;8 9 typedef struct Bintree
10 {
11     ElemType key;
12     struct Bintree* left;
13     struct Bintree* right;
14 }Bintree,*pBintree;
15
16
17 pBintree BT_Insert(ElemType target,pBintree* ppTree);
18 void BT_InOrder(pBintree root);
19 void BT_InOrderNoRec(pBintree root);
20 void BT_PreOrder(pBintree root);
21 void BT_PreOrderNoRec(pBintree root);
22 void BT_PostOrder(pBintree root);
23 void BT_PostOrderNoRec(pBintree root);
24 void BT_LevelOrder(pBintree root);
25
26 void BT_Destory(pBintree root);
27 #endif

基本函数实现

  1 /**********************************2  对二叉树的各种操作3  BY wei 2012-09-094 5  ***********************************/6 7 #include <queue>8 #include <stack>9 #include "bintree.h"10 using namespace std;11 12 // 访问13 static void visit(pBintree root)14 {15     if(root != NULL)16     {17         printf("%d\n",root->key);18     }19 }20 21 //生成一个新的节点22 static pBintree BT_MakeNode(ElemType target)23 {24     pBintree pNode = (pBintree)malloc(sizeof(Bintree));25     if(pNode == NULL)26     {27         printf("malloc failed");28         return NULL;29     }30     pNode->key = target;31     pNode->left = NULL;32     pNode->right = NULL;33     return pNode;34 }35 36 //插入37 pBintree BT_Insert(ElemType target,pBintree* ppTree)38 {39     pBintree Node;40     if(ppTree==NULL)41     {42         printf("failed\n");43         return NULL;44     }45     Node = *ppTree;46     if(Node == NULL)47     {48         printf("--**%d\n",target);49         return *ppTree = BT_MakeNode(target);50     }51     if(Node->key == target)    //不允许出现相同的元素52     {53         return NULL;54     }55     else if(Node->key > target)56     {57         //printf("--left:%d\n",target);58         return BT_Insert(target,&Node->left);59     }60     else61     {62         //printf("----right:%d\n",target);63         return BT_Insert(target, &Node->right);64     }65 66 }67 68 //中序遍历的递归实现69 void BT_InOrder(pBintree root)70 {71     if(root != NULL)72     {73         BT_InOrder(root->left);74             visit(root);75             BT_InOrder(root->right);76     }77 }78 //中序遍历的非递归实现79 void BT_InOrderNoRec(pBintree root)80 {81     stack<pBintree>    s;82     83     while((root != NULL) || (!s.empty()) )84     {85         if(root != NULL)86         {87             s.push(root);88             root = root->left;89         }90         else91         {92             root = s.top();93             visit(root);94             s.pop();95             root = root->right;96         }97     }98 }99
100 //前序遍历的递归实现
101 void BT_PreOrder(pBintree root)
102 {
103     if(root != NULL)
104     {
105         visit(root);
106         BT_PreOrder(root->left);
107         BT_PreOrder(root->right);
108     }
109 }
110 //前序遍历的非递归实现
111 void BT_PreOrderNoRec(pBintree root)
112 {
113     stack<pBintree>    s;
114
115     while((root != NULL) || (!s.empty()) )
116     {
117         if(root != NULL)
118         {
119             visit(root);
120             s.push(root);
121             root = root->left;
122         }
123         else
124         {
125             root = s.top();
126             s.pop();
127             root = root->right;
128         }
129     }
130 }
131
132
133
134 //后序遍历的递归实现
135 void BT_PostOrder(pBintree root)
136 {
137     if(root != NULL)
138     {
139         BT_PostOrder(root->left);
140         BT_PostOrder(root->right);
141         visit(root);
142     }
143 }
144 //后序遍历的非递归实现
145 void BT_PostOrderNoRec(pBintree root)
146 {
147     stack<pBintree> s;
148     pBintree pre = NULL;
149     pBintree top = NULL;
150     while((root != NULL) || (!s.empty()))
151     {
152         if(root != NULL)
153         {
154             s.push(root);
155             root = root->left;
156         }
157         else
158         {
159             top = s.top();
160             if(top->right != NULL && top->right != pre)
161                 root = top->right;
162             else
163             {
164                 visit(top);
165                 pre = top;
166                 s.pop();
167             }
168         }
169     }
170 }
171
172 //层次遍历的实现
173 void BT_LevelOrder(pBintree root)
174 {
175     queue<pBintree> q;
176     pBintree ptree;
177
178     if(root==NULL) return;
179
180     q.push(root);
181
182     while(!q.empty())
183     {
184         ptree = q.front();
185         q.pop();
186         visit(ptree);
187         if(ptree->left != NULL)
188         {
189             q.push(ptree->left);
190         }
191         if(ptree->right != NULL)
192         {
193             q.push(ptree->right);
194         }
195     }
196 }
197
198 void BT_Destory(pBintree root)
199 {
200     stack<pBintree>    s;
201      pBintree top = NULL;
202
203     while((root != NULL) || (!s.empty()) )
204     {
205         if(root != NULL)
206         {
207             s.push(root);
208             root = root->left;
209         }
210         else
211         {
212             top = root = s.top();
213             s.pop();
214             root = root->right;
215             if(top != NULL){ free(top);top=NULL;}
216         }
217     }
218 }

main函数

 1 #include <iostream>2 #include <time.h>3 #include "bintree.h"4 5 #define MAX_CNT 106 #define BASE 1007 8 int main()9 {
10     int i;
11     pBintree root = NULL;
12
13     srand((unsigned)time(NULL));
14
15     for(i=0; i<MAX_CNT; i++)
16     {
17         BT_Insert(rand()%BASE,&root);
18     }
19
20     //前序
21     printf("PreOrder\n");
22     BT_PreOrder(root);
23
24     printf("\n");
25     BT_PreOrderNoRec(root);
26     printf("---------------------\n");
27
28
29     //中序
30     printf("InOrder:\n");
31     BT_InOrder(root);
32     printf("\n");
33     BT_InOrderNoRec(root);
34     printf("_____________________\n");
35
36     //后序
37     printf("PostOrder\n");
38     BT_PostOrder(root);
39     printf("\n");
40     BT_PostOrderNoRec(root);
41     printf("======================\n");
42
43     //层次
44     printf("BY LEVELOrder\n");
45     BT_LevelOrder(root);
46     printf("\n");
47
48
49     //destory
50     BT_Destory(root);
51     return 0;
52 }

参考http://www.cppblog.com/ngaut/archive/2011/10/19/2351.html

cppblog上的东西转不过来,自己把代码敲了一遍

posted on 2012-09-09 20:54 anywei 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/anywei/archive/2012/09/09/bitree.html

二叉树的构建及各种遍历回顾相关推荐

  1. 二叉树的前序非递归遍历

    二叉树的前序非递归遍历 前面学习过二叉树的前序遍历,使用递归的方式.简单回顾一下: Status PerOrder(BiTree T) {//前序遍历二叉树if (T != NULL) {Visit( ...

  2. 二叉树前中后序遍历的非递归实现以及层次遍历、zig-zag型遍历详解

    前言 二叉树的遍历是一个比较常见的问题,递归实现二叉树的前中后序遍历比较简单,但非递归实现二叉树的前中后序遍历相对有难度.这篇博客将详述如何使用非递归的方式实现二叉树的前中后序遍历,在进行理论描述的同 ...

  3. 二叉树非递归先序遍历

    二叉树的递归先序遍历很简单,假设二叉树的结点定义如下: 1 struct BinaryTreeNode 2 { 3 int m_nValue; 4 BinaryTreeNode* m_pLeft; 5 ...

  4. 常考数据结构与算法:二叉树的之字形层序遍历

    题目描述 给定一个二叉树,返回该二叉树的之字形层序遍历,(第一层从左向右,下一层从右向左,一直这样交替) 例如: 给定的二叉树是{3,9,20,#,#,15,7}, 该二叉树之字形层序遍历的结果是 [ ...

  5. Python实现二叉树的三种深度遍历方法!

    python代码实现了二叉树,这次将会实现二叉树的几种遍历方法,来更好的解析二叉树的结构特点.分别是一种广度遍历,和三种深度遍历方法:先序遍历,中序遍历,后序遍历.下面是代码实现: 1.先序遍历 遍历 ...

  6. LeetCode Algorithm 103. 二叉树的锯齿形层序遍历

    103. 二叉树的锯齿形层序遍历 Ideas 首先得理解二叉树的层序遍历,它类似于广度优先搜索,在当前层搜索的时候,遍历到的每一个节点都要把它的所有孩子节点都添加到队列中. 然后我们要锯齿形遍历,可以 ...

  7. lintcode二叉树的锯齿形层次遍历 (双端队列)

    题目链接: http://www.lintcode.com/zh-cn/problem/binary-tree-zigzag-level-order-traversal/ 二叉树的锯齿形层次遍历 给出 ...

  8. 牛客题霸 [二叉树的之字形层序遍历] C++题解/答案

    牛客题霸 [二叉树的之字形层序遍历] C++题解/答案 题目描述 给定一个二叉树,返回该二叉树的之字形层序遍历,(第一层从左向右,下一层从右向左,一直这样交替) 例如: 给定的二叉树是{3,9,20, ...

  9. 103. 二叉树的锯齿形层次遍历/102. 二叉树的层序遍历

    2020-05-24 1.题目描述 二叉树的锯齿形层次遍历 2.题解 对于层次遍历而言,就是广度优先,由于题目要求奇数层逆序,我们可以 1)使用双端队列,奇偶性不同,则出入队列方式不同. 2)对于偶数 ...

最新文章

  1. 我泡在GitHub上的177天 by Ryan Seys
  2. PriorityBlockingQueue用法
  3. Abp框架准备加入.NET Foundation
  4. 课时66.颜色控制属性下(理解)
  5. 银行工作中有哪些需要注意的事?
  6. NAME:WRECK 漏洞影响近亿台物联网设备
  7. 1052. 卖个萌 (20)-PAT乙级真题
  8. 蓝桥杯2017年第八届C/C++省赛B组第二题-等差素数列
  9. 常用JQUERY插件大全
  10. 思科模拟器GNS3将路由器变成交换机的方法
  11. 【Large Scale Adversarial Representation Learning 大规模对抗学习(BigGAN) 】学习笔记
  12. 胡泳滨maya python
  13. 添加WhatsApp链接教程
  14. 2018年迎春杯复赛入围名单(五年级)
  15. Tangent Space的基向量计算方法
  16. qcow2和vmdk互相转
  17. Markdown语法介绍(详细)
  18. C# winform cefsharp 截取网页元素图片
  19. CSS3实现骗人版无缝轮播图
  20. 详细不啰嗦,电脑重装系统win10教程分享

热门文章

  1. 通向财务自由之路02_成功的决定因素:你
  2. 信号角度分析评估因子表现(无限资金回测模型)
  3. linux mount_nodev函数,mount()函数 Unix/Linux
  4. 这个技能,让可视化大屏开挂一样的秀!
  5. web报表工具FineReport最经常用到部分函数详解
  6. cas ajax请求重定向,cas 单点登录 .net client mvc 重定向问题
  7. 台达ms300变频器使用手册中文_台达变频器:满足未来驱动需求
  8. php 没有权限,PHP出现操作文件没有权限怎么办?
  9. 计算机算法设计与分析 数字三角形
  10. M - Corporate Identity(多组字符串匹配问题)