社区
数据结构与算法
帖子详情
高分悬赏二叉树的建树中序偏历
yangming_ok
2001-10-19 10:52:36
...全文
121
13
打赏
收藏
高分悬赏二叉树的建树中序偏历
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
13 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
jiji007
2001-10-21
打赏
举报
回复
漏了一个},不好意思
jiji007
2001-10-21
打赏
举报
回复
用模板的大哥是否copy《数据结构C++描述》太火爆,递归实现无难度,不用喧宾夺主
下面是非递归的:
void midord_tranvers(tree* t){
while(t) {
push(t);t=t->left_child;}
pop(t);
visit(t);
t=t->right_child;
}// for while
mathematica
2001-10-20
打赏
举报
回复
我在一个工程中用了另类叉树结构,正在写一篇论文,有兴趣可以看看嘛。
mathematica_5@263.net
7rainbow
2001-10-20
打赏
举报
回复
不错!
这是最基本的了~~
feng_zq
2001-10-20
打赏
举报
回复
void midorder(tnode *p)
{ if(p)
{midorder(p->left);
operate(tnode->data);
midorder(p->right);
}
}
hakuna
2001-10-20
打赏
举报
回复
同意楼上
hellnet
2001-10-20
打赏
举报
回复
用递归只要四行代码
huahao0672
2001-10-19
打赏
举报
回复
不会吧不就是中序建立吗,用不着这么夸张吧!!!
hakuna
2001-10-19
打赏
举报
回复
/////////////////////////////////////////////////////////////////
// 中序遍历:
template <class Type> void BinaryTree <Type>::InOrder()
// Public function calling a private
{
InOrder(root);
}
template <class Type> void BinaryTree <Type>::
InOrder(BinTreeNode <Type>* current)
// Private function
{
if (current!=NULL)
{
InOrder (current->leftChild);
cout <<current->data;
InOrder (current->rightChild);
}
}
////////////////////////////////////////////////////////////////////
// 二叉树的类声明及部分函数实现代码
template <class Type> class Binary Tree;
template <class Type> class BinTreeNode
{
friend class BinaryTree <Type>;
public:
BinTreeNode(): leftChild (NULL), rightChild (NULL) {}
BinTreeNode( Type item, BinTreeNode <Type> * left = NULL, BinTreeNode <Type> * right = NULL ): data (item),
leftChild (left), rightChild (right) {}
Type GetData () const { return leftChild; }
BinTreeNode <Type> * GetLeft () const { return leftChild; }
BinTreeNode <Type> * GetRight () const { return rightChild; }
void SetData (const Type & item) { data = item; }
void SetLeft ( BinTreeNode <Type> * L ) { leftChild = L; }
void SetRight ( BinTreeNode <Type> * R ) { rightChild = R; }
private:
BinTreeNode <Type> * leftChild, * rightChild;
Type data;
};
template <class Type> class BinaryTree
{
public:
BinaryTree (): root (NULL) {}
BinaryTree (Type value): ReefValue (value), root (NULL) {}
virtual ~ BinaryTree () { destroy (root); }
virtual int IsEmpty () { return root == NULL ? 1 : 0; }
virtual BinTreeNode <Type> * Parent (BinTreeNode <Type> * current)
{ return root == NULL || root == current ? NULL : Parent (root, current); }
virtual BinTreeNode <Type> * LeftChild (BinTreeNode <Type> * current)
{ return root != NULL ? current->leftChild : NULL; }
virtual BinTreeNode <Type> * RightChild (BinTreeNode <Type> * current)
{ return root != NULL ? current->rightChild : NULL; }
virtual int Insert (const Type & item);
virtual int Find (const Type & item) const;
const BinTreeNode <Type> * GetRoot () const { return root; }
friend istream & operator >> (istream & in, BinaryTree <Type> & Tree);
friend ostream & operator << (ostream & out, BinaryTree <Type> & Tree);
private:
BinTreeNode <Type> * root;
Type RefValue;
BinTreeNode <Type> * Parent (BinTreeNode <Type> * start, BinTreeNode <Type> * current);
int Insert (BinTreeNode <Type> * & current, const Type & item);
void Traverse (BinTreeNode <Type> * current, ostream & out) const;
int Find (BinTreeNode <Type> * current, const Type & item) const;
void destroy (BinTreeNode <Type> * current);
};
template <class Type> void BinaryTree <Type> :: destroy (BinTreeNode <Type> * current)
{
if (current != NULL)
{
destory (current->leftChild);
destory (current->rightChild);
delete current;
}
}
template <class Type> BinTreeNode <Type> * BinaryTree <Type> ::
Parent (BinTreeNode <Type> * start, BinTreeNode <Type> * current)
{
if (start == NULL) return NULL;
if (start->leftChild == current || start->rightChild == current ) return start;
BinTreeNode <Type> * p;
if ((p = Parent (start->leftChild, current)) != NULL) return p;
else return Parent (start->rightChild, current);
};
template <class Type> void BinaryTree <Type> :: Traverse (BinTreeNode <Type> * current, ostream & out) const
{
if (current != NULL)
{
out << current->data<<' ';
traverse (current->leftChild, out);
Traverse (current->rightChild, out);
}
}
template <class Type> istream & opeartor >> (istream & in, BinaryTree <Type> & Tree)
{
Type item;
cout <<"Construct binary tree: \n";
cout <<"Input data (end with " << Tree.RefValue << " ): ";
in >> item;
while (item != Tree.RefValue)
{
Tree.Insert (item);
cout <<"Input data (end with " << Tree.RefValue <<" ): ";
in >>item;
}
cout endl;
return in;
}
template <class Type> ostream & operator << (ostream & out, BinaryTree <Type> & Tree)
{
out <<"Preorder traversal of binary tree. \n";
Tree.Traverse (Tree.root, out);
out <<endl;
return out;
}
////////////////////////////////////////////////////////////////////////
plizi
2001-10-19
打赏
举报
回复
等等
hakuna
2001-10-19
打赏
举报
回复
一会儿给你,稍等
stormywaters
2001-10-19
打赏
举报
回复
中序遍历左子树,访问结点,中序遍历右子树
hakuna
2001-10-19
打赏
举报
回复
但是功能很强大呀
可以不都用,况且中序的代码只有几行而已
算法入门教程——
二叉树
循序渐进的讲解
二叉树
的相关知识。其中包括二叉搜索树的插入、删除与查询、
二叉树
的遍历、求一颗
二叉树
的高度等重要且经典的知识。
二叉树
中序
遍历的三种方法
例如下面的
二叉树
,
中序
遍历的结果如下:[5,10,6,15,2]对于
中序
遍历,直观上的结果就是将
二叉树
所有节点投影到下面的一条直线上,得到的顺序就是
二叉树
的
中序
遍历结果。1、递归法递归方法是最容易想到的方法。递归...
二叉树
的
中序
遍历
二叉树
的
中序
遍历的记忆法则是“左根右",即先遍历左子树节点,再遍历根节点,再遍历右子树节点。 以上图为例,
中序
遍历的结果是【D, B, E, A, F, C, G】 一、解题思路:递归 递归是我们实现前中后序遍历最常用的...
C语言实现
二叉树
的
中序
遍历
这里简单介绍三种
二叉树
中序
遍历的方法。
二叉树
的
中序
遍历就是首先遍历左子树,然后访问当前节点,最后遍历右子树。对于下面的
二叉树
,
中序
遍历结果如下: 结果:[5,10,6,15,2] 直观来看,
二叉树
的
中序
遍历就是将...
线索
二叉树
——
中序
遍历对
二叉树
线索化、对
中序
线索
二叉树
进行
中序
遍历(利用线索实现的非递归算法)、对
中序
线索
二叉树
进行逆向
中序
遍历(利用线索实现的非递归算法)、先序遍历对
二叉树
线索化等操作(王道版)
线索
二叉树
——
中序
遍历对
二叉树
线索化、对
中序
线索
二叉树
进行
中序
遍历(利用线索实现的非递归算法)、对
中序
线索
二叉树
进行逆向
中序
遍历(利用线索实现的非递归算法)、先序遍历对
二叉树
线索化等操作(王道版).......
数据结构与算法
33,008
社区成员
35,326
社区内容
发帖
与我相关
我的任务
数据结构与算法
数据结构与算法相关内容讨论专区
复制链接
扫一扫
分享
社区描述
数据结构与算法相关内容讨论专区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章