二叉树的创建与遍历(非递归遍历左右中,不破坏树结构)

创建

二叉树的递归3种遍历方式:

1,先中心,再左树,再右树

2,先左树,再中心,再右树

3,先左树,再右树,再中心

二叉树的非递归4种遍历方式:

1,先中心,再左树,再右树

2,先左树,再中心,再右树

3,先左树,再右树,再中心

4,层级遍历

二叉树的查找,求高度,求个数,求父节点,复制二叉树,释放二叉树

编译方法,用gcc编译不过去,用g++编译,命令如下:

g++ -g nodestack.c nodequeue.c bintree.c bintreemain.c

bintree.h

#ifndef __BINTREE__
#define __BINTREE__ #include <stdio.h>
#include <malloc.h>
#include <assert.h> #define ElemType char typedef struct BinTreeNode{
ElemType data;
struct BinTreeNode* leftChild;
struct BinTreeNode* rightChild;
}BinTreeNode; typedef struct BinTree{
BinTreeNode* root;
ElemType ref;
}BinTree; void init(BinTree* tr, ElemType val);
void createBinTree(BinTree* bt);
void createBinTree_str(BinTree* bt, char** str); //先中心,再左树,再右树
void show_clr(BinTree* tr);
//先左树,再中心,再右树
void show_lcr(BinTree* tr);
//先左树,再右树,再中心
void show_lrc(BinTree* tr);
//层级遍历
void show_level(BinTree* tr); //二叉树的查找方法1
int get_size1(BinTree* tr);
//二叉树的查找方法2
int get_size2(BinTree* tr);
//求二叉树的高度
int get_height(BinTree* tr);
//查找二叉树
BinTreeNode* search(BinTree* tr, ElemType key);
//求某个节点的父节点
BinTreeNode* get_parent(BinTree* tr, BinTreeNode* p);
//树是否为空
bool isBintreeEmpty(BinTree* tr);
//复制一课二叉树
void copy(BinTree* tr1, BinTree* tr2);
//释放二叉树
void bintree_clear(BinTree* tr); //先中心,再左树,再右树
void display_clr(BinTree* tr);
//先左树,再中心,再右树
void display_lcr(BinTree* tr);
//先左树,再右树,再中心
void display_lrc(BinTree* tr);
//先左树,再右树,再中心
void display_lrc1(BinTree* tr); #endif

bintree.c

