火急!!!!!!!!(在线等)

menxiang 2003-11-10 06:55:35
建立一个二叉树,并遍历!
...全文
26 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhouqingyuan 2003-11-10
  • 打赏
  • 举报
回复
up
aflyinghorse 2003-11-10
  • 打赏
  • 举报
回复
另一个版本
void create(TNODE2 **p)
{
char ch;
scanf("%c",&ch);
if (ch == '#') *p = NULL;
else{
*p = (TNODE2 *) malloc(sizeof(TNODE2));
(*p)->data = ch;
create(&((*p)->lchild));
create(&((*p)->rchild));
}
}
输入:abc##de#g##f###
rerli 2003-11-10
  • 打赏
  • 举报
回复
本来要回家了,看你这么急,特给你赶出来,TC2编译通过。
结帖给分吧:)。回家了。

#include <stdlib.h>

typedef struct node2
{
char data;
struct node2 *lchild;
struct node2 *rchild;
} TNODE2;

/*设字符列首元素指针为str,根节点为str[i],则它的左、右节点分别为str[2*i+1]和str[2*i+2]*/
TNODE2 *build(char *str, int i, int n)
{
TNODE2 *p;

if (i>=n)
{
return NULL;
}

p = (TNODE2 *) malloc(sizeof(TNODE2));
p->data = str[i];
p->lchild = build(str, 2*i+1, n);
p->rchild = build(str, 2*i+2, n);
return p;
}

/*前序遍历*/
void re_preorder(TNODE2 *p)
{
if (p != NULL)
{
printf("%c",p->data);
re_preorder(p->lchild);
re_preorder(p->rchild);
}
}

main()
{
static char str[7] = {'A','B','C','D','E','F','G'};
TNODE2 *p;

p = build(str,0,7);
re_preorder(p);

printf("\n");
system("pause");
}
menxiang 2003-11-10
  • 打赏
  • 举报
回复
用C语言!
manyroads 2003-11-10
  • 打赏
  • 举报
回复

// LevelOrder and the private recursive PreOrder,
// InOrder, and PostOrder mthods have been changed
// from template functions to integer functions because
// Visual C++ is unable to reslove overloaded functions
// when these were template functions

int _count;

#include<iostream.h>
#include "lqueue.h"
#include "btnode2.h"
#include "xcept.h"

template<class E, class K> class BSTree;
template<class E, class K> class DBSTree;

template<class T>
class BinaryTree {
friend BSTree<T,int>;
friend DBSTree<T,int>;
public:
BinaryTree() {root = 0;};
~BinaryTree(){};
bool IsEmpty() const
{return ((root) ? false : true);}
bool Root(T& x) const;
void MakeTree(const T& element,
BinaryTree<T>& left, BinaryTree<T>& right);
void BreakTree(T& element, BinaryTree<T>& left,
BinaryTree<T>& right);
void PreOrder(void(*Visit)(BinaryTreeNode<T> *u))
{PreOrder(Visit, root);}
void InOrder(void(*Visit)(BinaryTreeNode<T> *u))
{InOrder(Visit, root);}
void PostOrder(void(*Visit)(BinaryTreeNode<T> *u))
{PostOrder(Visit, root);}
void LevelOrder(void(*Visit)(BinaryTreeNode<T> *u));
void PreOutput() {PreOrder(Output, root); cout << endl;}
void InOutput() {InOrder(Output, root); cout << endl;}
void PostOutput() {PostOrder(Output, root); cout << endl;}
void LevelOutput() {LevelOrder(Output); cout << endl;}
void Delete() {PostOrder(Free, root); root = 0;}
int Height() const {return Height(root);}
int Size()
{_count = 0; PreOrder(Add1, root); return _count;}
private:
BinaryTreeNode<T> *root; // pointer to root
void PreOrder(void(*Visit)
(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t);
void InOrder(void(*Visit)
(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t);
void PostOrder(void(*Visit)
(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t);
static void Free(BinaryTreeNode<T> *t) {delete t;}
static void Output(BinaryTreeNode<T> *t)
{cout << t->data << ' ';}
static void Add1(BinaryTreeNode<T> *t) {_count++;}
int Height(BinaryTreeNode<T> *t) const;
};

template<class T>
bool BinaryTree<T>::Root(T& x) const
{// Return root data in x.
// Return false if no root.
if (root) {x = root->data;
return true;}
else return false; // no root
}

template<class T>
void BinaryTree<T>::MakeTree(const T& element,
BinaryTree<T>& left, BinaryTree<T>& right)
{// Combine left, right, and element to make new tree.
// left, right, and this must be different trees.
// create combined tree
root = new BinaryTreeNode<T>
(element, left.root, right.root);

// deny access from trees left and right
left.root = right.root = 0;
}

template<class T>
void BinaryTree<T>::BreakTree(T& element,
BinaryTree<T>& left, BinaryTree<T>& right)
{// left, right, and this must be different trees.
// check if empty
if (!root) throw BadInput(); // tree empty

// break the tree
element = root->data;
left.root = root->LeftChild;
right.root = root->RightChild;

delete root;
root = 0;
}

// template<class T>
// void BinaryTree<T>::PreOrder(
// void(*Visit)(BinaryTreeNode<T> *u),
// BinaryTreeNode<T> *t)
void BinaryTree<int>::PreOrder(
void(*Visit)(BinaryTreeNode<int> *u),
BinaryTreeNode<int> *t)
{// Preorder traversal.
if (t) {Visit(t);
PreOrder(Visit, t->LeftChild);
PreOrder(Visit, t->RightChild);
}
}

// template <class T>
// void BinaryTree<T>::InOrder(
// void(*Visit)(BinaryTreeNode<T> *u),
// BinaryTreeNode<T> *t)
void BinaryTree<int>::InOrder(
void(*Visit)(BinaryTreeNode<int> *u),
BinaryTreeNode<int> *t)
{// Inorder traversal.
if (t) {InOrder(Visit, t->LeftChild);
Visit(t);
InOrder(Visit, t->RightChild);
}
}

// template <class T>
// void BinaryTree<T>::PostOrder(
// void(*Visit)(BinaryTreeNode<T> *u),
// BinaryTreeNode<T> *t)
void BinaryTree<int>::PostOrder(
void(*Visit)(BinaryTreeNode<int> *u),
BinaryTreeNode<int> *t)
{// Postorder traversal.
if (t) {PostOrder(Visit, t->LeftChild);
PostOrder(Visit, t->RightChild);
Visit(t);
}
}

// template <class T>
// void BinaryTree<T>::LevelOrder(
// void(*Visit)(BinaryTreeNode<T> *u))
void BinaryTree<int>::LevelOrder(
void(*Visit)(BinaryTreeNode<int> *u))
{// Level-order traversal.
LinkedQueue<BinaryTreeNode<T>*> Q;
BinaryTreeNode<T> *t;
t = root;
while (t) {
Visit(t);
if (t->LeftChild) Q.Add(t->LeftChild);
if (t->RightChild) Q.Add(t->RightChild);
try {Q.Delete(t);}
catch (OutOfBounds) {return;}
}
}

template <class T>
int BinaryTree<T>::Height(BinaryTreeNode<T> *t) const
{// Return height of tree *t.
if (!t) return 0; // empty tree
int hl = Height(t->LeftChild); // height of left
int hr = Height(t->RightChild); // height of right
if (hl > hr) return ++hl;
else return ++hr;
}

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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