二叉树的递归遍历
来源:互联网 发布:淘宝装修教程 编辑:程序博客网 时间:2024/06/05 15:18
#include<iostream>
#include<cassert>
#include<cstdlib>
using namespace std;
template<class T>
struct TreeNode
{
T _data;
TreeNode<T> *left;
TreeNode<T> *right;
};
template<class T>
class Tree
{
typedef TreeNode<T> Node;
public:
Tree()
:_root(NULL)
{}
Tree(T *a,size_t size, const T& invalid)
{
size_t index=0;
_root=CreatNode(a,size,index,invalid);
}
Tree<T>& operator=(const Tree<T>& t)
{
if(this!=&t)
std::swap(_root,t._root );
return *this;
}
void _PrevOrder(Node* root)
{
cout<<root->_data <<' ';
_PrevOrder(root->left);
_PrevOrder(root->right);
}
void _InOrder(Node* root)
{
assert(root);
_InOrder(root->left);
cout<<root->_data<<' ';
_InOrder(root->right);
}
void _PostOrder(Node* root)
{
assert(root);
_PostOrder(root->left);
_PostOrder(root->right);
cout<<root->_data<<' ';
}
void PostOrder()
{
_PostOrder(_root);
cout<<endl;
}
void PrevOrder()
{
_PrevOrder(_root);
cout<<endl;
}
void InOrder()
{
_InOrder(_root);
}
protected:
Node* CreatNode(T* a,size_t size,size_t& index,const T& invalid)
{
assert(a);
Node* root=new Node(NULL);
if((index<=size)&&a[index]!=invalid)
{
root->_data=a[index];
root->left=CreatNode(a,size,++index,invalid);
root->right=CreatNode(a,size,++index,invalid);
#include<cassert>
#include<cstdlib>
using namespace std;
template<class T>
struct TreeNode
{
T _data;
TreeNode<T> *left;
TreeNode<T> *right;
};
template<class T>
class Tree
{
typedef TreeNode<T> Node;
public:
Tree()
:_root(NULL)
{}
Tree(T *a,size_t size, const T& invalid)
{
size_t index=0;
_root=CreatNode(a,size,index,invalid);
}
Tree<T>& operator=(const Tree<T>& t)
{
if(this!=&t)
std::swap(_root,t._root );
return *this;
}
void _PrevOrder(Node* root)
{
cout<<root->_data <<' ';
_PrevOrder(root->left);
_PrevOrder(root->right);
}
void _InOrder(Node* root)
{
assert(root);
_InOrder(root->left);
cout<<root->_data<<' ';
_InOrder(root->right);
}
void _PostOrder(Node* root)
{
assert(root);
_PostOrder(root->left);
_PostOrder(root->right);
cout<<root->_data<<' ';
}
void PostOrder()
{
_PostOrder(_root);
cout<<endl;
}
void PrevOrder()
{
_PrevOrder(_root);
cout<<endl;
}
void InOrder()
{
_InOrder(_root);
}
protected:
Node* CreatNode(T* a,size_t size,size_t& index,const T& invalid)
{
assert(a);
Node* root=new Node(NULL);
if((index<=size)&&a[index]!=invalid)
{
root->_data=a[index];
root->left=CreatNode(a,size,++index,invalid);
root->right=CreatNode(a,size,++index,invalid);
}
return root;
}
private:
Node* _root;
};
void test()
{
int a1[10]={1,2,3,'#','#',4,'#','#',5,6};
Tree<int> t1 (a1,10,'#');
t1.InOrder();
return root;
}
private:
Node* _root;
};
void test()
{
int a1[10]={1,2,3,'#','#',4,'#','#',5,6};
Tree<int> t1 (a1,10,'#');
t1.InOrder();
}
int main()
{
test();
system("pause");
return 0;
int main()
{
test();
system("pause");
return 0;
}
0 0
- 二叉树的递归遍历
- 二叉树的递归遍历
- 二叉树的递归遍历
- 二叉树的递归遍历
- 二叉树的递归遍历
- 二叉树的递归遍历
- 二叉树的递归遍历
- 二叉树的递归遍历
- 二叉树的递归遍历
- 二叉树的递归遍历
- 二叉树的递归遍历
- 二叉树的递归遍历
- 二叉树的递归遍历
- 二叉树的递归遍历
- 二叉树的遍历(递归)
- 二叉树的先中后序遍历,递归遍历,非递归遍历
- 二叉树的递归,非递归遍历
- 二叉树的递归+非递归遍历
- 用SurfaceView实现级联分层图(粗略篇)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- 【新手建站三部曲之一】——一块钱搭建自己的服务器(LAMP)
- 数据结构之大小堆&&优先级队列
- IOS UICollectionView使用
- 二叉树的递归遍历
- Android 通过ViewPager实现点击和滑动切换Fragment标签页
- 开发者的Facebook | GitHub投资人寻求退出?Microsoft意图收购?
- Git部分命令介绍
- AndroidNDK使用简介
- C/C++ 编译器的命名粉碎规则(name mangling)和C/C++混合(ZZ)
- Android应用中如何调用系统设置项
- MySQL锁表解决方法(转)
- Map的遍历方法