当前位置:首页 » 《随便一记》 » 正文

实验3完整代码_笑霸fianl的博客

11 人参与  2022年04月16日 13:59  分类 : 《随便一记》  评论

点击全文阅读


#include<iostream>
#include<Windows.h>
using namespace std;
//创建二叉树结构体;
typedef struct BiTNode {
	int data;
	struct BiTNode *lchild, *rchild;//左右孩子指针 
}BiTNode, *BiTree;
//构建一个循环队列
typedef struct Qnode {
	BiTNode *base;
	int front;//头
	int rear;//尾
}sqQueue;
class tree {
private:
	BiTree root;
	sqQueue q;
public:

	tree() {
		CreatBiTree(root);
	}
	BiTree get_root() {//得到root节点;
		return root;
	}
	//创建一个循环队列
	void InitQueue(sqQueue &q) {
		q.base = new BiTNode[5];
		q.front = q.rear = 0;
	}
	//创建二叉树
	BiTree CreatBiTree(BiTree &t) {
		int val;
		cin >> val;
		getchar();
		if (val <= 0) t = NULL;
		else {
			t = new BiTNode;
			t->data = val;
			CreatBiTree(t->lchild);
			CreatBiTree(t->rchild);
		}
		return t;
	}
	//先序遍历
	void PreOrderTraverse(BiTree &t) {
		if (!t) return;
		else {
			cout << t->data << " ";
			PreOrderTraverse(t->lchild);
			PreOrderTraverse(t->rchild);
		}
	}
	//中序排序
	void InOrderTraverse(BiTree &t) {
		if (!t) return;
		else {
			InOrderTraverse(t->lchild);
			cout << t->data << " ";
			InOrderTraverse(t->rchild);
		}
	}
	//后序遍历
	void PostOrderTraverse(BiTree &t) {
		if (!t) return;
		else {
			PostOrderTraverse(t->lchild);
			PostOrderTraverse(t->rchild);
			cout << t->data << " ";
		}
	}
	//层序遍历
	void LevelOrderTraverse(BiTree &t) {
		if (!t)return;
		else {
			InitQueue(q);
			q.base[q.rear] = *t;
			q.rear = (q.rear + 1) % 5;
		}
		while (q.front != q.rear) {
			BiTNode temp = q.base[q.front];
			cout << temp.data << " ";
			if (temp.lchild) {
				q.base[q.rear] = *temp.lchild;
				q.rear = (q.rear + 1) % 5;
			}
			if (temp.rchild) {
				q.base[q.rear] = *temp.rchild;
				q.rear = (q.rear + 1) % 5;
			}
			q.front = (q.front + 1) % 5;
		}
	}
	//统计节点的数目
	void Count_TreeEnds(BiTree &t, int &n) {
		if (t) {
			if (!t->lchild && !t->rchild)
				n++;
			Count_TreeEnds(t->lchild, n);
			Count_TreeEnds(t->rchild, n);
		}

	}
	//交换左右子树
	BiTree Exchange(BiTree &t) {
		if (!t)return NULL;
		BiTree lchild = Exchange(t->rchild);
		BiTree rchild = Exchange(t->lchild);
		t->lchild = lchild;
		t->rchild = rchild;
		return t;
	}
	//5. 计算并输出该二叉树的深度。
	int Depth(BiTree &t) {
		int l = 0, r = 0;
		if (t == NULL) return 0;
		l = Depth(t->lchild) + 1;
		r = Depth(t->rchild) + 1;
		return l > r ? l : r;
	}


};
int main() {
	tree T;
	int n = 0;//叶子结点
	int deep;//深度
	BiTree PT = T.get_root();
	cout << "先序遍历:";
	T.PreOrderTraverse(PT); cout << endl;
	cout << "中序遍历:";
	T.InOrderTraverse(PT); cout << endl;
	cout << "后序遍历:";
	T.PostOrderTraverse(PT); cout << endl;
	cout << "层序遍历:";
	T.LevelOrderTraverse(PT); cout << endl;
	cout << "叶子节点数:";
	T.Count_TreeEnds(PT, n);
	cout << n << endl;
	BiTree exT;
	exT = T.Exchange(PT);
	cout << "交换后的先序遍历:";
	T.PreOrderTraverse(exT); cout << endl;
	cout << "该二叉树的深度:";
	deep = T.Depth(exT); cout << deep << endl;
	system("pause");
	return 0;
}

点击全文阅读


本文链接:http://m.zhangshiyu.com/post/38183.html

遍历  节点  二叉树  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 傅庭州的春到南楼雪尽处精修版傅庭州苏枝夏全书在线
  • 「高武:我,大器晚成,儿子直呼躺赢」全文完结版阅读_刘江乙柳如烟全角色番外合集包
  • (番外)+(结局)我在回忆里万劫不复谢梵声秦见鹿结局+番外(秦见鹿谢梵声)完结_(我在回忆里万劫不复谢梵声秦见鹿结局+番外)列表_笔趣阁(秦见鹿谢梵声)
  • 凛月栖春山完结(关凛月程衍)全书免费_(关凛月程衍)凛月栖春山完结后续(关凛月程衍)
  • 放弃救赎师尊后,看他尸骨无存我笑了精彩章节试读_「师尊林云玉骨琴」全章节免费阅读
  • 春到南楼雪尽处(傅庭州苏枝夏)全书免费_(傅庭州苏枝夏)春到南楼雪尽处后续(傅庭州苏枝夏)
  • 完结文常梨厉晏舟列表_完结文常梨厉晏舟
  • 重返1977:从成为***状元开始剧情悬念章节分段解锁_「高远李健群」完结版全文
  • (番外)+(全书)君心如竹,此生荒芜结局+番外(沈南音贺方竹)完结_(沈南音贺方竹)列表_笔趣阁(君心如竹,此生荒芜结局+番外)
  • 剜月寄无书结局+番外(姜若凝楚淮舟)_(剜月寄无书结局+番外)列表_笔趣阁(姜若凝楚淮舟)
  • 剜月寄无书免费赏析(姜若凝楚淮舟)_剜月寄无书免费赏析姜若凝楚淮舟
  • (番外)+(全书)(瀛洲晚晚)_反派暴君难攻略全书+后续列表_笔趣阁(瀛洲晚晚)(瀛洲晚晚)完结_(瀛洲晚晚)列表_笔趣阁(反派暴君难攻略全书+后续)

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1