二叉树搜索树详解

本文深入探讨了二叉搜索树的定义、性质及其操作方法,包括构建、遍历(先序、中序、后续)、层次遍历等,并提供了C++实现代码。同时,介绍了二叉搜索树的时间复杂度及如何判断是否为满二叉树。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二叉搜索树的特点

二叉搜索树:二叉树的每一个结点最多有两个子数的树结构,也就是说除了叶子结点,其余结点最多有两个子节点

二叉搜索树的性质

  1. 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
  2. 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
  3. 它的左、右子树也分别为二叉搜索树

二叉搜索树的时间复杂度

增:循环遍历每个节点,比较各节点的值,直到找到相应位置,时间复杂度为log2n,2为底数。

查:循环遍历每个节点,比较各节点的值,直到找到相应位置,时间复杂度为log2n。

改:循环遍历每个节点,比较各节点的值,直到找到相应位置,将此节点数据值改为相应值,时间复杂度为log2n。

删:循环遍历每个节点,比较各节点的值,直到找到相应位置,将左子树最右节点或右子树最左节点移动至被删除节点处,且被移动节点的左或右节点移至被移动节点的位置,时间复杂度为log2n

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<stack>
#include<queue>
using namespace std;


//创建结点
typedef struct Node{
	int data;
	Node *right;
	Node *left;
}Node;
//创建树
typedef struct Tree
{
	Node* root;
}Tree;

//购买结点
Node * buyNode(int val, Node *left, Node *right)
{
	Node *p = (Node*)malloc(sizeof(Node));
	p->data = val;
	p->left = left;
	p->right = right;

	return p;
}

//构建二叉树
void create(Tree *tree,int val)
{
	if (tree->root == NULL)
	{
		tree->root = buyNode(val, NULL, NULL);
	}
	else
	{
		Node *p = tree->root;
		while (p != NULL)
		{
			if (p->data > val)
			{
				if (p->left == NULL)
				{
					p->left = buyNode(val, NULL, NULL);
					return;
				}
				else
				{
					p = p->left;
				}

			}
			else if (p->data <= val)
			{
				if (p->right == NULL)
				{
					p->right = buyNode(val, NULL, NULL);
					return;
				}
				else
				{
					p = p->right;
				}
			}
		}
	}
	
}
//非递归求树的最大值  
int getmaxnum(Node *head)
{
	if (head == NULL)
	{
		return -1;
	}
	else
	{
		Node *p = head;
		while (p->right != NULL)
		{
			p = p->right;
		}

		return p->data;
	}
}

//递归求树的最大值
int getmaxnum2(Node *head)
{
	if (head == NULL)
	{
		return -1;
	}
	else
	{
		int max1 = getmaxnum2(head->left);
		int max2 = getmaxnum2(head->right);
		int max3 = head->data;
		int res = max(max1, max(max2, max3));

		return res;
	}
}


//递归求树的高度
int gethight(Node *head)
{
	if (head == NULL)
	{
		return 0;
	}
	else
	{
		int h1 = gethight(head->left);
		int h2 = gethight(head->right);
		int res = max(h1, h2) + 1;

		return res;
	}

	
}

//先序遍历
void preorder(Node *head)
{
	if (head == NULL)
	{
		return;
	}
	else
	{
		printf("%d ", head->data);
		preorder(head->left);
		preorder(head->right);
	}
}

//中序遍历
void inorder(Node *head)
{
	if (head == NULL)
	{
		return;
	}
	else
	{
		inorder(head->left);
		printf("%d ", head->data);
		inorder(head->right);
	}
}

//后续遍历
void pasorder(Node *head)
{
	if (head == NULL)
	{
		return;
	}
	else
	{
		pasorder(head->left);
		pasorder(head->right);
		printf("%d ", head->data);
	}
}

//非递归先序遍历
void NicePreorder(Node *head)
{
	stack<Node* > s;
	if (head == NULL)
	{
		return;
	}
	Node *p = head;
	while (!s.empty() || p!= NULL)
	{
		while (p != NULL)
		{
			cout << p->data << " ";
			s.push(p);
			p = p->left;
		}
		if (!s.empty())
		{
			p = s.top();
			s.pop();
			p = p->right;
		}
	}
}
//非递归中序遍历
void NiceInorder(Node * head)
{
	if (head == NULL)
	{
		return;
	}
	stack<Node *> s;
	
	Node *p = head;
	while (!s.empty() || p!= NULL)
	{
		while (p!= NULL)
		{
			s.push(p);
			p = p->left;
		}
		if(!s.empty())
		{
			p = s.top();
			s.pop();
			cout << p->data << " ";
			p = p->right;
		}
	}
}

//非递归后续遍历
void NicePasOrder(Node *head)
{
	if (head == NULL)
	{
		return;
	}
	stack<Node *> s;
	Node*p = head;
	Node * lastp = NULL;
	
	while (p != NULL)
	{
		s.push(p);
		p = p->left;
	}
	while (!s.empty())
	{
		p = s.top();
		s.pop();
		if (p->right == NULL || p->right == lastp)
		{
			cout << p->data << " ";
			lastp = p;
		}
		else
		{
			s.push(p);
			p = p->right;
			while (p != NULL)
			{
				s.push(p);
				p = p->left;
			}
		}
	}
		
	
}

//第K层 层次遍历
void Print_Level_K(Node *head, int k)
{
	if (head != NULL && k == 0)
	{
		cout << head->data << " ";
	}
	else if (head != NULL)
	{
		Print_Level_K(head->left, k - 1);
		Print_Level_K(head->right, k - 1);
	}
}

//非递归层次遍历
void Nice_Print_level(Node *head)
{
	queue<Node *> q;
	if (head != NULL)
	{
		q.push(head);
	}
	while (!q.empty())
	{
		Node *p = q.front();
		q.pop();
		cout << p->data << " ";
		if (p->left != NULL)
		{
			q.push(p->left);
		}
		if (p->right != NULL)
		{
			q.push(p->right);
		}
	}
}

//判断一颗二叉树是不是满二叉树
bool Is_Full_Tree(Node * head)
{
	if (head == NULL)
	{
		return true;
	}
	else
	{
		return( Is_Full_Tree(head->left) && Is_Full_Tree(head->right) && gethight(head->left) == gethight(head->right));
	}

	return false;
}
int main()
{

	int arr[] = { 6, 3, 8, 2, 5, 1, 7 };

	Tree tree;
	tree.root = NULL;

	for (int i = 0; i < sizeof(arr) / sizeof(int); i++)
	{
		create(&tree, arr[i]);
	}

	//preorder(tree.root);
	//NicePreorder(tree.root);
	
	//Print_Level_K(tree.root, 2);
	//Nice_Print_level(tree.root);
	//inorder(tree.root);
	//NiceInorder(tree.root);
	pasorder(tree.root);
        cout<<endl;
	NicePasOrder(tree.root);
	
	//cout << Is_Full_Tree(tree.root) << endl;
	//printf("树的高度 = %d\n", gethight(tree.root));
	//printf("树的最大值 = %d\n", getmaxnum(tree.root));
	//printf("树的最大值 = %d\n", getmaxnum2(tree.root));
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值