数据结构 动态查找与二叉排序树

Posted 上山打老虎D

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构 动态查找与二叉排序树相关的知识,希望对你有一定的参考价值。

1. DS二叉排序树之创建和插入

题目描述

给出一个数据序列,建立二叉排序树,并实现插入功能

对二叉排序树进行中序遍历,可以得到有序的数据序列

输入

第一行输入t,表示有t个数据序列

第二行输入n,表示首个序列包含n个数据

第三行输入n个数据,都是自然数且互不相同,数据之间用空格隔开

第四行输入m,表示要插入m个数据

从第五行起,输入m行,每行一个要插入的数据,都是自然数且和前面的数据不等

以此类推输入下一个示例

输出

第一行输出有序的数据序列,对二叉排序树进行中序遍历可以得到

从第二行起,输出插入第m个数据后的有序序列,输出m行

以此类推输出下一个示例的结果

输入样例

1
6
22 33 55 66 11 44
3
77
50
10

输出样例

11 22 33 44 55 66
11 22 33 44 55 66 77
11 22 33 44 50 55 66 77
10 11 22 33 44 50 55 66 77

参考代码

#include<iostream>
using namespace std;

struct Node
{
	int data;
	struct Node* leftTree;
	struct Node* rightTree;
};
class BSTree
{
private:
	Node* head = new Node();
	Node* p = new Node();
public:
	BSTree()
	{
		head->data = -1;
		head->leftTree = head->rightTree = NULL;
	}
	void Create();
	void Insert(Node* p,int e);
	void InOrderTraverse(Node* p);
};
void BSTree::Create()
{
	int t;
	int n,e;
	cin >> t;
	while (t--)
	{
		cin >> n;
		while (n--)
		{
			cin >> e;
			if (head->data == -1)head->data = e;
			else
			{
				Insert(head,e);
			}
		}
		InOrderTraverse(head);
		cout << endl;
		cin >> n;
		while (n--)
		{
			cin >> e;
			Insert(head, e);
			InOrderTraverse(head);
			cout << endl;
		}

	}
}
void BSTree::Insert(Node *p,int e)
{
	if (e > p->data&&p->rightTree==NULL) 
	{
		Node* s = new Node();
		s->data = e;
		s->leftTree = s->rightTree = NULL;
		p->rightTree = s;
	}
	else if (e < p->data && p->leftTree == NULL)
	{
		Node* s = new Node();
		s->data = e;
		s->leftTree = s->rightTree = NULL;
		p->leftTree = s;
	}
	else if (e > p->data && p->rightTree != NULL)
	{
		Insert(p->rightTree, e);
	}
	else if (e < p->data && p->leftTree != NULL)
	{
		Insert(p->leftTree, e);
	}
}
void BSTree::InOrderTraverse(Node *p)
{
	
	if(p->leftTree!=NULL)InOrderTraverse(p->leftTree);
	cout << p->data << " ";
	if(p->rightTree!=NULL)InOrderTraverse(p->rightTree);
}

int main()
{
	BSTree bst;
	bst.Create();
	return 0;
}

2. DS二叉排序树之查找

题目描述

给出一个数据序列,建立二叉排序树,并实现查找功能

对二叉排序树进行中序遍历,可以得到有序的数据序列

输入

第一行输入t,表示有t个数据序列

第二行输入n,表示首个序列包含n个数据

第三行输入n个数据,都是自然数且互不相同,数据之间用空格隔开

第四行输入m,表示要查找m个数据

从第五行起,输入m行,每行一个要查找的数据,都是自然数

以此类推输入下一个示例

输出

第一行输出有序的数据序列,对二叉排序树进行中序遍历可以得到

从第二行起,输出查找结果,如果查找成功输出查找次数,如果查找失败输出-1

以此类推输出下一个示例的结果

输入样例

1
6
22 33 55 66 11 44
7
11
22
33
44
55
66
77

输出样例

11 22 33 44 55 66
2
1
2
4
3
4
-1

参考代码

#include<iostream>
using namespace std;

