二叉搜索树的特点
二叉搜索树:二叉树的每一个结点最多有两个子数的树结构,也就是说除了叶子结点,其余结点最多有两个子节点
二叉搜索树的性质
- 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
- 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
- 它的左、右子树也分别为二叉搜索树
二叉搜索树的时间复杂度
增:循环遍历每个节点,比较各节点的值,直到找到相应位置,时间复杂度为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;
}