二元查找樹


二元查找樹:(性質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;
}  




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章