二叉树 遍历+各种模板
来源:互联网 发布:淘宝螺旋是什么意思 编辑:程序博客网 时间:2024/06/08 16:48
#include <iostream>#include<queue>#include<cstring>#include<stdio.h>#include<stdlib.h>#include<stack>#define maxn 100010using namespace std;typedef char datatype;typedef struct BinNode{ datatype data; struct BinNode* lchild; struct BinNode* rchild;} BinNode;typedef BinNode* bintree;void createtree(bintree &t)///先序遍历{ datatype c; if((c=getchar()) == '#') t = NULL; else { t = (bintree)malloc(sizeof(BinNode)); t->data = c; createtree(t->lchild); createtree(t->rchild); }}/* 先序遍历(非递归) 思路:访问T->data后,将T入栈,遍历左子树;遍历完左子树返回时,栈顶元素应为T,出栈,再先序遍历T的右子树。*/void PreOrder2(bintree T){ stack<bintree> s;//p是遍历指针 bintree p = T; while(p || !s.empty()) { if(p != NULL) { s.push(p); printf("%c ",p->data); ///访问根节点 p = p->lchild; } else { p = s.top(); s.pop(); p = p->rchild; } }}void InOrder2(bintree T){ stack<bintree> s; bintree p = T; while(p || !s.empty()) { if(p != NULL) { s.push(p); p = p->lchild; } else { p = s.top(); printf("%c ",p->data); s.pop(); p = p->rchild; } }}////void Visit(BiTree T)//{// if(T->data != '#')// {// printf("%c ",T->data);// }//}////void PreOrder(BiTree T)//{// if(T != NULL)// {// Visit(T);// PreOrder(T->rchild);// }//}////void InOrder(BiTree T)//{// if(T != NULL)// {// InOrder(T->lchild);// Visit(T);// InOrder(T->rchild);// }//}////void PostOrder(BiTree T)//{// if(T != NULL)// {// PostOrder(T->lchild);// PostOrder(T->rchild);// Visit(T);// }//}//typedef struct BiTNodePost//{// bintree bintree;// char tag;//} BiTNodePost,*bintreePost;////void PostOrder2(bintree T)//{// stack<bintreePost> s;// bintree p = T;// bintreePost BT;// while(p != NULL || !s.empty())// {// while(p != NULL)// {// BT = (bintreePost)malloc(sizeof(BiTNodePost));// BT->bintree = p;// BT->tag = 'L';///访问过左子树// s.push(BT);// p = p->lchild;// }// ///左右子树访问完毕访问根节点// while(!s.empty() && (s.top())->tag == 'R')// {// BT = s.top();// s.pop();// BT->bintree;// printf("%c ",BT->bintree->data);// }// if(!s.empty())// {// BT = s.top();// BT->tag = 'R';// p = BT->bintree;// p = p->rchild;// }// }//}bintree search_tree(bintree t,datatype x){ if(!t) { return NULL; } if(t->data == x) { return t; } else { if(!search_tree(t->lchild,x)) { return search_tree(t->rchild,x); } return t; }}int count_tree(bintree t){ if(t) { return (count_tree(t->lchild)+count_tree(t->rchild)+1); } return 0;}int is_equal(bintree t1,bintree t2){ if(!t1 && !t2) //都为空就相等 { return 1; } if(t1 && t2 && t1->data == t2->data) //有一个为空或数据不同就不判断了 { if(is_equal(t1->lchild,t2->lchild)) if(is_equal(t1->rchild,t2->rchild)) { return 1; } } return 0;}int hight_tree(bintree t){ int h,left,right; if(!t) { return 0; } left = hight_tree(t->lchild); right = hight_tree(t->rchild); h = (left>right?left:right)+1; return h;}int main(){ bintree T; createtree(T); cout<<"Preorder"<<endl; PreOrder2(T); cout<<endl; cout<<"Inorder"<<endl; InOrder2(T); cout<<endl; int count=count_tree(T); cout<<count<<endl; int hight=hight_tree(T); cout<<hight<<endl; bintree t; t=search_tree(T,'G'); return 0;}
阅读全文
0 0
- 二叉树 遍历+各种模板
- 二叉树各种遍历
- 二叉树各种遍历
- 二叉树各种遍历
- 二叉树遍历模板
- 二叉树的各种遍历
- 二叉树的各种遍历
- 二叉树及其各种遍历
- 二叉树的各种遍历
- 二叉树 各种遍历算法
- 二叉树的各种遍历
- 二叉树的各种遍历
- 二叉树的各种遍历
- 二叉树的遍历模板
- 【模板】二叉树的遍历
- 二叉树的各种遍历,二叉树改链表,二叉树复习
- 树,二叉树,森林各种遍历总结:
- 二叉树的各种遍历【hao】
- 【ASP.net】——服务端脚本,客户端脚本
- 2017/8/23
- linux防火墙暂时关闭,及防火墙永久关闭
- vue.js学习之组件(下篇)
- JAVA内部类知识整理
- 二叉树 遍历+各种模板
- 根据图片循环添加圆点
- 谷歌浏览器如何使用谷歌搜索引擎
- 剑指Offer—26—二叉搜索树与双向链表
- 线程Thread和Runnable的区别
- Android View工作原理详解及源码分析(1)
- Objective-C基本分析法 反编译
- C++后台推荐书籍
- 深度学习笔记(一)感知器