#include "bintree.h"
#include "nodequeue.h"
#include "nodestack.h" void init(BinTree* tr, ElemType val){
tr->root = NULL;
tr->ref = val;
} void createRoot(BinTree* bt, BinTreeNode** t){
ElemType item;
scanf("%c", &item);
if(item == bt->ref){
*t = NULL;
}
else{
*t = (BinTreeNode*)malloc(sizeof(BinTreeNode));
assert(*t != NULL);
(*t)->data = item;
createRoot(bt, &((*t)->leftChild));
createRoot(bt, &((*t)->rightChild));
}
} void createBinTree(BinTree* bt){
createRoot(bt, &(bt->root));
} void createNode_str(BinTree* bt, BinTreeNode** t, char** str){ if(**str == '\0'){
return;
} if(**str == bt->ref){
*t = NULL;
*str = *str + 1;
}
else{
*t = (BinTreeNode*)malloc(sizeof(BinTreeNode));
(*t)->data = **str;
*str = *str + 1;
createNode_str(bt, &((*t)->leftChild), str);
createNode_str(bt, &((*t)->rightChild), str);
}
} void createBinTree_str(BinTree* bt, char** str){
createNode_str(bt, &(bt->root), str);
}
//先中心,再左树,再右树
void show_node_clr(BinTreeNode* n){
if(NULL == n)return;
else{
printf("%c ", n->data);
show_node_clr(n->leftChild);
show_node_clr(n->rightChild);
}
}
//先中心,再左树,再右树
void show_clr(BinTree* tr){
show_node_clr(tr->root);
} //先左树,再中心,再右树
void show_node_lcr(BinTreeNode* n){
if(NULL == n)return;
else{
show_node_lcr(n->leftChild);
printf("%c ", n->data);
show_node_lcr(n->rightChild);
}
}
//先左树,再中心,再右树
void show_lcr(BinTree* tr){
show_node_lcr(tr->root);
} //先左树,再右树,再中心
void show_node_lrc(BinTreeNode* n){
if(NULL == n)return;
else{
show_node_lrc(n->leftChild);
show_node_lrc(n->rightChild);
printf("%c ", n->data);
}
}
//先左树,再右树,再中心
void show_lrc(BinTree* tr){
show_node_lrc(tr->root);
} //非递归层级遍历
//利用队列,先进先出
void show_node_level(BinTreeNode* n){
if(NULL == n) return;
NodeQueue queue;
init_queue(&queue);
enQueue(&queue, n); BinTreeNode* tmp;
while(!isQueueEmpty(&queue)){
if(getHead(&queue) == NULL)break;
tmp = getHead(&queue)->data;
deQueue(&queue);
printf("%c ", tmp->data);
if(tmp->leftChild != NULL)
enQueue(&queue, tmp->leftChild);
if(tmp->rightChild != NULL)
enQueue(&queue, tmp->rightChild);
}
printf("\n");
} //层级遍历
void show_level(BinTree* tr){
show_node_level(tr->root);
} //--------------------分界线-------------------- //取得树中节点个数
void get_node_size1(BinTreeNode* n, int* p){
if (NULL == n)return;
else{
*p = *p + 1;
get_node_size1(n->leftChild, p);
get_node_size1(n->rightChild, p);
}
}
//取得树中节点个数
int get_size1(BinTree* tr){
int size = 0;
get_node_size1(tr->root, &size);
return size;
}
int get_node_size2(BinTreeNode* p){
if(NULL == p) return 0;
else{
return get_node_size2(p->leftChild) + get_node_size2(p->rightChild) + 1;
}
}
int get_size2(BinTree* tr){
return get_node_size2(tr->root);
}
//分别求左右树的高度,哪个高,哪就是树的高度
void get_node_height(BinTreeNode* n, int* p){
if(NULL == n) return;
*p = *p + 1; int t1 = 0, t2 = 0; get_node_height(n->leftChild, &t1);
get_node_height(n->rightChild,&t2);
if(t1 > t2)
*p = *p + t1;
else
*p = *p + t2;
}
int get_height(BinTree* tr){
int size = 0;
if(tr->root != NULL) size++;
int t1 = 0, t2 = 0;
get_node_height(tr->root->leftChild, &t1);
get_node_height(tr->root->rightChild, &t2);
return t1 > t2 ? t1 + size : t2 + size;
}
//递归查找节点,如果在左节点找到了,就不需要查找右节点了
BinTreeNode* search_node(BinTreeNode* n, ElemType key){
if (NULL == n || n->data == key){
return n;
}else{
BinTreeNode* tmp;
tmp = search_node(n->leftChild, key);
if(NULL == tmp){
tmp = search_node(n->rightChild, key);
}
return tmp;
}
}
BinTreeNode* search(BinTree* tr, ElemType key){
BinTreeNode* n = NULL;
n = search_node(tr->root, key);
return n;
}
//递归查找父节点,如果父节点的左节点已经为目标节点,就不需要再判断这个父节点的右节点了。
BinTreeNode* get_node_parent(BinTreeNode* n, BinTreeNode* target){
if(NULL == n || NULL == target) return NULL;
if(n->leftChild == target || n->rightChild == target){
return n;
}
else{
BinTreeNode* tmp = NULL;
tmp = get_node_parent(n->leftChild, target);
if(NULL == tmp){
tmp = get_node_parent(n->rightChild, target);
}
return tmp;
}
}
BinTreeNode* get_parent(BinTree* tr, BinTreeNode* target){
get_node_parent(tr->root, target);
}
bool isBintreeEmpty(BinTree* tr){
return NULL == tr->root;
}
void copy_node(BinTreeNode** n1, BinTreeNode* n2){
if(NULL == n2){
*n1 = NULL;
return;
}else{
BinTreeNode* p = (BinTreeNode*)malloc(sizeof(BinTreeNode*));
p->data = n2->data;
*n1 = p;
copy_node(&((*n1)->leftChild), n2->leftChild);
copy_node(&((*n1)->rightChild), n2->rightChild);
} }
void copy(BinTree* tr1, BinTree* tr2){
copy_node(&(tr1->root), tr2->root);
}
void bintree_node_clear(BinTreeNode** n){
if(NULL == *n)return;
bintree_node_clear(&((*n)->leftChild));
bintree_node_clear(&((*n)->rightChild));
free(*n);
*n = NULL;
}
void bintree_clear(BinTree* tr){
bintree_node_clear(&(tr->root));
// init(tr, '#');
} //非递归 先中心,再左树,再右树
//利用栈,先进后出
void display_node_clr(BinTreeNode* n){
if(NULL == n){
printf("是空树\n");
return;
}
nodestack stack;
init(&stack);
push(&stack, n);
Node* tmp = NULL;
while(0 != stack.size){
tmp = pop(&stack);
if(NULL == tmp) continue; printf("%c ", tmp->data->data); if(tmp->data->rightChild != NULL)
push(&stack, tmp->data->rightChild);
if(tmp->data->leftChild != NULL)
push(&stack, tmp->data->leftChild);
}
}
//非递归 先中心,再左树,再右树
void display_clr(BinTree* tr){
display_node_clr(tr->root);
}
//非递归 先左树,再中心,再右树
//利用栈,先进后出
void display_node_lcr(BinTreeNode* n){
if(NULL == n){
printf("是空树\n");
return;
}
nodestack stack;
init(&stack);
push(&stack, n);
Node* tmp = NULL;
while(0 != stack.size){
tmp = pop(&stack);
if(NULL != tmp->data->leftChild){
if(NULL != tmp->data->rightChild){
push(&stack, tmp->data->rightChild);
}
//中心节点
push(&stack, tmp->data);
push(&stack, tmp->data->leftChild);
}
else{
//再把中心节点pop出来
if(NULL == tmp->data->rightChild){
printf("%c ", tmp->data->data);
//pop出来的tmp为中心节点
tmp = pop(&stack);
if(NULL == tmp)continue;
printf("%c ", tmp->data->data);
}
else{
printf("%c ", tmp->data->data);
push(&stack, tmp->data->rightChild);
}
}
}
}
//非递归 先左树,再中心,再右树
void display_lcr(BinTree* tr){
display_node_lcr(tr->root);
}
//非递归 先左树,再右树,再中心
//利用栈,先进后出
//关键点,把有子树的父节点的tag设置成R。当pop出的节点有子节点,并且此节点的tag为R的话,就说明此节点的子节点已经入栈了,所以直接打印出来。
void display_node_lrc(BinTreeNode* n){ if(NULL == n){
printf("是空树\n");
return;
}
nodestack stack;
init(&stack);
push(&stack, n);
Node* tmp = NULL;
while(0 != stack.size){
tmp = pop(&stack); if(tmp->tag == R){
printf("%c ", tmp->data->data);
continue;
} if(NULL != tmp->data->leftChild){ //中心节点
push(&stack, tmp->data);
//把中心节点的属性设置成R
stack.top->tag = R;
if(NULL != tmp->data->rightChild){
push(&stack, tmp->data->rightChild);
}
push(&stack, tmp->data->leftChild);
}
else{
if(NULL == tmp->data->rightChild){
printf("%c ", tmp->data->data);
}
else{
//中心节点
push(&stack, tmp->data);
//把中心节点的属性设置成R
stack.top->tag = R;
push(&stack, tmp->data->rightChild);
}
}
}
}
//非递归 先左树,再右树,再中心
void display_lrc(BinTree* tr){
display_node_lrc(tr->root);
}

