• 搜索二叉树


    #include<stdio.h>
    
    struct Node
    {
        int key;
        Node *p;
        Node *left;
        Node *right;
        Node(int k = 0)
        {
            p=NULL;
            left=NULL;
            right=NULL;
            key=k;
        }
    private:
        Node& operator=(const Node&);
    }*NIL;
    
    struct Tree
    {
        Node *root;
        Tree()
        {
            root = NIL;
        }
    };
    //求最小值
    Node* Minimum(Node *root)
    {
        while(root->left != NIL)
        {
            root = root->left;
        }
        return root;
    }
    //中序遍历
    void InorderTreeWalk(const Node *root)
    {
        if(root != NIL)
        {
            InorderTreeWalk(root->left);
            printf("%d ",root->key);
            InorderTreeWalk(root->right);
        }
    }
    //查询
    Node* TreeSearch(const Tree *T,const int k)
    {
        Node *x=T->root;
        while(x != NIL && x->key!=k)
        {
            if(x->key>k)
            {
                x = x->left;
            }
            else
            {
                x = x->right;
            }
        }
        return x;
    }
    //插入
    void TreeInsert(Tree *T, Node *z)
    {
        Node *y = NIL;
        Node *x = T->root;
        while (x != NIL)
        {
            y=x;
            if(z->key < x->key)
            {
                x = x->left;
            }
            else
            {
                x = x->right;
            }
        }
        z->p = y;
        z->left = NIL;
        z->right = NIL;
        if(y == NIL)
        {
            T->root = z;
        }
        else
        {
            if(z->key < y->key)
            {
                y->left = z;
            }
            else
            {
                y->right = z;
            }
        }
    }
    //交换子树
    void Transplant(Tree *T,Node *u,Node* v)
    {
        if(u->p == NIL)
        {
            T->root = v;        
        }
        else
        {
            if(u == u->p->left)
            {
                u->p->left = v;
            }
            else
            {
                u->p->right = v;
            }
        }
        if(v != NIL)
        {
            v->p = u->p;
        }
    }
    //删除
    void TreeDelete(Tree *T, Node *z)
    {
        if(z->left == NIL)
        {
            Transplant(T,z,z->right);
        }
        else if(z->right == NIL)
        {
            Transplant(T,z,z->left);
        }
        else
        {
            Node *y = Minimum(z->right);
            if(y!=z->right)
            {
                Transplant(T,y,y->right);
                y->right = z->right;
                z->right->p = y;
            }
            Transplant(T,z,y);
            y->left = z->left;
            z->left->p = y;
        }
    }
    
    int main()
    {
        Tree T;
        for(int i=10;i>9;i--)
        {
            TreeInsert(&T,new Node(i));
        }
        InorderTreeWalk(T.root);
        printf("
    ");
        Node *temp;
        Node Temp;
        if((temp = TreeSearch(&T,5)) != NIL)
        {
            printf("find 5
    ");
            TreeDelete(&T,temp);
            printf("Delete 5
    ");
        }
        InorderTreeWalk(T.root);
        printf("
    ");
        /*
        if(TreeSearch(&T,0) == NIL)
        {
            printf("do not find 0
    ");
        }
        */
        return 0;
    }
  • 相关阅读:
    任务管理器程序——让任务更好的完成
    任务管理器——让你的任务更好的完成 助力中考
    Re:萌娘百科上的黑幕实现
    404 页面不存在
    JavaScript中,数组和对象的遍历方法总结
    JavaScript Math方法的基本使用
    html css二级导航栏
    css常用元素通用样式表
    web前端sprite,精灵图,雪碧图
    个人总结
  • 原文地址:https://www.cnblogs.com/johnsblog/p/3898267.html
Copyright © 2020-2023  润新知