高分悬赏二叉树的建树中序偏历

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
  • 打赏
  • 举报
回复
但是功能很强大呀
可以不都用,况且中序的代码只有几行而已

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