【100题】第一题
1.把二元查找树转变成排序的双向链表
题目:
输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。
要求不能创建任何新的结点,只调整指针的指向。
10
/ \
6 14
/ \ / \
4 8 12 16
转换成双向链表
4=6=8=10=12=14=16。
首先我们定义的二元查找树 节点的数据结构如下:
struct BSTreeNode
{
int m_nValue; // value of node
BSTreeNode *m_pLeft; // left child of node
BSTreeNode *m_pRight; // right child of node
};
#include <stdio.h>
struct BSTreeNode //结构体
{
int m_nValue; // 节点值
BSTreeNode *m_pLeft; // 左孩子
BSTreeNode *m_pRight; // 右孩子
};
typedef BSTreeNode DoubleList; //用typedef声明自定义类型
DoubleList * pHead;
DoubleList * pListIndex;
void convertToDoubleList(BSTreeNode * pCurrent); //转换为 双向链表
// 创建二元查找树
void addBSTreeNode(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) //小的放到左边
{
addBSTreeNode(pCurrent->m_pLeft, value);//递归调用
}
else if ((pCurrent->m_nValue) < value)//大的放到右边
{
addBSTreeNode(pCurrent->m_pRight, value);
}
else
{
printf("重复加入节点");
//cout<<"重复加入节点"<<endl;
}
}
}
void ergodicBSTree(BSTreeNode * pCurrent) //中序递归遍历
{
if (NULL == pCurrent)
{
return;
}
ergodicBSTree(pCurrent->m_pLeft);
convertToDoubleList(pCurrent); // 节点接到链表尾部
ergodicBSTree(pCurrent->m_pRight);
}
void convertToDoubleList(BSTreeNode * pCurrent)// 二叉树转换成双向链表
{
pCurrent->m_pLeft = pListIndex;
if (NULL != pListIndex)//双向链表中含有元素
{
pListIndex->m_pRight = pCurrent;
}
else
{
pHead = pCurrent;
}
pListIndex = pCurrent;//这一步是关键,保证每一步之后 都是插入双链表最后
printf("%d\n",pCurrent->m_nValue);//输出插入的当前节点值
}
int main()
{
BSTreeNode * pRoot = NULL;
pListIndex = NULL;
pHead = NULL;
addBSTreeNode(pRoot, 10);
addBSTreeNode(pRoot, 4);
addBSTreeNode(pRoot, 6);
addBSTreeNode(pRoot, 8);
addBSTreeNode(pRoot, 12);
addBSTreeNode(pRoot, 14);
addBSTreeNode(pRoot, 15);
addBSTreeNode(pRoot, 16);
ergodicBSTree(pRoot);//中序遍历树,同时建立链表,并按照遍历顺序输出每个节点
return 0;
}
///////////////////////////////////////////////
4
6
8
10
12
14
15
16
Press any key to continue
//////////////////////////////////////////////
--------------------------------------------------------------
同样是上道题,给各位看一段简洁的代码,领悟一下c的高效与美:
void change(Node *p, Node *&last) //中序遍历
{
if (!p)
return;
change(p->left, last);
if (last)
last->right = p;
p->left = last;
last = p;
change(p->right, last);
}
void main()
{
Node *root = create();
Node *tail = NULL;
change(root, tail);
while (tail)
{
cout << tail->data << " ";
tail = tail->left;
}
cout << endl;
}
--------------------------以下为第二种写法------------------------
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node *left;
Node *right;
Node(int d = 0, Node *lr = 0, Node *rr = 0):data(d), left(lr), right(rr){}
};
Node *create()
{
Node *root;
Node *p4 = new Node(4);
Node *p8 = new Node(8);
Node *p6 = new Node(6, p4, p8);
Node *p12 = new Node(12);
Node *p16 = new Node(16);
Node *p14 = new Node(14, p12, p16);
Node *p10 = new Node(10, p6, p14);
root = p10;
return root;
}
Node *change(Node *p, bool asRight)
{
if (!p)
return NULL;
Node *pLeft = change(p->left, false);
if (pLeft)
pLeft->right = p;
p->left = pLeft;
Node *pRight = change(p->right, true);
if (pRight)
pRight->left = p;
p->right = pRight;
Node *r = p;
if (asRight)
{
while (r->left)
r = r->left;
}else{
while (r->right)
r = r->right;
}
return r;
}
void main(){
Node *root = create();
Node *tail = change(root, false);
while (tail)
{
cout << tail->data << " ";
tail = tail->left;
}
cout << endl;
root = create();
Node *head = change(root, true);
while (head)
{
cout << head->data << " ";
head = head->right;
}
cout << endl;
}