bintreemain.c

#include "bintree.h"

int main(){
BinTree tr;
init(&tr, '#'); //ABC##DE##F##G##H##
//createBinTree(&tr); char* a = "ABC##DE##F##G#H##";
//char* a = "AB##C##";
BinTree tr1;
init(&tr1, '#');
createBinTree_str(&tr1, &a);
show_clr(&tr1);
printf("\n");
show_lcr(&tr1);
printf("\n");
show_lrc(&tr1);
printf("\n"); show_level(&tr1); //非递归遍历,中 左 右
display_clr(&tr1);
printf("\n");
//非递归遍历,左 中 右
display_lcr(&tr1);
printf("\n");
//非递归遍历,左 右 中
display_lrc(&tr1);
printf("\n"); return 0;
}

nodequeue.h

#ifndef __NODEQUEUE__
#define __NODEQUEUE__ #include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include <memory.h>
#include <stdbool.h> struct BinTreeNode; #define ElemType1 BinTreeNode* typedef struct Node{
ElemType1 data;
struct Node* next;
}Node; typedef struct NodeQueue{
Node* front;
Node* tail;
size_t size;
}NodeQueue; void init_queue(NodeQueue*);
void enQueue(NodeQueue*, ElemType1);
void deQueue(NodeQueue*);
void show_list(NodeQueue*);
int length(NodeQueue*);
void clear(NodeQueue*);
void destroy(NodeQueue*);
Node* getHead(NodeQueue*);
bool isQueueEmpty(NodeQueue*); #endif

nodequeue.c

#include "nodequeue.h"

