第十二周数据结构作业 (二叉查找树C语言代码实现)

#include<iostream>
#include<stdio.h>
#include<stack>
#include<queue>
#include<malloc.h>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<opencv.hpp>

using namespace cv;
using namespace std;
//struct area

typedef struct node {
	int data;
	struct node *left_child;
	struct node *right_child;
}BST;
typedef struct {
	int x;
	int y;
}POS;
//universal variance area 
int Mapsequence[60] = {0};
int countMap = 1;
POS pos;
//function announce
BST *SortTreeCreate();
BST *SortTreeSearch(BST *root, int key);
void Levelorder(BST *Tree);
void mapping(BST *T);
void Posfunction(int num);
void drawnode(int b, int g, int r);

Mat image(1000, 1000, CV_8UC3);
int main()
{
	namedWindow("tree");
	int key,i=1; BST *root=NULL, *p = NULL;
	printf("Please enter the key you want to search:");
	scanf("%d", &key);
	root=SortTreeCreate();
	mapping(root);
	drawnode(0, 255, 0);
	p=SortTreeSearch(root,key);
	if (p)
	{
		printf("The key:%d is exisiting!\n", p->data);
		while (Mapsequence[i++] != 0)
		{
			if (Mapsequence[i - 1] == p->data)break;
		}
		Posfunction(i - 1);
		circle(image, Point(pos.x, pos.y), 25, Scalar(0, 0, 255), -1);
		imshow("tree", image);
		waitKey(0);
	}
	else
		printf("No this key in Binary Search Tree!\n");

}

//function area 
BST *SortTreeCreate()
{
	int data,n=0,i=0,runone=1;
	FILE *fp;
	BST *root=NULL, *p,*parent,*child;
	if (!(fp=fopen("C:\\Users\\chenh\\Desktop\\新建文件夹 (3)\\DATA.txt", "r")))
	{
		printf("Open the file error!\n");
		exit(1);
	}
	//This while is to read the data data file and creat Binary Search Tree
	while (!feof(fp))
	{
		fscanf(fp, "%d\n", &data);
		if (runone)
		{
			root = (BST *)malloc(sizeof(BST));
			runone = 0;
			root->data = data; root->left_child = NULL; root->right_child = NULL;
			continue;
		}
		p = (BST *)malloc(sizeof(BST));
		p->data = data; p->left_child = NULL; p->right_child = NULL;
		parent = root;
		//This while is to add node to tree in proper postion
		while (1)
		{
			if (data <= parent->data)
			{
				if (parent->left_child == NULL)
				{
					parent->left_child = p;
					break;
				}
				else
				{
					parent = parent->left_child;
				}
			}
			else
			{
				if (parent->right_child == NULL)
				{
					parent->right_child = p;
					break;
				}
				else
				{
					parent = parent->right_child;
				}
			}
		}  
	}
	return root;
}
BST *SortTreeSearch(BST *root,int key)
{
	if (!root) return NULL;//if root is null
	if (key == root->data) return root;
	if (key < root->data)
		return SortTreeSearch(root->left_child, key);
	return SortTreeSearch(root->right_child, key);
}
void mapping(BST *T)
//This function is to mapping node map
//这个函数的作用是将树节点的序号映射到一个数组中(编号)
{
	//queue<BST *> Que;
	Levelorder(T);
}
void Levelorder(BST *Tree) //层序遍历_队列实现
{
	//利用了一点点C++的东西 稍微看一下就知道了
	queue < BST *> q;
	if (Tree != NULL)
	{
		q.push(Tree);//Add root to queue
		Mapsequence[countMap++] = Tree->data;//给二叉树中的每一个节点编号
	}
	while (q.empty() == false)  //队列不为空判断
	{
		printf("%d → ", q.front()->data);
		if (q.front()->left_child != NULL)   //如果有左孩子,left_Child入队列
		{
			q.push(q.front()->left_child);
			Mapsequence[countMap++] = q.front()->left_child->data;
		}
		else Mapsequence[countMap++] = -520;//-520表示没有该编号位置没有左节点
		if (q.front()->right_child != NULL)   //如果有右孩子,right_Child入队列
		{
			q.push(q.front()->right_child);
			Mapsequence[countMap++] = q.front()->right_child->data;
		}
		else Mapsequence[countMap++] = -520;
		q.pop();  //已经遍历过的节点出队列
	}
}
void drawnode(int b, int g, int r)
{
	int i = 1;
	while (Mapsequence[i++] != 0)
	{
		if (Mapsequence[i - 1] == -520)continue;
		Posfunction(i - 1);
		circle(image, Point(pos.x, pos.y), 15, Scalar(b, g, r), -1);
		char s[3]; s[0] = (Mapsequence[i - 1]/10 + '0'); s[1] = (Mapsequence[i - 1] % 10 + '0');
		s[2] = '\0';
		putText(image,s, Point(pos.x, pos.y), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255), 2, 8);
		imshow("tree", image);
		waitKey(0);
		if (Mapsequence[2 * (i - 1)] != -520&& Mapsequence[2 * (i - 1)] != 0)
		{
			int x = pos.x; int y = pos.y;
			Posfunction(2 * (i - 1));
			line(image, Point(x, y), Point(pos.x, pos.y), Scalar(255, 255, 255), 2, CV_AA);
			imshow("tree", image);
			waitKey(0);
		}

		if (Mapsequence[2 * (i - 1)+1] != -520 && Mapsequence[2 * (i - 1)+1] != 0)
		{
			Posfunction(i - 1);
			int x = pos.x; int y = pos.y;
			Posfunction(2 * (i - 1)+1);
			line(image, Point(x, y), Point(pos.x, pos.y), Scalar(255, 255, 255), 2, CV_AA);
			imshow("tree", image);
			waitKey(0);
		}
		
	}
}
void Posfunction(int num)//5层以内二叉树画法
{
	if (num<1)
	{
		printf("enter num is illeage\n");
		exit(1);
	}
	else if (num==1)
	{
		pos.x = 500; pos.y = 50;
		return;
	}
	else if (num==2||num==3)
	{
		pos.y = 150;
		if (num == 2)
			pos.x = 300;
		else if (num == 3)
			pos.x = 700;
		return;
	}
	else if (num>=4&&num<=7)
	{		
		pos.y = 250;
		pos.x = 200 * (num - 3);
		return;
	}
	else if (num>=8&&num<=15)
	{
		pos.y = 350;
		pos.x = 150+100 * (num - 8);
		return;
	}
	else if (num>=16&&num<=31)
	{
		pos.y = 550;
		pos.x = 125+50*(num-16);
		return;
	}
	else
	{
		printf("此画图函数解决不了了!!!\n");
	}

}
发布了29 篇原创文章 · 获赞 14 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43343116/article/details/90641006