第4章-二叉树的输出程序

程序的核心思想:http://wenku.baidu.com/link?url=c9yGE_yu-PLWNVq8IB2Lmd96-vpI5Vp0n23-FwywOXKmo3VZ1rmXGRLQ4BzGRTq_Y59Ps3lEegveMU5zYq1S846grtXGYt8DyagZWEvHa0i

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <list>
#include <algorithm>
#include "SearchTree.h"
#include <graphics.h>
#include <string>
#include <conio.h>
using namespace std;

void Tree2Nodes(SearchTree T, int nodes[][4], int k = 1)	//数组从1开始编号,不能从0开始
{
	if(T != NULL)
	{
		Tree2Nodes(T->Left, nodes, 2*k);
		nodes[k][2] = 1;
		nodes[k][3] = T->Element;
		Tree2Nodes(T->Right, nodes, 2*k + 1);
	}
}

void Draw(SearchTree T, int nodes[][4], int radius,  int k = 1)
{
	if(T != NULL)
	{
		Draw(T->Left, nodes, radius, 2 * k);
		setlinecolor(BLUE);
		circle(nodes[k][0], nodes[k][1], radius);
		char s[10];
		sprintf(s, "%d", nodes[k][3]);
		settextstyle(2 * radius, 0, _T("宋体"));
		outtextxy(nodes[k][0] + radius + 1, nodes[k][1] - radius, s);
		setlinecolor(YELLOW);
		if(nodes[2 * k][2] == 1)
			line(nodes[k][0], nodes[k][1] + radius, nodes[2 * k][0], nodes[2 * k][1] - radius);
		if(nodes[2 * k + 1][2] == 1)
			line(nodes[k][0], nodes[k][1] + radius, nodes[2 * k + 1][0], nodes[2 * k + 1][1] - radius);
		Draw(T->Right, nodes, radius, 2 * k + 1);
	}
}

void DisplayTree(SearchTree T)
{
	int width = 640 / (pow(2, Depth(T) - 1) + 1), height = 0.85 * 480 / (Depth(T) - 1);	//作图区的宽640,高480
	int radius = 0.125 * width;
	int nodes[1024][4] = {0};		//最多可以输出10层的树
	int depth = Depth(T);
	Tree2Nodes(T, nodes);
	for(int i = depth; i >= 1; i--)
	{
		int first;			//每一层中,首结点的横坐标
		if(i == depth)
			first = width;
		else
			first = (int)(pow(2, depth - i - 1)*width + width / 2);
		int w;				//每一层中,相邻两结点的距离
		w = (int)(pow(2, depth - i) * width);
		for(int j = (int)(pow(2, i - 1)); j <= (int)(pow(2, i) - 1); j++)
		{
			if(nodes[j][2] == 1)
			{
				nodes[j][0] = (int)(first + (j - pow(2, i - 1)) * w);
				nodes[j][1] = height * (i - 1) + radius;
			}
		}
	}

	Draw(T, nodes, radius);

}

int main()
{
	
	SearchTree T = NULL;
	T = Insert(20, T);
	T = Insert(10, T);
	T = Insert(30, T);
	initgraph(640, 480);
	DisplayTree(T);
	getch();
	closegraph();
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来为您解答这个问题。 首先,先序序列创建二叉树的过程是这样的: 1. 如果先序序列为空,则二叉树为空。 2. 否则,取出先序序列的第一个元素作为根节点,然后将先序序列分为左子树和右子树两部分。 3. 对左子树和右子树分别进行递归,生成左子树和右子树。 下面是使用 C++ 代码实现先序序列创建二叉树的过程: ```c++ #include <iostream> using namespace std; // 二叉树结点的定义 struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; // 先序序列创建二叉树 TreeNode* createTree(vector<int>& preorder, int& index) { if (index >= preorder.size() || preorder[index] == -1) { // 先序序列遍历完毕或者遇到空节点 return nullptr; } // 创建当前结点 TreeNode* root = new TreeNode(preorder[index]); // 递归创建左子树和右子树 index++; root->left = createTree(preorder, index); index++; root->right = createTree(preorder, index); return root; } // 中序遍历二叉树 void inorderTraversal(TreeNode* root, vector<int>& inorder) { if (root == nullptr) { return; } inorderTraversal(root->left, inorder); inorder.push_back(root->val); inorderTraversal(root->right, inorder); } // 后序遍历二叉树 void postorderTraversal(TreeNode* root, vector<int>& postorder) { if (root == nullptr) { return; } postorderTraversal(root->left, postorder); postorderTraversal(root->right, postorder); postorder.push_back(root->val); } // 计算叶子结点的数量 int countLeaves(TreeNode* root) { if (root == nullptr) { return 0; } if (root->left == nullptr && root->right == nullptr) { return 1; } return countLeaves(root->left) + countLeaves(root->right); } int main() { vector<int> preorder = {1, 2, 4, -1, -1, 5, -1, -1, 3, -1, 6, 7, -1, -1, -1}; int index = 0; // 先序序列创建二叉树 TreeNode* root = createTree(preorder, index); // 中序遍历二叉树 vector<int> inorder; inorderTraversal(root, inorder); // 后序遍历二叉树 vector<int> postorder; postorderTraversal(root, postorder); // 计算叶子结点的数量 int leaves = countLeaves(root); // 输出先序序列、中序序列、后序序列和叶子结点数量 cout << "先序序列:"; for (int x : preorder) { cout << x << " "; } cout << endl; cout << "中序序列:"; for (int x : inorder) { cout << x << " "; } cout << endl; cout << "后序序列:"; for (int x : postorder) { cout << x << " "; } cout << endl; cout << "叶子结点数量:" << leaves << endl; return 0; } ``` 这个程序输出结果为: ``` 先序序列:1 2 4 -1 -1 5 -1 -1 3 -1 6 7 -1 -1 -1 中序序列:4 2 5 1 6 3 7 后序序列:4 5 2 6 7 3 1 叶子结点数量:5 ``` 注意,我们在输入先序序列的时候,用 `-1` 表示空节点。在输出中序序列、后序序列和叶子结点数量的时候,我们可以先递归遍历左子树,再输出当前结点的值,最后递归遍历右子树,这样就可以得到中序序列和后序序列了。计算叶子结点的数量也可以使用递归的方法实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值