与二叉树有关的一些操作:先序,中序,后序,层次遍历,计算深度,叶结点数
来源:互联网 发布:云计算概念股龙头 编辑:程序博客网 时间:2024/04/27 22:14
//先序遍历
template <typename T>
void preorderOutput(tnode<T> *t, const string& separator = " ")
{
// the recursive scan terminates on a empty subtree
if (t != NULL)
{
cout << t->nodeValue << separator; // output the node
inorderOutput(t->left, separator); // descend left
inorderOutput(t->right, separator); // descend right
}
}
//中序遍历
template <typename T>
void inorderOutput(tnode<T> *t, const string& separator = " ")
{
// the recursive scan terminates on a empty subtree
if (t != NULL)
{
inorderOutput(t->left, separator); // descend left
cout << t->nodeValue << separator; // output the node
inorderOutput(t->right, separator); // descend right
}
}
//后序遍历
template <typename T>
void postorderOutput(tnode<T> *t, const string& separator = " ")
{
// the recursive scan terminates on a empty subtree
if (t != NULL)
{
postorderOutput(t->left, separator); // descend left
postorderOutput(t->right, separator); // descend right
cout << t->nodeValue << separator; // output the node
}
}
//层次遍历
template <typename T>
void levelorderOutput(tnode<T> *t, const string& separator = " ")
{
// store siblings of each node in a queue so that they are
// visited in order at the next level of the tree
queue<tnode<T> *> q;
tnode<T> *p;
// initialize the queue by inserting the root in the queue
q.push(t);
// continue the iterative process until the queue is empty
while(!q.empty())
{
// delete front node from queue and output the node value
p = q.front();
q.pop();
cout << p->nodeValue << separator;
// if a left child exists, insert it in the queue
if(p->left != NULL)
q.push(p->left);
// if a right child exists, insert next to its sibling
if(p->right != NULL)
q.push(p->right);
}
}
//计算叶结点数
// assume that count initialized to 0
template <typename T>
void countLeaf (tnode<T> *t, int& count)
{
if (t != NULL)
{
// check if t is a leaf node (no children).
// if so, increment count
if (t->left == NULL && t->right == NULL)
count++;
countLeaf(t->left, count); // descend left
countLeaf(t->right, count); // descend right
}
}
//计算树的深度,空树深度设为-1
// determine the depth of the tree using a postorder scan
template <typename T>
int depth (tnode<T> *t)
{
int depthLeft, depthRight, depthval;
if (t == NULL)
// depth of an empty tree is -1
depthval = -1;
else
{
// find the depth of the left subtree of t
depthLeft= depth(t->left);
// find the depth of the right subtree of t
depthRight= depth(t->right);
// depth of the tree with root t is 1 + maximum
// of the depths of the two subtrees
depthval = 1 +
(depthLeft > depthRight ? depthLeft : depthRight);
}
return depthval;
}
- 与二叉树有关的一些操作:先序,中序,后序,层次遍历,计算深度,叶结点数
- 二叉树 建立二叉树 二叉树先、中、后序遍历 计算叶结点数 、树的深度
- 二叉树的先序创建,先序,中序,后序的递归与非递归遍历,层次遍历,叶子结点数及树的深度
- 数据结构--二叉树的创建、先序遍历、中序遍历、后序遍历、深度、叶子结点数
- Java实现二叉树的先序、中序、后序、层次遍历,数的最大深度、最大宽度
- 二叉树的先序 中序 后序 层次遍历 求叶节点个数、深度、最大元、最小元
- 二叉树的深度、叶子数以及先、中、后、层次遍历
- 数据结构之 二叉树的构造与遍历(先序,中序,后序,层次)
- 二叉树的先序遍历,后序遍历,中序遍历,层次遍历
- 二叉树BiTree的先序遍历.中序遍历.后序遍历.结点数.叶子结点数和数的高度
- 先序,中序,后序,层次遍历二叉树
- 二叉树 的先序 中序、后序遍历、层次遍历以及树状打印等操作
- 先序,中序,后序,求叶子结点数,深度,拷贝,几种二叉树的常见递归使用方法
- 二叉树的各种操作 先序 中序 后续 层次 遍历 求树高度 节点深度 知先序中序求后续 二叉排序树
- 二叉树的先、中、后序遍历及层次遍历的迭代版算法
- [C/C++] 先序建立二叉树| 先序、中序、后序遍历二叉树| 求二叉树深度、节点数、叶节点数 算法实现
- 二叉树 非递归 先序遍历 中序遍历 后序遍历 层次遍历
- 二叉树-建树,层次遍历,先序遍历,中序遍历,后序遍历
- 在CMP实体BEAN中使用BLOB数据类型(转载)
- Xerces panic error 解决方法
- mysql,sqlserver,oracle三种数据库的大对象存取
- 缺少if(!Page.IsPostBack)引起的错误(原创)
- Java 事务讨论
- 与二叉树有关的一些操作:先序,中序,后序,层次遍历,计算深度,叶结点数
- 弹出窗口总结收集(续)
- 网络管理中的常用命令
- 转载:vi使用手册(zt)
- 5天学会使用NoahWeb表现层制作动态网站(第五天)
- VPN的基本配置
- Window XP 命令提示
- 不需要任何附加信息的伪凹凸光照计算方法。
- vc中有关备份数据库的问题