• careercup-树与图 4.6


    4.6 设计一个算法,找出二叉查找树中指定结点的“下一个”结点(也即中序后继)。可以假定每个结点都含有指向父节点的连接。

    思路:

    有两种情况:1)如果该结点存在右子树,则中序后继即为右子树中最小的结点。

          2)如果该结点不存在右子树,则后继结点为使得所给结点在其祖先结点的左子树上的第一个祖先结点,因此一直找父节点,知道找到一个父节点使得该结点在左子树上。

    C++实现代码:

    #include<iostream>
    #include<new>
    using namespace std;
    
    struct BinarySearchTree
    {
        int elem;
        BinarySearchTree *parent;
        BinarySearchTree *left;
        BinarySearchTree *right;
        BinarySearchTree(int x):elem(x),parent(NULL),left(NULL),right(NULL) {}
    };
    
    void insert(BinarySearchTree *&root,int z)
    {
        BinarySearchTree *y=new BinarySearchTree(z);
        if(root==NULL)
        {
            root=y;
            return;
        }
        else if(root->left==NULL&&z<root->elem)
        {
            root->left=y;
            y->parent=root;
            return;
        }
        else if(root->right==NULL&&z>root->elem)
        {
            root->right=y;
            y->parent=root;
            return;
        }
        if(z<root->elem)
            insert(root->left,z);
        else
            insert(root->right,z);
    }
    
    void createBST(BinarySearchTree *&root)
    {
        int arr[10]= {29,4,6,1,8,3,0,78,23,89};
        for(auto a:arr)
            insert(root,a);
    }
    
    void inorder(BinarySearchTree *root)
    {
        if(root)
        {
            inorder(root->left);
            cout<<root->elem<<" ";
            inorder(root->right);
        }
    }
    
    BinarySearchTree* findMin(BinarySearchTree *root)
    {
        if(root==NULL||!root->left)
            return root;
        while(root->left)
        {
            root=root->left;
        }
        return root;
    }
    
    BinarySearchTree* findMax(BinarySearchTree *root)
    {
        if(root==NULL||!root->right)
            return root;
        while(root->right)
        {
            root=root->right;
        }
        return root;
    }
    
    BinarySearchTree* findProcessor(BinarySearchTree *root,BinarySearchTree* x)
    {
        if(x->left)
            return findMax(x->left);
        BinarySearchTree *y=x->parent;
        while(y&&y->left==x)
        {
            x=y;
            y=x->parent;
        }
        return y;
    }
    
    BinarySearchTree* findSuccessor(BinarySearchTree *x)
    {
        if(x->right)
            return findMin(x->right);
        BinarySearchTree *y=x->parent;
        while(y&&y->right==x)
        {
            x=y;
            y=x->parent;
        }
        return y;
    }
    
    int main()
    {
        BinarySearchTree *root=NULL;
        createBST(root);
        inorder(root);
    }
  • 相关阅读:
    Codeforces Round #271 (Div. 2) F. Ant colony 线段树
    poj 1744 tree 树分治
    HDU Shell Necklace CDQ分治+FFT
    BZOJ 1567: [JSOI2008]Blue Mary的战役地图 矩阵二维hash
    BZOJ 1042: [HAOI2008]硬币购物 容斥+背包
    HDU 6078 Wavel Sequence 树状数组优化DP
    Gym
    HDU 6058 Kanade's sum 二分,链表
    HDU 6061 RXD and functions NTT
    ZOJ 3233 Lucky Number 容斥原理
  • 原文地址:https://www.cnblogs.com/wuchanming/p/4148139.html
Copyright © 2020-2023  润新知