设计一个ADT树高分求你们

wuyimin 2004-06-26 01:32:50
设计一个ADT树高分,要求包含以下方法:
1:对于纸面上画出的任意一颗二叉树,用递归程序实现这棵二叉树的机内表示
2:求二叉树的高度
3:判断二叉树是否为完全二叉树
4:非递归地先序遍历二叉树
要求:1;二叉树采用链式储存结构且每个结点为
link info rlink
2.这8个字母任意,输出各字母的编码值
面是编程问题
下面是书面问题内容包含:1.问题的描述:已知什么,要求什么.
2.问题的分析
3:具体的设计:数据结构.算法河人机界面的设计.
4.设计的实现(程序清单,清单应有足够的注释)
5.算例(即测试数据)
6.算法的时间复杂性
7.提出算法的改进方法(如果可能)
高手一定要帮忙啊!求你们了!一定得帮小弟啊!谢谢你门啊!
...全文
120 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hongfeeling 2004-06-28
  • 打赏
  • 举报
回复
楼上的真强啊,学习!
aixuer 2004-06-27
  • 打赏
  • 举报
回复
要注意! 下划线开始的函数都是私有接口。 不要把类内部成员暴露出来。
aixuer 2004-06-27
  • 打赏
  • 举报
回复
#ifndef BINARYTREE_H
#define BINARYTREE_H

#include<iostream>
#include<cassert>
#include<cstddef>
using std::ostream;


template<class T>
class binaryTree
{
public:
// 构造函数
binaryTree();

// 拷贝构造函数
binaryTree(const binaryTree<T> &other);

// 重载复制运算
const binaryTree<T> &operator=(
const binaryTree<T> &other);

// 析构函数
virtual ~binaryTree();

// 销毁二叉树
void destroy(void);

// 插入结点
void insert(const T &element);

// 求树高
int height();

// 求节点总数
int count();

// 求叶结点个数
int leaves();

// 删除结点
void deleteNode(const T& element);

// 打印
void inprint(ostream& out);
void postprint(ostream& out);
void preprint(ostream& out);

protected:

struct Node
{
T data;
Node *llink;
Node *rlink;

// Node类的构造函数
Node(const T &element)
:data(element),llink(NULL),rlink(NULL){}
} *root;

private:

void _destory(Node* &p);

int _height(Node* &p);

int _count(Node* &p);

int _max(int,int);

int _leaf(Node* &p);

void _copy(Node* &p,Node* q);

void _delete(Node* &p);

void _in(ostream& out,Node* &p);

void _pre(ostream& out,Node* &p);

void _post(ostream& out,Node* &p);
};

// 以下是实现部分


// 私有接口:
template<class T>
void binaryTree<T>::_destory(Node* &p)
{
if(p)
{
_destory(p->llink); // 先销毁左子树
_destory(p->rlink); // 再销毁右子树
delete p;
p = NULL;
}
}



template<class T>
int binaryTree<T>::_height(Node* &p)
{
if(p == NULL)
return 0;
else
return 1 + _max(_height(p->llink),_height(p->rlink));
}


template<class T>
int binaryTree<T>::_count(Node* &p)
{
if(p == NULL)
return 0;
else
return 1 + _count(p->llink) + _count(p->rlink);
}



template<class T>
int binaryTree<T>::_max(int a,int b)
{
return a>=b?a:b;
}



template<class T>
int binaryTree<T>::_leaf(Node* &p)
{
static int counter;

if(p == NULL)
return 0;

if(p->llink==NULL && p->rlink==NULL)
counter++;
else
{
_leaf(p->llink);
_leaf(p->rlink);
}

return counter;
}


template<class T>
void binaryTree<T>::_copy(Node* &p,Node* q)
{
if(q==NULL)
p = NULL;
else
{
// 使用引用的原因
p = new Node(q->data);
assert(p);
_copy(p->llink,q->llink);
_copy(p->rlink,q->rlink);
}
}