void init_queue(NodeQueue* queue){
queue->front = queue->tail = (Node*)malloc(sizeof(Node));
queue->tail->next = NULL;
queue->size = 0;
}
//入队(尾插)
void enQueue(NodeQueue* queue, ElemType1 val){
Node* p = (Node*)malloc(sizeof(Node));
p->data = val;
if(queue->front->next == NULL){
queue->front->next = p;
}
else{
queue->tail->next = p;
}
queue->tail = p;
p->next = NULL;
queue->size++;
}
//出队(头删)
void deQueue(NodeQueue* queue){
if(queue->size == 0)return;
Node* tmp = queue->front->next;
queue->front->next = queue->front->next->next;
free(tmp);
queue->size--;
}
nt length(NodeQueue* queue){
return queue->size;
}
void show_list(NodeQueue* queue){
Node* p = queue->front;
while(p->next != NULL){
printf("%d\n", p->next->data);
p = p->next;
}
}
void clear(NodeQueue* queue){
if(queue->size == 0)return;
Node* p = queue->front;
Node* tmp;
while(p->next != NULL){
tmp = p->next;
p = p->next;
free(tmp);
}
queue->tail = queue->front;
queue->tail->next = NULL;
queue->size = 0;
}
void destroy(NodeQueue* queue){
clear(queue);
free(queue->front);
}
Node* getHead(NodeQueue* queue){
return queue->front->next;
} bool isQueueEmpty(NodeQueue* queue){
return queue->front == queue->tail;
}

nodestack.h

#ifndef __NODESTACK__
#define __NODESTACK__ #include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include <memory.h>
#include <stdbool.h> struct BinTreeNode; typedef BinTreeNode* ElemType2;
typedef enum{L, R}Tag; typedef struct Node{
ElemType2 data;
Tag tag;
struct Node *next;
}Node; typedef struct nodestack{
int size;
Node *base;
Node *top;
}nodestack; void init(nodestack*);
void push(nodestack*, ElemType2);
void show_list(nodestack*);
Node* pop(nodestack*);
int length(nodestack*);
void clear(nodestack*);
void destroy(nodestack*);
#endif

nodestack.c

#include "nodestack.h"

Node* createNode(ElemType2 val){
Node* n = (Node*)malloc(sizeof(Node));
n->data = val;
n->tag = L;
n->next = NULL;
return n;
}
void init(nodestack* stack){
stack->size = 0;
stack->top = NULL;
stack->base = NULL;
}
void push(nodestack* stack, ElemType2 x){
Node* n = createNode(x);
//当栈为空的时候
if(stack->base == NULL){
stack->top = stack->base = n;
n->next = NULL;
stack->size++;
return;
}
n->next = stack->top;
stack->top = n;
stack->size++;
}
void show_list(nodestack* stack){
Node* top = stack->top;
while(top != NULL){
printf("%d\n", top->data);
top = top->next;
}
}
Node* pop(nodestack* stack){
if(stack->size == 0)return NULL; Node* n = stack->top;
stack->top = stack->top->next;
stack->size--;
return n;
}
int length(nodestack* stack){
return stack->size;
}
void clear(nodestack* stack){
Node* top = stack->top;
Node* tmp;
while(top != NULL){
tmp = top;
top = top->next;
free(tmp);
}
stack->top = stack->base = NULL;
stack->size = 0;
}
void destroy(nodestack* stack){
clear(stack);
}