struct Node
{
	int data;
	struct Node* leftTree;
	struct Node* rightTree;
};
class BSTree
{
private:
	Node* head = new Node();
	int count = 0;
	//Node* p = new Node();
public:
	BSTree()
	{
		head->data = -1;
		head->leftTree = head->rightTree = NULL;
	}
	void Create();
	void Insert(Node* p, int e);
	void InOrderTraverse(Node* p);
	int Check(Node *p,int e);
};
void BSTree::Create()
{
	int t;
	int n, e;
	cin >> t;
	while (t--)
	{
		cin >> n;
		while (n--)
		{
			cin >> e;
			if (head->data == -1)head->data = e;
			else
			{
				Insert(head, e);
			}
		}
		InOrderTraverse(head);
		cout << endl;
		cin >> n;
		while (n--)
		{
			cin >> e;
			count = 0;
			int flag=Check(head,e);
			if (flag==1)cout << count << endl;
			else cout <<"-1" <<endl;
		}

	}
}
void BSTree::Insert(Node* p, int e)
{
	if (e > p->data && p->rightTree == NULL)
	{
		Node* s = new Node();
		s->data = e;
		s->leftTree = s->rightTree = NULL;
		p->rightTree = s;
	}
	else if (e < p->data && p->leftTree == NULL)
	{
		Node* s = new Node();
		s->data = e;
		s->leftTree = s->rightTree = NULL;
		p->leftTree = s;
	}
	else if (e > p->data && p->rightTree != NULL)
	{
		Insert(p->rightTree, e);
	}
	else if (e < p->data && p->leftTree != NULL)
	{
		Insert(p->leftTree, e);
	}
}
int BSTree::Check(Node* p,int e)
{
	count++;
	if (e > p->data)
	{
		if (p->rightTree == NULL)return -1;
		Check(p->rightTree, e);
	}
	else if (e == p->data)return 1;
	else if (e < p->data)
	{
		if (p->leftTree == NULL)return -1;
		Check(p->leftTree, e);
	}
}
void BSTree::InOrderTraverse(Node* p)
{

	if (p->leftTree != NULL)InOrderTraverse(p->leftTree);
	cout << p->data << " ";
	if (p->rightTree != NULL)InOrderTraverse(p->rightTree);
}

int main()
{
	BSTree bst;
	bst.Create();
	return 0;
}

3. DS二叉排序树之删除

题目描述

给出一个数据序列,建立二叉排序树,并实现删除功能

对二叉排序树进行中序遍历,可以得到有序的数据序列

输入

第一行输入t,表示有t个数据序列

第二行输入n,表示首个序列包含n个数据

第三行输入n个数据,都是自然数且互不相同,数据之间用空格隔开

第四行输入m,表示要删除m个数据

从第五行起,输入m行,每行一个要删除的数据,都是自然数

以此类推输入下一个示例

输出

第一行输出有序的数据序列,对二叉排序树进行中序遍历可以得到

从第二行起,输出删除第m个数据后的有序序列,输出m行

以此类推输出下一个示例的结果

输入样例

1
6
22 33 55 66 11 44
3
66
22
77

输出样例

11 22 33 44 55 66
11 22 33 44 55
11 33 44 55
11 33 44 55

参考代码

#include<iostream>
using namespace std;

