【100题】二叉排序树转成双向链表

#include <stdio.h>
#include <iostream>
using namespace std;
struct BSTreeNode
{
    int m_nValue; // value of node
    BSTreeNode *m_pLeft; // left child of node
    BSTreeNode *m_pRight; // right child of node
};
typedef BSTreeNode DoubleList;
DoubleList * pHead;
// 创建二元查找树
void createBinarySearchTree(BSTreeNode * & pCurrent, int value)
{
    if (NULL == pCurrent)
    {
        BSTreeNode * pBSTree = new BSTreeNode();
        pBSTree->m_pLeft = NULL;
        pBSTree->m_pRight = NULL;
        pBSTree->m_nValue = value;
        pCurrent = pBSTree;
    }
    else 
    {
        if ((pCurrent->m_nValue) > value)
        {//如果比当前节点值小,则放到左子树
            createBinarySearchTree(pCurrent->m_pLeft, value);
        }
        else if ((pCurrent->m_nValue) < value)
        {//如果比当前节点值大,则放到右子树
            createBinarySearchTree(pCurrent->m_pRight, value);
        }
        else
        {//否则提示重复插入了。
            cout<<"重复加入节点"<<endl;
        }
    }
}

BSTreeNode *ConvertNode(BSTreeNode *pNode,bool asRight)
{
	if(!pNode)
	{
		return NULL;
	}
	BSTreeNode *pLeft = NULL;
	BSTreeNode *pRight = NULL;
	//如果左孩子不空,则递归转换左子树-->双向链表,并返回左子链表的最右端节点(最大值节点)
	if(pNode->m_pLeft)
	{
		pLeft = ConvertNode(pNode->m_pLeft,false);
	}
	//将当前节点与左子链表的最右端连接起来。
	if(pLeft)
	{
		pLeft->m_pRight = pNode;
		pNode->m_pLeft = pLeft;
	}
	//如果右孩子不空,则递归转换右子树-->双向链表,并返回右子链表的最左端的节点(最小值节点)
	if(pNode->m_pRight)
	{
		pRight = ConvertNode(pNode->m_pRight,true);
	}
	//把当前节点与右子链表上最左端节点连接起来。
	if(pRight)
	{
		pNode->m_pRight = pRight;
		pRight->m_pLeft = pNode;
	}

	//
	BSTreeNode *pTemp = pNode;
	//如果该分支是右子树,则将指针移动到最左侧
	if(asRight)
	{
		while(pTemp->m_pLeft)
		{
			pTemp = pTemp->m_pLeft;
		}
	}
	else//否则肯定是左子树,则移动指针到最右侧
	{
		while(pTemp->m_pRight)
		{
			pTemp = pTemp->m_pRight;
		}
	}
	return pTemp;
}

BSTreeNode *Convert(BSTreeNode *pHeadOfTree)
{
	return ConvertNode(pHeadOfTree,true);
}

int main()
{
    BSTreeNode * pRoot = NULL;
    pHead = NULL;
    createBinarySearchTree(pRoot, 10);
    createBinarySearchTree(pRoot, 4);
    createBinarySearchTree(pRoot, 6);
    createBinarySearchTree(pRoot, 8);
    createBinarySearchTree(pRoot, 12);
    createBinarySearchTree(pRoot, 14);
    createBinarySearchTree(pRoot, 15);
    createBinarySearchTree(pRoot, 16);

	pHead = Convert(pRoot);
	//从最左侧开始打印双向链表
	cout << "从左向右打印:";
	DoubleList *p = pHead;
	while(p != NULL)
	{
		cout << p->m_nValue<<" ";
		p= p->m_pRight;
	}
	//从最右侧开始打印双向链表
	cout << endl <<"从右向左打印:";
	while(pHead->m_pRight != NULL)
	{
		pHead= pHead->m_pRight;
	}
	while(pHead != NULL)
	{
		cout << pHead->m_nValue<<" ";
		pHead = pHead->m_pLeft;
	}
	cout << endl;
    return 0;
}


 

运行结果:

从左向右打印:4 6 8 10 12 14 15 16
从右向左打印:16 15 14 12 10 8 6 4
请按任意键继续. . .

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值