template<class T>
void binaryTree<T>::_delete(Node* &p)
{
Node *current;
Node *temp;
Node *trailCurrent;

if(p==NULL)
return;
// 删除叶结点
else if(p->llink==NULL && p->rlink==NULL)
{
temp = p;
p = NULL;
delete temp;
}
// p没有左子树
else if(p->llink == NULL)
{
temp = p;
p = temp->rlink;
delete temp;
}
// p没有右子树
else if(p->rlink == NULL)
{
temp = p;
p = temp->llink;
delete temp;
}
// p左右子树都有
else
{
current = p->llink;
trailCurrent = NULL;

// current移到到p左子树最右边的结点
while(current->rlink != NULL)
{
trailCurrent = current;
current = current->rlink;
}

p->data = current->data;

if(trailCurrent->rlink == NULL)
p->llink = current->llink;
else
trailCurrent->rlink = current->llink;
}
}


template<class T>
void binaryTree<T>::_in(ostream& out,Node* &p)
{
if(p != NULL)
{
_in(out,p->llink);
out<<'['<< p->data <<"] ";
_in(out,p->rlink);
}
}



template<class T>
void binaryTree<T>::_post(ostream& out,Node* &p)
{
if(p != NULL)
{
_in(out,p->llink);
_in(out,p->rlink);
out<<'['<< p->data <<"] ";
}
}



template<class T>
void binaryTree<T>::_pre(ostream& out,Node* &p)
{
if(p != NULL)
{
out<<'['<< p->data <<"] ";
_in(out,p->llink);
_in(out,p->rlink);
}
}




// 公有接口:
template<class T>
binaryTree<T>::~binaryTree<T>()
{
_destory(root);
}



template<class T>
binaryTree<T>::binaryTree()
{
root = NULL;
}



template<class T>
void binaryTree<T>::destroy()
{
_destory(root);
}



template<class T>
void binaryTree<T>::insert(const T &element)
{
Node *trailCurrent;
Node *current = root;
Node *newNode = new Node(element);

assert(newNode);

// 二叉树为空
if(!root)
{
root = newNode;
return;
}

// 找到插入结点的位置 trailCurrent
while(current)
{
// trailCurrent 始终是 current 的父结点
trailCurrent = current;

if(current->data == element)
return;
else if(current->data > element)
current = current->llink;
else
current = current->rlink;
}

// 安插结点
if(trailCurrent->data > element)
trailCurrent->llink = newNode;
else
trailCurrent->rlink = newNode;

delete current;
}



template<class T>
int binaryTree<T>::height()
{
return _height(root);
}



template<class T>
int binaryTree<T>::count()
{
return _count(root);
}



template<class T>
int binaryTree<T>::leaves()
{
return _leaf(root);
}


template<class T>
binaryTree<T>::binaryTree(const binaryTree<T> &other)
{
if(other.root == NULL)
root == NULL;
else
_copy(root,other.root);
}



template<class T>
const binaryTree<T>& binaryTree<T>::operator =(
const binaryTree<T> &other)
{
// 防备自我拷贝
if(this == &other)
return *this;

// 销毁原树
_destory(root);

if(other.root == NULL)
{
root = NULL;
return *this;
}

_copy(root,other.root);

return *this;
}



template<class T>
void binaryTree<T>::deleteNode(const T &element)
{
Node *current;
Node *trailCurrent;
bool found = false;

if(root == NULL)
return;
else
{
current = root;
trailCurrent = root;

while(current != NULL && !found)
{
if(current->data == element)
found = true;
else
{
trailCurrent = current;

if(element < current->data)
current = current->llink;
else
current = current->rlink;
}
}
// 没有找到元素
if(current = NULL)
return;
else
if(current == root)
_delete(root);
else if(element < trailCurrent->data)
_delete(trailCurrent->llink);
else
_delete(trailCurrent->rlink);
}
}


template<class T>
void binaryTree<T>::inprint(ostream& out)
{
_in(out,root);
}


template<class T>
void binaryTree<T>::postprint(ostream& out)
{
_post(out,root);
}


template<class T>
void binaryTree<T>::preprint(ostream& out)
{
_pre(out,root);
}


#endif // BINARYTREE_H

二叉树模板在这里, 人机界面的设计你自己搞定吧。
非递归地先序遍历二叉树,也不是很难,自己好好想想。

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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