二元查找树


二元查找树:(性质2和3决定了,二元查找树中没有重复的元素)

 它首先要是一棵二元树,在这基础上它或者是一棵空树;或者是具有下列性质的二元树: (1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值; (2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值; (3)左、右子树也分别为二元查找树


1.把二元查找树转变成排序的双向链表

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

方法一:

#include <iostream>
using namespace std;
//二元查找树的的数据结构
struct BSTreeNode
{
	int value;
	BSTreeNode *pLeft;
	BSTreeNode *pRight;
};

BSTreeNode *pHead = NULL;   //辅助变化成双链表
BSTreeNode *pListIndex = NULL;

void CreateBSTree(BSTreeNode *&pCurrent, int val)
{
	if(pCurrent == NULL)
	{
		BSTreeNode *pNewNode = new BSTreeNode;
		pNewNode->value = val;
		pNewNode->pLeft = NULL;
		pNewNode->pRight = NULL;
		pCurrent = pNewNode;    //递归出来后,指向根节点
	}
	else
	{
		if(val < pCurrent->value)
			CreateBSTree(pCurrent->pLeft, val);
		else if(val > pCurrent->value)
			CreateBSTree(pCurrent->pRight, val);
		else
			return;
	}
}

void BSTreeToList(BSTreeNode *pCurrent)   //转化成双链表
{
	pCurrent->pLeft = pListIndex;    //pListIndex用来指向上一个节点

	if(pListIndex == NULL)
		pHead = pCurrent;
	else
		pListIndex->pRight = pCurrent;

	pListIndex = pCurrent;
	cout<<pListIndex->value<<endl;
}

void MidSearch(BSTreeNode *pRoot)
{
	if(pRoot != NULL)
	{
		MidSearch(pRoot->pLeft);
		//cout<<pRoot->value<<endl;
		BSTreeToList(pRoot);
		MidSearch(pRoot->pRight);
	}
	else
		return;
}


int main()  
{  
    BSTreeNode *pRoot = NULL;

	CreateBSTree(pRoot, 10);
	CreateBSTree(pRoot, 6);
	CreateBSTree(pRoot, 4);
	CreateBSTree(pRoot, 8);
	CreateBSTree(pRoot, 14);
	CreateBSTree(pRoot, 12);
	CreateBSTree(pRoot, 16);

	MidSearch(pRoot);
	return 0;
}  
方法二:

#include <iostream>
using namespace std;

//二元查找树的的数据结构
class CBSTreeNode
{
public:
	int m_value;
	CBSTreeNode *m_pLeft;
	CBSTreeNode *m_pRight;


	CBSTreeNode(int value=0, CBSTreeNode *pLeft=NULL, CBSTreeNode *pRight=NULL):m_value(value),m_pLeft(pLeft),m_pRight(pRight){}
};


class CBSTree
{
	CBSTreeNode *pHead;   //辅助变化成双链表
	CBSTreeNode *pListIndex;

public:
	CBSTree():pHead(NULL),pListIndex(NULL){}


public:
	CBSTreeNode* Create();
	void MidSearch(CBSTreeNode *pRoot);
	void BSTreeToList(CBSTreeNode *pCurrent);   //转化成双链表
};


CBSTreeNode* CBSTree::Create()
{
	CBSTreeNode *pNewNode1 = new CBSTreeNode(4);
	CBSTreeNode *pNewNode2 = new CBSTreeNode(8);
	CBSTreeNode *pNewNode3 = new CBSTreeNode(6, pNewNode1, pNewNode2);
	CBSTreeNode *pNewNode4 = new CBSTreeNode(12);
	CBSTreeNode *pNewNode5 = new CBSTreeNode(16);
	CBSTreeNode *pNewNode6 = new CBSTreeNode(14, pNewNode4, pNewNode5);
	CBSTreeNode *pNewNode7 = new CBSTreeNode(10, pNewNode3, pNewNode6);
	
	CBSTreeNode *Root = pNewNode7;
	return Root;
}

void CBSTree::BSTreeToList(CBSTreeNode *pCurrent)   //转化成双链表
{
	pCurrent->m_pLeft = pListIndex;    //pListIndex用来指向上一个节点


	if(pListIndex == NULL)
		pHead = pCurrent;
	else
		pListIndex->m_pRight = pCurrent;


	pListIndex = pCurrent;
	cout<<pListIndex->m_value<<endl;
}

void CBSTree::MidSearch(CBSTreeNode *pRoot)
{
	if(pRoot != NULL)
	{
		MidSearch(pRoot->m_pLeft);
		//cout<<pRoot->value<<endl;
		BSTreeToList(pRoot);
		MidSearch(pRoot->m_pRight);
	}
	else
		return;
}

int main()  
{  
	CBSTree BSTreeObj;
	CBSTreeNode *pRoot=NULL;
	pRoot = BSTreeObj.Create();
	BSTreeObj.MidSearch(pRoot);
	return 0;
}  




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值