(复制可用)数据结构之二叉树的创建、前序、中序后续遍历

#include “queue”#include “stack”#include “vector”#includeusing namespace std;#include#include using namespace std;class BiNode {public:char data;struct BiNode *lchild, *rchild;};class BiT...
摘要由CSDN通过智能技术生成

#include “queue”
#include “stack”
#include “vector”
#include
using namespace std;
#include
#include
using namespace std;
class BiNode {
public:
char data;
struct BiNode *lchild, *rchild;
};

class BiTree {
private:
BiNode * root;
int height;
void pre_Order(BiNode * t);
void in_Order(BiNode * t);
void post_Order(BiNode * t);
BiNode* create(string &s, int&pos);
void get_Height(BiNode *t, int h);
public:
BiTree() { root = NULL; height = 0; }
///按照前序遍历序列创建二叉树
void createBiTree(string s);
///前序遍历二叉树
void preOrder();
///中序遍历二叉树
void inOrder();
///后序遍历二叉树(递归方法)
void postOrder();
///后序遍历二叉树(使用栈的非递归方法)
void postOrder1();
///层序遍历二叉树
void levelOrder();
///求树的高度
int getHeight();
///求两个节点的最大公共祖先
void ancestor(char A, char B);
};

///递归创建二叉树,如果是#表示空节点
BiNode * BiTree::create(string &s, int &pos) {
++pos;
BiNode * t;
if ((unsigned)pos >= s.size())
return NULL;
else {
if (s[pos] == ‘#’)
t = NULL;
else {
t = new BiNode;
t->data = s[pos];
t->lchild = create(s, pos);
t->rchild = create(s, pos);
}
return t;
}
}
//我实现的:
//BiNode BiTree::create(string &s, int &pos) {
// pos++;
// BiNode
t;
// if (pos >= s.size()) return NULL;
// else {
// if (s[pos] == ‘#’) t = NULL;
// else {
// t = new BiNode;
// t->data = s[pos];
// t->lchild = create(s, pos);
// t->rchild = create(s, pos);
// }
// return t;
// }
//}
///按照前序遍历序列创建二叉树
void BiTree::createBiTree(string s) {
int pos = -1;
root = create(s, pos);
}
///前序遍历二叉树
void BiTree::preOrder() {
pre_Order(root);
cout << endl;
}
void BiTree::pre_Order(BiNode * t) {
if (t != NULL) {
cout << t->data << ’ ';
pre_Order(t->lchild);
pre_Order(t->rchild);
}
}
//自己实现:
//void BiTree::pre_Order(BiNode *t) {
// if (t != NULL) {
// cout << t->data << " ";
// pre_Order(t->lchild);
// pre_Order(t->rchild);
// }
//}
///中序遍历二叉树
void BiTree::inOrder() {
in_Order(root);
cout << endl;
}
void BiTree::in_Order(BiNode *t) {
if (t != NULL) {
in_Order(t->lchild);
cout << t->data << ’ ';
in_Order(t->rchild);
}
}
///后序遍历二叉树(递归方法)
void BiTree::postOrder() {
post_Order(root);
cout << endl;
}
void BiTree::post_Order(BiNode *t) {
if (t != NULL) {
post_Order(t->lchild);
post_Order(t->rchild);
cout << t->data << ’ ';
}
}
///后序遍历二叉树(使用栈的非递归方法)
///后续遍历先遍历左子树,再遍历右子树,最后遍历根节点
///对于一个节点而言,先一直遍历到最左节点
///然后用r记录右子树是否遍历,如果没有遍历,则遍历右子树
void BiTree::postOrder1() {
///p表示当前树节点指针,
///r表示最近访问的树节点指针
BiNode *p, r;
r = NULL;
p = root;
stack<BiNode
> my_stack;
while (p != NULL || !my_stack.empty()) {
if § {
///一直走到树的最左边
my_stack.push§;
p = p->lchild;
}
else {
p = my_stack.top();
///如果右子树没有遍历,遍历右子树
if (p->rchild != NULL && p->rchild != r) {
p = p->rchild;
my_stack.push§;
///注意这里需要向左转,因为如果不向左转,
///将会遍历右子树节点两边
p = p->lchild;

		}
		///否则遍历根节点
		else {
			p = my_stack.top();
			my_stack.pop();
			cout << p->data << ' ';
			///更新最近遍历的节点
			r = p;
			///将遍历后的节点设为NULL,进行下一个节点的遍历
			p = NULL;
		}
	}
}
cout << endl;

}
///使用队列进行层序遍历二叉树
void BiTree::levelOrder() {
if (root == NULL)
return;
queue<BiNode*> q;
q.push(root);
while (!q.empty()) {
BiNode * t;
t = q.front();
q.pop();
cout << t->data << ’ ';
if (t->lchild != NULL)
q.push(t->lchild);
if (t->rchild != NULL)
q.push(t->rchild);
}
cout << endl;
}
///求树的高度
int BiTree::getHeight() {
get_Height(root, 0);
return height;
}
void BiTree::get_Height(BiNode *t, int h) {
if (t != NULL) {
++h;
if (h > height)
height = h;
get_Height(t->lchild, h);
get_Height(t->rchild, h);
}
}

int main()
{
BiTree a;
string s;
s = “ABD##E#F##C##”;
a.createBiTree(s);
cout << “前序遍历:” << endl;
a.preOrder();
cout << “中序遍历:” << endl;
a.inOrder();
cout << “后序遍历1:” << endl;
a.postOrder();
cout << “后序遍历2:” << endl;
a.postOrder1();
cout << “层序遍历:” << endl;
a.levelOrder();
cout << “树高:” << endl;
cout << a.getHeight() << endl;
system(“pause”);
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值