二叉树的建立与遍历

因为太懒了就直接上代码吧
遍历方法包含前、中、后序遍历以及层次遍历。

#include<iostream>
#include<queue>
using namespace std;
typedef struct Btree {
	int val;
	struct Btree* lchild;
	struct Btree* rchild;
	Btree():val(0),lchild(nullptr),rchild(nullptr){}
}node;
int luo_time = 1;
node* root = new node();
node* FindVal(const int& x,node* q) {//找出val值为x的节点,返回其地址
	if (q->val == x&&q->lchild==nullptr&&q->rchild==nullptr) {
		return q;
	}
	if (q->lchild != nullptr) {
		node* w = FindVal(x, q->lchild);
		if (w != nullptr) {
			return w;
		}
	}
	if (q->rchild != nullptr) {
		node* w = FindVal(x, q->rchild);
		//return w;
		if (w != nullptr) {
			return w;
		}
	}
	return nullptr;
}
void AddNode(int par, int lch, int rch) {//为val值为par的节点添加左右孩子节点
	Btree* f = FindVal(par, root);
	if (f == nullptr) {
		cout << "此次插入不成功!!!" << endl;
	}
	else {
		node* temp1 = new node();
		node* temp2 = new node();
		temp1->val = lch, temp2->val = rch;
		f->lchild = temp1, f->rchild = temp2;
	}
}
void PrintLevel(node* m) {//层次遍历
	cout << "层次遍历:" << endl;
	queue<node*>p;
	p.push(m);
	while (!p.empty()) {
		node* x = p.front();
		cout << x->val << " ";
		p.pop();
		if (x->lchild != nullptr) {
			p.push(x->lchild);
		}
		if (x->rchild != nullptr) {
			p.push(x->rchild);
		}
	}
}
void PrintBtreeq(node* m) {//前序遍历二叉树
	if (m != nullptr) {
		cout << m->val << " ";
		if (m->lchild != nullptr) {
			PrintBtreeq(m->lchild);
		}
		if (m->rchild != nullptr) {
			PrintBtreeq(m->rchild);
		}
	}
	else {
		return;
	}
}
void PrintBtreez(node* m) {//中序遍历
	if (m != nullptr) {
		if (m->lchild != nullptr) {
			PrintBtreez(m->lchild);
		}
		cout << m->val << " ";
		if (m->rchild != nullptr) {
			PrintBtreez(m->rchild);
		}
	}
}
void PrintBtreeh(node* m) {//后序遍历
	if (m != nullptr) {
		if (m->lchild != nullptr) {
			PrintBtreeh(m->lchild);
		}
		if (m->rchild != nullptr) {
			PrintBtreeh(m->rchild);
		}
		cout << m->val << " ";
	}
}
void CreatBtree() {//创建二叉树
	int par, lch, rch;
	cout << "输入二叉树的节点:(范例:1 2 3,这是以1为父亲节点,2和3分别为1的左右孩子节点)" << endl;
	cout << "当我们输入0 0 0时即代表停止输入!" << endl;
	cin >> par >> lch >> rch;
	while (par != 0 || lch != 0 || rch != 0) {
		if (luo_time == 1) {
			root->val = par;
			node* tmp1 = new node();
			node* tmp2 = new node();
			//f->lchild = new Btree(), f->rchild = new Btree();
			tmp1->val = lch, tmp2->val = rch;
			root->lchild = tmp1, root->rchild = tmp2;
		}
		else {
			AddNode(par, lch, rch);
		}
		//cout << "输出测试:" << endl;
		//PrintBtreeh(root);
		//cout << endl;
		luo_time++;
		cin >> par >> lch >> rch;
	}
}
int main() {
	CreatBtree();
	PrintLevel(root);
	cout << "\n" << "前序遍历:" << endl;
	PrintBtreeq(root);
	cout << "\n" << "中序遍历:" << endl;
	PrintBtreez(root);
	cout << "\n" << "后序遍历:" << endl;
	PrintBtreeh(root);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉树是一种树形结构,它的每个节点最多只有两个子节点。二叉树遍历方式有三种:先序遍历、中序遍历和后序遍历。其中,先序遍历是指先访问根节点,然后访问左子树,最后访问右子树;中序遍历是指先访问左子树,然后访问根节点,最后访问右子树;后序遍历是指先访问左子树,然后访问右子树,最后访问根节点。 二叉树建立可以通过递归或非递归方式实现。递归方式建立二叉树的过程是:先读入一个节点的值,如果该节点的值不为空,则创建一个新节点,并将该节点的值赋给新节点;然后递归调用建立左子树和右子树的过程,直到读入的节点值为空为止。非递归方式建立二叉树的过程是:使用一个栈来存储节点,先读入根节点的值,创建一个新节点,并将该节点入栈;然后读入下一个节点的值,如果该节点的值不为空,则创建一个新节点,并将该节点入栈,并将该节点作为上一个节点的左子节点;如果该节点的值为空,则弹出栈顶节点,并将该节点作为上一个节点的右子节点。 二叉树遍历算法可以通过递归或非递归方式实现。递归方式实现二叉树遍历算法比较简单,只需要按照遍历顺序递归访问左子树和右子树即可。非递归方式实现二叉树遍历算法需要使用栈来存储节点。先序遍历的非递归算法是:先将根节点入栈,然后弹出栈顶节点并访问该节点,将该节点的右子节点入栈,再将该节点的左子节点入栈,重复上述过程直到栈为空;中序遍历的非递归算法是:先将根节点入栈,然后将根节点的所有左子节点入栈,重复弹出栈顶节点并访问该节点,将该节点的右子节点入栈,重复上述过程直到栈为空;后序遍历的非递归算法是:先将根节点入栈,然后将根节点的所有左子节点入栈,重复弹出栈顶节点并访问该节点,如果该节点的右子节点不为空且未被访问过,则将该节点的右子节点入栈,否则访问该节点,重复上述过程直到栈为空。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值