1.二叉搜索树的基本操作——自己准备校招用的——不喜勿喷哦本人将,重复的节点,只保存了数量。但是输出的时候却没有输出所有的节点,想要输出所有的节点,自己加个for循环即可哦
#include <iostream>
#include <queue>
using namespace std;
struct jian{
int key;
int num;
jian *lc;
jian *rc;
};
queue<jian*> q;
queue<jian*> p;
/**
* 创建树
*/
jian *create(jian *root,int x){
if(root==NULL){
root=new jian;
root->key=x;
root->num=1;
root->lc=NULL;
root->rc=NULL;
}else{
if(root->key==x){
root->num++;
}else if(root->key<x){
root->rc=create(root->rc,x);
}else{
root->lc=create(root->lc,x);
}
}
return root;
}
/**
* 前序遍历
*/
void xianxu(jian *root)
{
if(root!=NULL){
cout<<root->key<<" ";
xianxu(root->lc);
xianxu(root->rc);
}
}
/**
* 中序遍历
*/
void zhongxu(jian *root)
{
if(root!=NULL){
zhongxu(root->lc);
cout<<root->key<<" ";
zhongxu(root->rc);
}
}
/**
* 后序遍历
*/
void houxu(jian *root)
{
if(root!=NULL){
houxu(root->lc);
houxu(root->rc);
cout<<root->key<<" ";
}
}
/**
* 层次遍历,两个队列,一个是按照层次取值,找出左右子树,放入队列,删掉队列首,
* 一个是,存放取出的节点
*/
void cengcibianli(jian *root){
if(q.empty()){
q.push(root);
}
while(!q.empty()){
jian *tree=q.front();
if(tree->lc!=NULL){
q.push(tree->lc);
}
if(tree->rc!=NULL){
q.push(tree->rc);
}
p.push(tree);
q.pop();
}
}
/**
* 在二叉搜索树中,最大的值一定是在左子树中的
* 当节点的左子树为空的话,那么这个点就是最小值的节点,自己画个图看看
*/
jian *findMin(jian *root){
if(root->lc==NULL)
{
cout<<root->key<<endl;
return root;
}
if(root->lc!=NULL){
findMin(root->lc);
}
}
/**
*在二叉搜索树中,最大的值一定是在右子树中的,
*当结节的右子树为空的话,那么这个节点就是最大值的节点
*/
jian *findMax(jian *root){
if(root->rc==NULL)
{
cout<<"所在的树的最大值是:"<<root->key<<endl;
return root;
}
if(root->rc!=NULL){
findMax(root->rc);
}
}
/**
* 删除最小的那个值
*/
jian *removeMin(jian *root){
if(root==NULL){
return NULL;
}
if(root->lc==NULL){
/**
* 如果左子树是空的那么,返回右子树的地址,纵然是null也无所谓
*/
jian* rightRc=root->rc;
//delete root;
return rightRc;//返回的地址,被上一个节点连接
}
root->lc=removeMin(root->lc);
return root;
}
jian *removeMax(jian *root){
if(root==NULL){
return NULL;
}
//删除最大的,肯定是在右子树旁边找,删除某一个最大值,只要再把子树移到上边就行
//树的结构不会发生变化
//当key所在的节点没有右孩子的时候,那么就可以,将左孩子一上去,
//递归函数的返回值是非常重要的
//删除最小值同理
if(root->rc==NULL){
jian *leftLc=root->lc;
//delete root;
return leftLc;
}
root->rc=removeMax(root->rc);
return root;
}
jian* removeKey(jian *root,int key)
{
if(root==NULL){
cout<<"不存在该节点"<<endl;
return NULL;
}
/**
* 先找出对应key的节点的地址
*/
if(root->key>key){
root->lc=removeKey(root->lc,key);
return root;
}else if(root->key<key){
root->rc=removeKey(root->rc,key);
return root;
}else{
//key==root->key
/**
* 找到节点对应的地址
* 1.如果左子树为空的树,说明是个最小值,删除节点,将子树移上去结构不变
*/
if(root->lc==NULL){
jian *rightRc=root->rc;
delete root;
return rightRc;
}
/**
* 找到节点对应的地址
* 1.如果右子树为空的树,说明是个最大值,删除节点,将子树移上去结构不变
*/
if(root->rc==NULL){
jian *leftLc=root->lc;
delete root;
return leftLc;
}
//左右子树都不为空的时候,
/**
* 左右子树都不为空的时候
* 1.以当前节点的右子树的节点值为根,寻找右子树的最小值,作为替换,key所对应的节点
* 2.返回的地址的右子树连接key对应节点的右子树删除最小值的新树
* 3.左子树不变
* 4其实寻找替换节点还可以用左子树的对应的最大值
*/
jian *tree=findMin(root->rc);//寻找右子树中最小的值,当节点
tree->rc=removeMin(root->rc);//
tree->lc=root->lc;
return tree;
}
}
int main()
{
int i;
//int a[5]={5,2,4,8,6};
int a[7]={10,7,4,9,12,13,11};
jian *root;
root=NULL;
for(i=0;i<7;i++){
root=create(root,a[i]);
}
//xianxu
cout<<"前序遍历"<<endl;
xianxu(root);
cout<<endl;
cout<<"中序遍历,其实就是从小到大排序!"<<endl;
zhongxu(root);
cout<<endl;
cout<<"后序遍历"<<endl;
houxu(root);
cout<<endl;
cout<<"层次遍历"<<endl;
cengcibianli(root);
while(!p.empty()){
jian *tree=p.front();
cout<<p.front()->key<<" ";
p.pop();
}
cout<<endl;
cout<<"删除最小值"<<endl;
/*
* 最小值是左子树中,最左边的值
*/
findMin(root);
cout<<"删除最大值"<<endl;
/**
*最大值是右子树中最右边的值
*/
findMax(root);
cout<<"删除二叉搜索树中的某个节点"<<endl;
cout<<"先查找某一个节点"<<endl;
//jian* tree;
//tree=searchValue(2);
cout<<"zainane"<<endl;
int searchKey=12;
root=removeKey(root,searchKey);
cengcibianli(root);
while(!p.empty()){
jian *tree=p.front();
cout<<p.front()->key<<" ";
p.pop();
}
}
/*queue<jian*> q;
queue<jian*> p;
jian* create(jian *root,int x){
if(root==NULL){
//这里创建的二叉搜索树,重复元素,用个数代替
root=new jian;
root->key=x;
root->lc=NULL;
root->rc=NULL;
root=new jian;
root->key=x;
root->num=1;
root->lc=NULL;
root->rc=NULL;
}
else
{
if(x>root->key)
{
root->rc=create(root->rc,x);
}
else
{
root->lc=create(root->lc,x);
}
if(x>root->key){
root->rc=create(root->rc,x);
}else if(x<root->key){
root->lc=create(root->lc,x);
}else{
root->num++;
}
}
return root;
}
void bianli(jian *root){
if(q.empty()){
q.push(root);
}
jian *tree=q.front();
if(tree->lc!=NULL){
q.push(tree->lc);
}
if(tree->rc!=NULL){
q.push(tree->rc);
}
p.push(tree);
q.pop();
}
int main()
{
jian* root;
int x,i;
int a[5]={5,4,6,2,8};
for(i=0;i<5;i++)
{
root=create(root,a[i]);
}
//bfs层次遍历一下
// bianli(root);
while(!p.empty()){
jian *tree=p.front();
cout<<p.front()->key<<" "<<endl;
p.pop();
}
}*/
212

被折叠的 条评论
为什么被折叠?



