今天看到一个有趣的算法,然后自己动手写来了一下。要求把二叉查找树BineraySortTree转化成链表List,并且在这过程中不是用辅助的存储空间,只是变化一下指针。
熄灯后写了差不多1个小时,总是有段错误。。。查找半天才发现吧是没有给指针初始化,桑不起啊。。。。。OK,下面是详细的代码,测试过,可以运行。
#include<stdio.h>
#include<stdlib.h>
/****************************************************
*define the node structure
****************************************************/
struct BSTreeNode
{
int value;
struct BSTreeNode *left;
struct BSTreeNode *right;
};
/***************************************************
*
*Function name: Insert
*
*Info:use this function to insert node into the binarysrotree
* and the structure is stable.
*
*Arguments:BSTreeNode *p,the node of the tree to be run
* int x,the nodeinfo.
*
* ************************************************/
struct BSTreeNode *Insert(struct BSTreeNode *p,int x)
{
if(p == NULL)
{
p= (struct BSTreeNode *)malloc(sizeof(struct BSTreeNode));
p->value=x;
p->left=NULL;
p->right=NULL;
}
else
{
if(p->value >x)
{
p->left=Insert(p->left,x);
}
else
{
p->right=Insert(p->right,x);
}
}
return p;
}
/******************************************************
*
*Name: Traverse
*
*Info: run the binarysortree in middle rule
*
*Arguments:BSTreeNode *p, the node of the tree to be run of
*
* ***************************************************/
void Traverse(struct BSTreeNode *p)
{
if(p == NULL)
{
return;
}
Traverse(p->left);
printf("%d ",p->value);
Traverse(p->right);
}
/******************************************************
*
*Function Name:Convert
*
*Function Info:Change the BinarySortTree to List
*
*Arguments:BSTreeNode * node
*
*************************************************** */
struct BSTreeNode * Convert(struct BSTreeNode * node)
{
if(node == NULL)
{
return NULL;
}
struct BSTreeNode *leftMax,*rightMin;
leftMax=node->left;
rightMin=node->right;
/*Find the maxnode in the leftChildTree*/
while(leftMax != NULL && leftMax->right != NULL)
{
leftMax=leftMax->right;
}
/*Find the minnode in the rightChildTree*/
while(rightMin != NULL && rightMin->left != NULL)
{
rightMin=rightMin->left;
}
/*递归求解*/
Convert(node->right);
Convert(node->left);
/*Connect the leftchildtree and rightchildtree to the root ,as brothers
*/
if(leftMax != NULL)
{
leftMax->right=node;
}
if(rightMin != NULL)
{
rightMin->left=node;
}
node->left=leftMax;
node->right=rightMin;
return node;
}
/*------------------------------main-------------------------------------*/
int main()
{
/*切记--------初始化指针---------------!!
* 未初始话的指针可读不可写*/
struct BSTreeNode *p = NULL;
/*如果不初始化指针的话,在Insert函数递归调用中会出现段错误*/
int a[10]={5,3,6,7,4,2,1,15,8,12};
int i=0;
while(i<10)
{
p=Insert(p,a[i++]);
}
/*中序遍历二叉查找树*/
Traverse(p);
/*将二叉查找树转化成链表*/
Convert(p);
while(p != NULL)
{
printf("%d ",p->value );
p=p->right;
}
return 0;
}
大功告成!
熄灯后写了差不多1个小时,总是有段错误。。。查找半天才发现吧是没有给指针初始化,桑不起啊。。。。。OK,下面是详细的代码,测试过,可以运行。
#include<stdio.h>
#include<stdlib.h>
/****************************************************
*define the node structure
****************************************************/
struct BSTreeNode
{
int value;
struct BSTreeNode *left;
struct BSTreeNode *right;
};
/***************************************************
*
*Function name: Insert
*
*Info:use this function to insert node into the binarysrotree
* and the structure is stable.
*
*Arguments:BSTreeNode *p,the node of the tree to be run
* int x,the nodeinfo.
*
* ************************************************/
struct BSTreeNode *Insert(struct BSTreeNode *p,int x)
{
if(p == NULL)
{
p= (struct BSTreeNode *)malloc(sizeof(struct BSTreeNode));
p->value=x;
p->left=NULL;
p->right=NULL;
}
else
{
if(p->value >x)
{
p->left=Insert(p->left,x);
}
else
{
p->right=Insert(p->right,x);
}
}
return p;
}
/******************************************************
*
*Name: Traverse
*
*Info: run the binarysortree in middle rule
*
*Arguments:BSTreeNode *p, the node of the tree to be run of
*
* ***************************************************/
void Traverse(struct BSTreeNode *p)
{
if(p == NULL)
{
return;
}
Traverse(p->left);
printf("%d ",p->value);
Traverse(p->right);
}
/******************************************************
*
*Function Name:Convert
*
*Function Info:Change the BinarySortTree to List
*
*Arguments:BSTreeNode * node
*
*************************************************** */
struct BSTreeNode * Convert(struct BSTreeNode * node)
{
if(node == NULL)
{
return NULL;
}
struct BSTreeNode *leftMax,*rightMin;
leftMax=node->left;
rightMin=node->right;
/*Find the maxnode in the leftChildTree*/
while(leftMax != NULL && leftMax->right != NULL)
{
leftMax=leftMax->right;
}
/*Find the minnode in the rightChildTree*/
while(rightMin != NULL && rightMin->left != NULL)
{
rightMin=rightMin->left;
}
/*递归求解*/
Convert(node->right);
Convert(node->left);
/*Connect the leftchildtree and rightchildtree to the root ,as brothers
*/
if(leftMax != NULL)
{
leftMax->right=node;
}
if(rightMin != NULL)
{
rightMin->left=node;
}
node->left=leftMax;
node->right=rightMin;
return node;
}
/*------------------------------main-------------------------------------*/
int main()
{
/*切记--------初始化指针---------------!!
* 未初始话的指针可读不可写*/
struct BSTreeNode *p = NULL;
/*如果不初始化指针的话,在Insert函数递归调用中会出现段错误*/
int a[10]={5,3,6,7,4,2,1,15,8,12};
int i=0;
while(i<10)
{
p=Insert(p,a[i++]);
}
/*中序遍历二叉查找树*/
Traverse(p);
/*将二叉查找树转化成链表*/
Convert(p);
while(p != NULL)
{
printf("%d ",p->value );
p=p->right;
}
return 0;
}
大功告成!