二叉树的遍历
来源:互联网 发布:卡盟域名是什么 编辑:程序博客网 时间:2024/06/15 09:27
先序遍历:1.访问根结点 2.先序遍历根结点的左子树 3.先序遍历根结点的右子树
中序遍历:1.中序遍历根结点的左子树 2.访问根结点 3.中序遍历根结点的右子树
后序遍历: 1.后序遍历根结点的左子树 2.后序遍历根结点的右子树 3.访问根结点
二叉树的顺序存储结构:
二叉树的顺序存储结构是用一组连续的存储单元存放二叉树的数据元素。顺序存储二叉树时,首先对二叉树中的结点按照满二叉树的形式进行编号,不存在的结点也要编号,之后将编号为i的结点存储在数据的第i-1个分量中。但是一般的二叉树也必须按完全二叉树的形式存储,但是对于那些单支较多的二叉树来说不合适,因为将造成太多空间的浪费。但是比较适合满二叉树和完全二叉树。
二叉树的链式存储结构:
二叉链表存储:如果每个结点结构由一个数据域和俩个分别指向其左、右子树的指针组成。data域存放结点的数据信息,lchild与rchild存放指向左孩子和右孩子的指针
三叉链表存储:三叉链表是在二叉链表的基础上加一个指向双亲结点的指域parent,既便于查找每个结点的孩子结点,又便于查找其双亲结点。但是增加了存储空间的开销。 尽管二叉链表中无法由结点直接找到其双亲,但由于二叉链表结构灵活,操作方便,对于一般情况的二叉树,甚至比顺序存储结构还节省空间。因此,二叉链表是常用的二叉树存储方式。
二叉树的二叉链表实现遍历:
二叉树类
#include<iostream>#include"DoubleNode.h" //双链表结点类#include"SeqStack.h" //顺序栈#include"LinkedStack.h" //链式栈#include"SeqQueue.h" //顺序循环队列using namespace std;template <class T>class BinaryTree // 二叉树类{public:DoubleNode<T> *root; //指向根结点BinaryTree(); //构造空二叉树BinaryTree(T prelist [],int n); //标明空子树的先根序列构造一棵二叉树BinaryTree(T prelist [],T inlist [],int n); //先根和中根序列构造二叉树~BinaryTree();bool IsEmpty(); //判断是否是空二叉树int Count(); //返回结点个数int Height(); //返回二叉树的高度DoubleNode<T> *Search(T value); //查找首次出现值为value的结点DoubleNode<T> *GetParent(DoubleNode<T> *node); //返回node结点的双亲结点void preRoot(); //先根次序遍历二叉树void midRoot(); //中根次序遍历二叉树void postRoot(); //后根次序遍历二叉树DoubleNode<T> *Insert(DoubleNode<T> *p, T value,bool priorChild = true);//插入value作为P结点的孩子void Remove(DoubleNode<T> * p,bool priorChild = true); //删除p结点的左或右子树void PrintGList(); //以广义表输出二叉树void preRootTraverse(); // 先根次序遍历二叉树的非递归算法void midRootTraverse(); // 中根次序遍历二叉树的非递归算法void LevelOrder(); //按层次遍历二叉树private:void Destroy (DoubleNode<T> *p); //撤销二叉树int Count(DoubleNode<T> *p); //返回以P结点为根的子树结点个数int Height(DoubleNode<T> *p); //返回以P结点为根的子树结点的高度DoubleNode<T>*Search (DoubleNode<T> *p, T value); //在以p为根的子树中查找首次出现的值为value的结点DoublenNode<T> *GetParent (DoubleNode<T> *p, DoubleNode<T> *node); // p为根的子树中查找并返回首次出现的值为value的结点void preRoot(DoubleNode<T> *p); //先跟次序遍历以p结点为根的子树void midtRoot(DoubleNode<T> *p); //中根次序遍历以p结点为根的子树void postRoot(DoubleNode<T> *p); //后根次序遍历以P结点为根的子树DoubleNode<T> *creat(T prelist[],int n,int &i); //以标明空子树的先根遍历序列创建子树DoubleNode<T> *creat(T prelist[],T inlist[],int preStart, int inStart,int n); //以先根和中根序列创建一棵子树void PrintGList (DoubleNode<T> *p); //以广义表表示输出以p结点为根的子树};template <class T>BinaryTree<T>::BinaryTree() //构造空二叉树{ this->root =NULL;}template<class T>BinaryTree<T>::~BinaryTree(){ cout<<"撤销二叉树:"; Destroy(this->root); cout<<endl;}template<class T>void BinaryTree<T>::Destroy(DoubleNode<T> *p){ if(p!=NULL) { Destroy (p->prior); Destroy (p->next); cout<<p->data<<" "; // 显示撤销结点的次序 delete p; }}template<class T>bool BinaryTree<T>::IsEmpty(){ return this->root==NULL;}
统计二叉树结点数
template <class T>int BinaryTree<T>::Count(){ return Count(this->root);}template<class T>int BinaryTree<T>::Count(DoubleNode<T> *p){ if(p==NULL) return 0; else return 1+Count(p->prior)+Count(p->next);}
二叉树的遍历
#include<iostream>#include"DoubleNode.h" //双链表结点类#include"SeqStack.h" //顺序栈#include"LinkedStack.h" //链式栈#include"SeqQueue.h" //顺序循环队列using namespace std;template <class T>class BinaryTree // 二叉树类{public:DoubleNode<T> *root; //指向根结点BinaryTree(); //构造空二叉树BinaryTree(T prelist [],int n); //标明空子树的先根序列构造一棵二叉树BinaryTree(T prelist [],T inlist [],int n); //先根和中根序列构造二叉树~BinaryTree();bool IsEmpty(); //判断是否是空二叉树int Count(); //返回结点个数int Height(); //返回二叉树的高度DoubleNode<T> *Search(T value); //查找首次出现值为value的结点DoubleNode<T> *GetParent(DoubleNode<T> *node); //返回node结点的双亲结点void preRoot(); //先根次序遍历二叉树void midRoot(); //中根次序遍历二叉树void postRoot(); //后根次序遍历二叉树DoubleNode<T> *Insert(DoubleNode<T> *p, T value,bool priorChild = true);//插入value作为P结点的孩子void Remove(DoubleNode<T> * p,bool priorChild = true); //删除p结点的左或右子树void PrintGList(); //以广义表输出二叉树void preRootTraverse(); // 先根次序遍历二叉树的非递归算法void midRootTraverse(); // 中根次序遍历二叉树的非递归算法void LevelOrder(); //按层次遍历二叉树private:void Destroy (DoubleNode<T> *p); //撤销二叉树int Count(DoubleNode<T> *p); //返回以P结点为根的子树结点个数int Height(DoubleNode<T> *p); //返回以P结点为根的子树结点的高度DoubleNode<T>*Search (DoubleNode<T> *p, T value); //在以p为根的子树中查找首次出现的值为value的结点DoublenNode<T> *GetParent (DoubleNode<T> *p, DoubleNode<T> *node); // p为根的子树中查找并返回首次出现的值为value的结点void preRoot(DoubleNode<T> *p); //先跟次序遍历以p结点为根的子树void midtRoot(DoubleNode<T> *p); //中根次序遍历以p结点为根的子树void postRoot(DoubleNode<T> *p); //后根次序遍历以P结点为根的子树DoubleNode<T> *creat(T prelist[],int n,int &i); //以标明空子树的先根遍历序列创建子树DoubleNode<T> *creat(T prelist[],T inlist[],int preStart, int inStart,int n); //以先根和中根序列创建一棵子树void PrintGList (DoubleNode<T> *p); //以广义表表示输出以p结点为根的子树};template <class T>BinaryTree<T>::BinaryTree() //构造空二叉树{ this->root =NULL;}template<class T>BinaryTree<T>::~BinaryTree(){ cout<<"撤销二叉树:"; Destroy(this->root); cout<<endl;}template<class T>void BinaryTree<T>::Destroy(DoubleNode<T> *p){ if(p!=NULL) { Destroy (p->prior); Destroy (p->next); cout<<p->data<<" "; // 显示撤销结点的次序 delete p; }}template<class T>bool BinaryTree<T>::IsEmpty(){ return this->root==NULL;}template <class T>int BinaryTree<T>::Count(){ return Count(this->root);}template<class T>int BinaryTree<T>::Count(DoubleNode<T> *p){ if(p==NULL) return 0; else return 1+Count(p->prior)+Count(p->next);}template <class T>void BinaryTree<T>::preRoot(){ cout<<"先根次序遍历二叉树"; preRoot(this->root); cout<<endl;}template<class T>void BinaryTree<T>::preRoot(DoubleNode<T> *p){ if(p!=NULL) { cout<<p->data<<" "; preRoot(p->prior); preRoot(p->next); }}template <class T>void BinaryTree<T>::midRoot(){ cout<<"中根次序遍历二叉树"; midRoot(this->root); cout<<endl;}template<class T>void BinaryTree<T>::midtRoot(DoubleNode<T> *p){ if(p!=NULL) { midRoot(p->prior); cout<<p->data<<" "; midRoot(p->next); }}template<class T>void BinaryTree<T>::postRoot(){ cout<<"后根次序遍历二叉树"; postRoot(this->root); cout<<endl;}template<class T>void BinaryTree<T>::postRoot(DoubleNode<T> *p){ if(p!=NULL) { postRoot(p->prior); postRoot(p->next); cout<<p->data<<" "; }}#define _CRT_SECURE_NO_WARNINGS#include<iostream>#include"BinaryTree.h"using namespace std;void Creat(BinaryTree<char> &bitree){ DoubleNode<char> *child_d = new DoubleNode<char>('D',NULL,new DoubleNode<char>('H')); DoubleNode<char> *child_e = new DoubleNode<char>('E',NULL,new DoubleNode<char>('I')); DoubleNode<char> *child_b = new DoubleNode<char>('B',child_d,child_e); DoubleNode<char> *child_f = new DoubleNode<char>('F',new DoubleNode<char>('J'),NULL); DoubleNode<char> *child_g = new DoubleNode<char>('G',new DoubleNode<char>('K'),NULL); DoubleNode<char> *child_c = new DoubleNode<char>('C',child_f,child_g); bitree.root = new DoubleNode<char>('A',child_b,child_c);}int main(){ BinaryTree<char> bitree; Creat(bitree); bitree.preRoot(); bitree.midRoot(); bitree.postRoot(); return 0;}
上一封 下一封
« 返回
阅读全文
0 0
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 字蛛的使用
- 泛函编程(17)-泛函状态-State In Action
- 【思路题】BZOJ4721 [NOIP2016]蚯蚓
- 字符串的插入
- JSP框架Tiles,好东西。
- 二叉树的遍历
- 泛函编程(16)-泛函状态-Functional State
- AJAX 状态值(readyState)与状态码(status)详解
- 机房重构——数据库设计
- 泛函编程(15)-泛函状态-随意数产生器
- 01背包1001
- Pearls in a Row CodeForces
- poj 1195 Mobile phones
- 泛函编程(14)-try to map them all