C++#include<bits/stdc++.h>
using namespace std;
struct trees{
int data;
int high;
trees* left;
trees* right;
};
//构建二叉树
void insert(trees** t,int val){
if(*t == NULL){
trees* node = new trees;
node->data=val;
node->left=NULL;
node->right=NULL;
*t=node;
}else {
if((*t)->data > val){
insert(&((*t)->left),val);
}else {
insert(&((*t)->right),val);
}
}
}
//递归搜索
trees* search(trees* t,int val){
if(t == NULL)return NULL;
if(t->data > val)return search(t->left,val);
else if(t->data > val)return search(t->right,val);
else if(t->data == val)return t;
return NULL;
}
//非递归搜索
trees* searchh(trees* t,int val,int *cont){
trees* cur = t;
while(cur!=NULL){
if(cur->data < val){
cur=cur->right;
(*cont)++;
}else if(cur->data > val){
cur=cur->left;
(*cont)++;
}else if(cur->data == val){
return cur;
}
}
return NULL;
}
//中序遍历
void midtree(trees* t){
if(t == NULL){
return;
}else {
midtree(t->left);
cout<<t->data<<' ';
midtree(t->right);
}
}
//先序遍历
void pretree(trees* t){
if(t == NULL){
return;
}else {
cout<<t->data<<' ';
pretree(t->left);
pretree(t->right);
}
}
//后序遍历
void posttree(trees* t){
if(t == NULL){
return;
}else {
posttree(t->left);
posttree(t->right);
cout<<t->data<<' ';
}
}
//删除节点
trees* deletenode(trees* t,int val){
if(t == NULL)return NULL;
if(t->data > val){
//递归左子树
t->left = deletenode(t->left,val);
return t;
}else if(t->data < val){
//递归右子树
t->right = deletenode(t->right,val);
return t;
}else if(t->data == val){
//1.空
if(t->left == NULL&&t->right == NULL){
trees* temp = t;
delete t;
temp=NULL;
return temp;
}else if(t->left != NULL&&t->right == NULL){
//有右子树
trees* temp=t->left;
delete t;
return temp;
}else if(t->left == NULL&&t->right != NULL){
//有左子树
trees* temp=t->left;
delete t;
return temp;
}else if(t->left != NULL&&t->right != NULL){
//右、左子树都有
trees* cur=t->right;
while(cur->left != NULL){
cur = cur->left;
}
cur->left = t->left;
trees* temp=t->right;
delete t;
return temp;
}
}
return t;
}
trees* newnode(int val){
trees* node = (trees*)malloc(sizeof(trees));
node->data=val;
node->high=1;
node->left=NULL;
node->right=NULL;
return node;
}
//获得树的高度
int gethigh(trees* node){
if(node == NULL){
return 0;
}
return node->high;
}
//左旋函数
trees* leftrotate(trees* root){
trees* newroot = root->right;
trees* t2 = newroot->left;
newroot->left = root;
root->right = t2;
root->high=1+max(gethigh(root->left),gethigh(root->right));
newroot->high=1+max(gethigh(newroot->left),gethigh(newroot->right));
return newroot;
}
trees* rightrotate(trees* root){
trees* newroot = root->left;
trees* t2 = newroot->right;
newroot->right = root;
root->left = t2;
root->right = t2;
root->high=1+max(gethigh(root->left),gethigh(root->right));
newroot->high=1+max(gethigh(newroot->left),gethigh(newroot->right));
return newroot;
}
//获取平衡因子
int getbalance(trees* node){
return gethigh(node->left) - gethigh(node->right);
}
//插入节点
trees* insertnode(trees* node,int key){
if(node == NULL)return newnode(key);
if(key < node->data)node->left = insertnode(node->left,key);
else if(key > node ->data)node->right=insertnode(node->right,key);
else return node;
node->high=1+max(gethigh(node->left),gethigh(node->right));
//平衡因子
int balance = getbalance(node);
//LL失衡
if(balance>1&&getbalance(node->left)>0)return rightrotate(node);
//LR失衡
if(balance>1&&getbalance(node->left)<0){
node->left=leftrotate(node->left);
return rightrotate(node);
}
//RR失衡
if(balance<-1&&getbalance(node->right)<0)return leftrotate(node);
//RL失衡
if(balance<-1&&getbalance(node->right)>0){
node->right=rightrotate(node->right);
return leftrotate(node);
}
return node;
}
int main(){
return 0;
}