二叉树/二叉搜索树的操作汇集(c++)

来源:互联网 发布:零基础学钢琴 知乎 编辑:程序博客网 时间:2024/05/21 07:09
//还包含二叉查找树的一些算法
#include<stdio.h>
#include<iostream>
#include<vector>
using namespace std;

struct binaryNode
{
    int value;
    binaryNode *right;
    binaryNode *left;
    binaryNode *parent;
};
int treeheight(binaryNode *root);
void priOrder(binaryNode *root)//前序
{
    if(NULL==root)
        return;
    else
    {
    cout<<root->value;
    priOrder(root->left);
    priOrder(root->right);
    }
}
void MidOrder(binaryNode *root)//中序
{
    if(NULL==root)
        return;
    else
    {
    
    priOrder(root->left);
    cout<<root->value;
    priOrder(root->right);
    }
}
void printNodeByLevel(binaryNode * root,int level)//按层打印递归写法
{
    if(root==NULL||level<0)
    {return;}
    if(level==0)
        cout<<root->value<<endl;
    printNodeByLevel(root->left,level-1);
    printNodeByLevel(root->right,level-1);
}
void printLevel(binaryNode *root)
{
    for(int i=0;i<treeheight(root)+1;i++)
    {
        printNodeByLevel(root,i);
        cout<<endl;
    }
}
int treeheight(binaryNode *root)
{
    if(root==NULL)
        return 0;
    if(root->left==NULL&&root->right==NULL)//为叶子节点时高度为0
            return 0;
    return max(treeheight(root->left)+1,treeheight(root->right)+1);
}
void printCengCiNoRecursive(binaryNode *root)//类似于搜索引擎的用队列实现广度优先遍历
{    if(root==NULL)
        return ;
    vector<binaryNode*> vec;
    vec.push_back(root);
    int curentLevel=0,lastPosition=1;
    while(curentLevel<vec.size())
    {   lastPosition=vec.size();
        while(curentLevel<lastPosition)
        {
            
        cout<<vec[curentLevel]->value<<" ";
        if(NULL!=vec[curentLevel]->left)
            vec.push_back(vec[curentLevel]->left);
        if(NULL!=vec[curentLevel]->right)
            vec.push_back(vec[curentLevel]->right);
        curentLevel++;
        }
        cout<<endl;
    }
    
}
void swapreference(binaryNode **p,binaryNode **q)//这两**的理解要注意
{
    binaryNode *temp;
    temp=*p;
    *p=*q;
    *q=temp;
}
void mirrorRotate(binaryNode * root)//镜像
{
    if(root==NULL)
        return;
    swapreference(&root->left,&root->right);
    mirrorRotate(root->left);
    mirrorRotate(root->right);
}
void prpath(int path[],int level)
{
    for(int i=0;i<level;i++)
        cout<<path[i]<<" ";
}
void printNodepath(int path[],int sum,binaryNode * root,int level)//输入数字看是否有相应的路径值的和是它
{
    if(root==NULL)
        return;
    
    //path=(int*)malloc(sizeof(int)*10);
    path[level++]=root->value;
    sum-=root->value;
    if(root->left==NULL&&root->right==NULL)
    {
    if(sum==0)
        prpath(path,level);
    }
    else
    {
    //*path=root->value;
        if(root->left!=NULL)//注意边界条件
    printNodepath(path,sum,root->left,level);
            if(root->right!=NULL)
    printNodepath(path,sum,root->right,level);
    }
    sum+=root->value;
    level--;
}
binaryNode*  findtheMinValueOfSearchTree(binaryNode *root )//找到干叉查找树的最小值
{
    //if(NULL!=root)
        //return findtheMinValueOfSearchTree(root->left);
    //else return
    while(root->left!=NULL)
    {
        root=root->left;
    }
    return root;
}
binaryNode*  findtheMaxValueOfSearchTree(binaryNode *root )//找到干叉查找树的最大值
{
    //if(NULL!=root)
        //return findtheMinValueOfSearchTree(root->left);
    //else return
    while(root->right!=NULL)
    {
        root=root->right;
    }
    return root;
}
//查找二叉查找树中节点x的前驱节点,返回指向该节点的指针
//在查找过程中,如果节点x左子树不为空,那么返回左子树的最大节点即可
//如果节点x的左子树为空,那么前驱节点为x的某一个祖先节点的父节点,而且该祖先节点是作为其父节点的右儿子
binaryNode* tree_predecessor(binaryNode *x)
{
 if (x->left != NULL)
  return findtheMaxValueOfSearchTree(x->left);

 binaryNode *y = x->parent;
 while (y != NULL && x == y->left)
 {
  x = y;
  y = y->parent;
 }
 return y;
}
//查找二叉查找树中节点x的后继节点,返回指向该节点的指针
//在查找过程中,如果节点x右子树不为空,那么返回右子树的最小节点即可
//如果节点x的右子树为空,那么后继节点为x的某一个祖先节点的父节点,而且该祖先节点是作为其父节点的左儿子
binaryNode * findTreetree_successor(binaryNode *target)
{
    if(target->right!=NULL)
        return findtheMinValueOfSearchTree(target->right);
    binaryNode *y=target->parent;
    while(y!=NULL&&target==y->right)
    {
        target=y;
        y=y->parent;
    }
    return y;
}
void insertSearchTree(binaryNode *root,binaryNode *insert)//向二叉查找树中插入
{
    binaryNode *y=NULL;
    y=root;
    while(root!=NULL)
    {
        y=root;
        if(root->value<insert->value)
            root=root->right;
        else
            root=root->left;
    };
    insert->parent=y;
    if(y==NULL)
        root=insert;
    else
    {
        if(y->value<insert->value)
            y->right=insert;
        else
            y->left=insert;
    }
}
//1:如果z没有子女,则删除z,修改其父节点p[z],使null为其子女,2:如果z只有一个子女,则可以通过在其子结点与父节点间建立一条连来删除z
//3:如果z有两个子女,则先删除z的后继y,再用y的内容来替代z的内容;
binaryNode *deleteNodeFromSearchTree(binaryNode *root,binaryNode *z)//从二叉查找树的删除节点z为要删除的节点
{
    binaryNode *y,*x;//y为后继节点或待删节点,x为后继的子节点
    if(z->left==NULL||z->right==NULL)//确实要删除的节点,它可能是z也可能是后继y
        y=z;
    else
        y=findTreetree_successor(z);
    if(y->left!=NULL)//x被置为y的非空子女或空
        x=y->left;
    else
        x=y->right;
    if(x!=NULL)//通过修改p[y]或x中的指针将y删除
        x->parent=y->parent;
    if(y->parent==NULL)
        root=x;
    else if(y==y->parent->left)
        y->parent->left=x;
    else
        y->parent->right=x;
    if(y!=z)//如果要删除的是z的后继,则复制内容过去
        z->value=y->value;
    return y;
}
/*
int main()
{
    binaryNode *a=new binaryNode();
    a->parent=NULL;a->value=3;a->left=new binaryNode();a->left->parent=a;a->left->value=2;a->right=new binaryNode();a->right->value=5;a->right->parent=a;//要用new 初始化,不然没空间给它会报错
    a->left->left=new binaryNode();a->left->left->value=1;a->left->left->parent=a->left;
    a->left->left->left=NULL;//a->left->left->left->parent=a->left->left;
    a->left->left->right=NULL;//a->left->left->right->parent=a->left->left;
    a->left->right=NULL;
    a->right->left=NULL;a->right->right=NULL;
    //priOrder(a);
    //printNodeByLevel(a,2);
    //cout<<treeheight(a);
    //printLevel(a);
    //mirrorRotate(a);
    //printCengCiNoRecursive(a);
    //cout<<sizeof(int);//sizeof返回的是字节数,Int 是4
    int b[10]={0};
    //printNodepath(b,7,a,0);
    //binaryNode *h=findtheMinValueOfSearchTree(a);
    //binaryNode *h=findTreetree_successor(a->left);
    //binaryNode *insert=new binaryNode();//插入测试
    //insert->value=7;
    //insert->left=NULL;insert->right=NULL;
    //insertSearchTree(a,insert);
    deleteNodeFromSearchTree(a,a->left);
    MidOrder(a);
    //cout<<h->value;
    system("pause");
    return 0;
}*/
原创粉丝点击