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