c/c++叉树的创建与遍历(非递归遍历左右中,不破坏树结构)的更多相关文章

  1. c/c++二叉树的创建与遍历(非递归遍历左右中,破坏树结构)

    二叉树的创建与遍历(非递归遍历左右中,破坏树结构) 创建 二叉树的递归3种遍历方式: 1,先中心,再左树,再右树 2,先左树,再中心,再右树 3,先左树,再右树,再中心 二叉树的非递归4种遍历方式: ...

  2. Java实现二叉树的创建、递归/非递归遍历

    近期复习数据结构中的二叉树的相关问题,在这里整理一下 这里包含: 1.二叉树的先序创建 2.二叉树的递归先序遍历 3.二叉树的非递归先序遍历 4.二叉树的递归中序遍历 5.二叉树的非递归中序遍历 6. ...

  3. 145.Binary Tree Postorder Traversal---二叉树后序非递归遍历

    题目链接 题目大意:后序遍历二叉树. 法一:普通递归,只是这里需要传入一个list来存储遍历结果.代码如下(耗时1ms): public List<Integer> postorderTr ...

  4. 数据结构二叉树的递归与非递归遍历之java,javascript,php实现可编译(1)java

    前一段时间,学习数据结构的各种算法,概念不难理解,只是被C++的指针给弄的犯糊涂,于是用java,web,javascript,分别去实现数据结构的各种算法. 二叉树的遍历,本分享只是以二叉树中的先序 ...

  5. C++编程练习(17)----“二叉树非递归遍历的实现“

    二叉树的非递归遍历 最近看书上说道要掌握二叉树遍历的6种编写方式,之前只用递归方式编写过,这次就用非递归方式编写试一试. C++编程练习(8)----“二叉树的建立以及二叉树的三种遍历方式“(前序遍历 ...

  6. 非递归遍历N-ary树Java实现

    2019-03-25 14:10:51 非递归遍历二叉树的Java版本实现之前已经进行了总结,这次做的是非递归遍历多叉树的Java版本实现. 在非递归遍历二叉树的问题中我个人比较推荐的是使用双whil ...

  7. ZT 二叉树的非递归遍历

    ZT 二叉树的非递归遍历 二叉树的非递归遍历 二叉树是一种非常重要的数据结构,很多其它数据结构都是基于二叉树的基础演变而来的.对于二叉树,有前序.中序以及后序三种遍历方法.因为树的定义本身就 是递归定 ...

  8. c语言描述的二叉树的基本操作(层序遍历,递归,非递归遍历)

    #include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define ...

  9. WPF树形菜单--递归与非递归遍历生成树结构的集合

    一.新建了WPF项目作为测试,使用TreeView控件进行界面展示. 第一步创建实体类TreeEntity: public class TreeEntity { private int _mid; p ...

随机推荐

  1. 创作型---原型模式(C# ICloneable接口的实现)

    在软件系统中,当创建一个类的实例的过程很昂贵或很复杂,并且我们需要创建多个这样类的实例时,可以通过对原来对象拷贝一份来完成创建,这样在内存中不需要创建多个相同的类实例,从而减少内存的消耗和达到类实例的 ...

  2. Chrome插件开发,美化网页上的文件列表。chrome-extension,content-scripts

    趁着2018年还剩最后几天,发几篇博客,荒废太久了,惭愧. 最近也是需求驱动,研究了下Chrome插件开发.来看一下我们公司运维提供的日志查看页面 所有项目的日志都参杂在一起,每次去找都很痛苦.慢慢发 ...

  3. c# 创建,加载,修改XML文档

    using System.Xml.Linq; static void Main(string[] args) { XDocument xDocument = new XDocument(new XEl ...

  4. 自动化运维经验谈,以及为什么Docker是革命性的

    互联网+的需要 在信息越来越繁杂的互联网时代,公司所运行的项目越来越多,项目相关服务繁多,服务之间存在复杂的依赖关系,运维与管理任务越来越繁重,手工交付需要花费很多的人力与时间,且安全性和时效性均无法 ...

  5. 远程连接centos7 上的mysql报(ERROR 2003 (HY000): Can't connect to MySQL server on '168.x.x.x' (10060) )

    1.MySQL端口 因为上一篇文章我就已经给MySQL新建了一新用户,且赋予了远程连接数据库的所有权限(GRANT ALL PRIVILEGES ON *.* TO 'newuser' @ '%' I ...

  6. 微信小程序之封装http请求

    下面将封装http请求服务部分的服务以及引用部分 // 本服务用于封装请求 // 返回的是一个promisepromise var sendRrquest = function (url, metho ...

  7. dpr,ppi,dip,viewport的一些概念

    一 ppi,dpr,dip,分辨率,屏幕尺寸,设备物理像素,设备独立像素 分辨率:1920px*1080px 在这个设备上纵向上有 1920个像素点(色块)... 屏幕尺寸:5英寸(in) = 5*2 ...

  8. Android tabLayout+recyclerView实现锚点定位

    原文链接:https://mp.weixin.qq.com/s/L3o2i3WTmg1ScXEYDS8YCg 在上一篇文章 Android 实现锚点定位中,我们介绍了tablayout+scrollV ...

  9. Android在程序崩溃或者捕获异常之后重新启动app

    在Android应用开发中,偶尔会因为测试的不充分导致一些异常没有被捕获,这时应用会出现异常并强制关闭,这样会导致很不好的用户体验,为了解决这个问题,我们需要捕获相关的异常并做处理. 首先捕获程序崩溃 ...

  10. Java中的变量与常量

    Java中的常量 final 常量名=值; final PI=3.1415;  //声明一个常量PI 定义常量:final double PI=3.1415926; Java三大变量分别是  类变量( ...