• [1]输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表


    输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。
    要求不能创建任何新的结点,只调整指针的指向。
              10
            /    
           6     14
          /     /  
         4  8 12  16
    转换成双向链表
    4=6=8=10=12=14=16

    解:

    二元查找树: 它首先要是一棵二元树,在这基础上它或者是一棵空树;或者是具有下列性质的二元树:

    (1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;

    (2)若右子树不空,则右子树上所有结点的值均大于它的根节点的值;

    (3)左、右子树也分别为二元查找树

    /**
    * 1:构造二叉查找树;
    * 2:中序遍历二叉查找树,因此结点按从小到大顺序访问,
    *    假设之前访问过的结点已经调整为一个双向链表,那么
    *    只需要将当前结点连接至双向链表的最后一个结点即可,
    *    访问完后,双向链表也就调整完了
    *
    */
    #include <stdio.h>
    
    //      10
    //    /   
    //   6    14
    //  /   /  
    // 4  8 12  16
    
    struct BSTreeNode
    {
        int m_nValue;
        BSTreeNode *m_pLeft;
        BSTreeNode *m_pRight;
    };
    
    typedef BSTreeNode DoubleList;  
    DoubleList * pHead;  
    DoubleList * pListIndex; 
    static int i;
    
    /* 建立二叉排序树 */
    void addBSTreeNode(BSTreeNode *&pCurrent, int value)
    {
        if(pCurrent == NULL)
        {
            BSTreeNode *pBSTree = new BSTreeNode();
            pBSTree->m_nValue   = value;
            pBSTree->m_pLeft    = NULL;
            pBSTree->m_pRight   = NULL;
            pCurrent            = pBSTree;
        }
        else if(pCurrent->m_nValue < value)
        {
            addBSTreeNode(pCurrent->m_pRight, value);
        }
        else if(pCurrent->m_nValue > value)
        {
            addBSTreeNode(pCurrent->m_pLeft, value);
        }
        else
        {
            printf("Node repeated.
    ");
        }
    }
    
    void Mid_Walk_Tree(BSTreeNode *pRoot)
    {
        if(pRoot != NULL)
        {
            if(NULL != pRoot->m_pLeft)
            {
                Mid_Walk_Tree(pRoot->m_pLeft);    
            }
            
            printf("%d ", pRoot->m_nValue);
            
            if(NULL != pRoot->m_pRight)
            {
                Mid_Walk_Tree(pRoot->m_pRight);    
            }
        }
    }
    
    void ConvertToDoubleList(BSTreeNode *pCurrent)
    {
        pCurrent->m_pLeft = pListIndex;
        if(NULL != pListIndex)
        {
            pListIndex->m_pRight = pCurrent;
        }
        else
        {
            pHead = pCurrent;
        }
        pListIndex = pCurrent;
        printf("pCurrent->m_nValue = %d
    ", pCurrent->m_nValue);
    }
    
    /* 中序遍历二叉树,同时调整结点指针 */
    void inOrderBSTree(BSTreeNode* pBSTree)
    {
    
        if(NULL == pBSTree)
        {
            return;
        }
        
        if(NULL != pBSTree->m_pLeft)
        {
            inOrderBSTree(pBSTree->m_pLeft);
        }
        
        printf("i = %d
    ", i);
        
        ConvertToDoubleList(pBSTree);
        
        if(NULL != pBSTree->m_pRight)
        {
            inOrderBSTree(pBSTree->m_pRight);
        }
        i++;
    }
    
    
    int main()
    {
        BSTreeNode *pRoot = NULL;
        addBSTreeNode(pRoot, 10);
        addBSTreeNode(pRoot, 6);
        addBSTreeNode(pRoot, 14);
        addBSTreeNode(pRoot, 4);
        addBSTreeNode(pRoot, 8);
        addBSTreeNode(pRoot, 12);
        addBSTreeNode(pRoot, 16);
        
        //Mid_Walk_Tree(pRoot);
        
        
        inOrderBSTree(pRoot);
        
        
        return 0;
    }
  • 相关阅读:
    Python函数学习——作用域与嵌套函数
    Python函数学习——初步认识
    python-安装,设置环境变量(win10)
    python-正则表达式
    python-时间
    python-集合
    python-模块
    Python-sys模块,异常
    python-自定义异常,with用法
    python-异常
  • 原文地址:https://www.cnblogs.com/eternal1025/p/4422729.html
Copyright © 2020-2023  润新知