struct Node
{
	int data;
	struct Node* leftTree;
	struct Node* rightTree;
};
class BSTree
{
private:
	Node* head = new Node();
	Node* temp = new Node();
	Node* pre = new Node();
public:
	BSTree()
	{
		head->data = -1;
		head->leftTree = head->rightTree = NULL;
	}
	void Create();
	void Insert(Node* p, int e);
	void InOrderTraverse(Node* p);
	int Check(Node* p,int e);
	int Delete(Node* p, int e);
};
void BSTree::Create()
{
	int t;
	int n, e;
	cin >> t;
	while (t--)
	{
		cin >> n;
		while (n--)
		{
			cin >> e;
			if (head->data == -1)head->data = e;
			else
			{
				Insert(head, e);
			}
		}
		InOrderTraverse(head);
		cout << endl;
		cin >> n;
		while (n--)
		{
			cin >> e;
			int flag = Check(head, e);
			if(flag==1)Delete(temp, e);
			InOrderTraverse(head);
			cout << endl;
		}

	}
}
void BSTree::Insert(Node* p, int e)
{
	if (e > p->data && p->rightTree == NULL)
	{
		Node* s = new Node();
		s->data = e;
		s->leftTree = s->rightTree = NULL;
		p->rightTree = s;
	}
	else if (e < p->data && p->leftTree == NULL)
	{
		Node* s = new Node();
		s->data = e;
		s->leftTree = s->rightTree = NULL;
		p->leftTree = s;
	}
	else if (e > p->data && p->rightTree != NULL)
	{
		Insert(p->rightTree, e);
	}
	else if (e < p->data && p->leftTree != NULL)
	{
		Insert(p->leftTree, e);
	}
}
int BSTree::Check(Node* p, int e)
{
	if (e > p->data)
	{
		if (p->rightTree == NULL)return -1;
		if (p->rightTree->data == e)
		{
			if (p->rightTree->leftTree == NULL && p->rightTree->rightTree == NULL)pre = p;
		}
		Check(p->rightTree, e);
	}
	else if (e == p->data)
	{
		temp = p;
		return 1;
	}
	else if (e < p->data)
	{
		if (p->leftTree == NULL)return -1;
		if (p->leftTree->data == e)
		{
			if (p->leftTree->leftTree == NULL && p->leftTree->rightTree == NULL)pre = p;
		}
		Check(p->leftTree, e);
	}
}
int BSTree::Delete(Node* p, int e)
{
	
	if (!p->rightTree&&p->leftTree!=NULL)
	{
		Node* q = new Node();
		q = p;
		p = p->leftTree;
		delete q;
	}
	else if (!p->leftTree&&p->rightTree!=NULL)
	{
		Node* q = new Node();
		q = p;
		p = p->leftTree;
		delete q;
	}
	
	else if (p->leftTree==NULL && p->rightTree==NULL)
	{
		if (pre->leftTree->data == e)pre->leftTree = NULL;
		else pre->rightTree = NULL;
		Node* q = new Node();
		q = p;
		delete q;
	}
	else
	{
		Node* q = new Node();
		Node* s = new Node();
		q = p;
		s = p->leftTree;
		while (s->rightTree)
		{
			q = s;
			s = s->rightTree;
		}
		p->data = s->data;
		if (q != p)q->rightTree = s->leftTree;
		else q->leftTree = s->leftTree;
		delete s;
	}
	return 0;
}
void BSTree::InOrderTraverse(Node* p)
{

	if (p->leftTree != NULL)InOrderTraverse(p->leftTree);
	cout << p->data << " ";
	if (p->rightTree != NULL)InOrderTraverse(p->rightTree);
}

int main()
{
	BSTree bst;
	bst.Create();
	return 0;
}

4. DS查找—二叉树平衡因子

题目描述

二叉树用数组存储,将二叉树的结点数据依次自上而下,自左至右存储到数组中,一般二叉树与完全二叉树对比,比完全二叉树缺少的结点在数组中用0来表示。

计算二叉树每个结点的平衡因子,并按后序遍历的顺序输出结点的平衡因子。

–程序要求–
若使用C++只能include一个头文件iostream;若使用C语言只能include一个头文件stdio
程序中若include多过一个头文件,不看代码,作0分处理
不允许使用第三方对象或函数实现本题的要求

输入

测试次数t

每组测试数据一行,数组元素个数n,后跟n个字符,二叉树的数组存储。

输出

对每组测试数据,按后序遍历的顺序输出树中结点的平衡因子(测试数据没有空树)

输入样例

2
6 ABC00D
24 ABCD0EF0000H00000000000I

输出样例

B 0
D 0
C 1
A -1
D 0
B 1
I 0
H 1
E 2
F 0
C 2
A -2

参考代码

#include<iostream>
using namespace std;

int getheight查找与二叉树

数据结构之树篇2——二叉排序(查找,搜索)树

数据结构(二叉排序树)

数据结构(二叉排序树)

二叉树BT与二叉查找树BST概览

数据结构(B树)