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

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


 题目:

输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。


  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 
#include "BST.h"

using namespace std;
//将二元查找树转变成双向链表


int main()
{
	int data[]={10,6,14,4,8,12,16};
	int num=7;

	struct BSTreeNode *Root=NULL;
	BSTree tp1;

	struct ListNode *lroot;

	//二元查找树的建立
	Root=tp1.buildBSTree(Root,data,num);
    //显示
	std::cout<<"前序遍历:"<


BST.h

#ifndef _BST_H_
#define _BST_H_

#include 

struct BSTreeNode
{
	int m_nValue; // value of node
	BSTreeNode *m_pLeft; // left child of node
	BSTreeNode *m_pRight; // right child of node
};

struct ListNode
{
	int data;
	ListNode *m_left;
	ListNode *m_right;
};

class BSTree{
public:
	BSTreeNode *buildBSTree(BSTreeNode *root,int *data,int num);
	BSTreeNode *BSTreeDelete(BSTreeNode *root,int *data,int num);
	void BSTreeNodePrint_leftfirst(BSTreeNode *root);
	void BSTreeNodePrint_midfirst(BSTreeNode *root);
	void BSTreeNodePrint_rightfirst(BSTreeNode *root);
};

struct ListNode *BSTreeToList(BSTreeNode *root);
void BSTreeListSearch(BSTreeNode *root,ListNode **lroot,ListNode **currentListNode,int &m);
void ListNodePrint(ListNode *head);

#endif

BST.cpp

#include "BST.h"

BSTreeNode *BSTree::buildBSTree(BSTreeNode *root,int *data,int num)
{
	for(int i=0;im_nValue=*(data+i);//initialize new node
		z->m_pLeft=NULL;
		z->m_pRight=NULL;

		BSTreeNode *y=NULL;
		BSTreeNode *x=root;
		while(x!=NULL)
		{
			y=x;
			if (z->m_nValuem_nValue)
			{
				x=x->m_pLeft;
			}
			else
			{
				x=x->m_pRight;
			}
		}
		if (y==NULL)
		{
			root=z;
		}
		else if (z->m_nValuem_nValue)
		{
			y->m_pLeft=z;
		}
		else
		{
			y->m_pRight=z;
		}
	}
	return root;
}


void BSTreeListSearch(BSTreeNode *root,ListNode **lroot,ListNode **currentListNode,int &m)//
{	
	BSTreeNode *currentNode=root;
	if(root!=NULL)
	{
		ListNode *zp=(struct ListNode*)malloc(sizeof(struct ListNode));

		if (currentNode->m_pLeft!=NULL)
		{
			BSTreeListSearch(currentNode->m_pLeft,lroot,currentListNode,m);
		}

		m++;//通过m判断是否为双向链表的第一个元素
		//将该节点加入到链表中
		zp->data=currentNode->m_nValue;
		zp->m_left=*currentListNode;		
		zp->m_right=NULL;
		if (m!=1)
		{
			(*currentListNode)->m_right=zp;
		}

		//更新链表中当前节点
		*currentListNode=zp;

		if(m==1)
		{
			*lroot=*currentListNode;
		}

		if (currentNode->m_pRight!=NULL)
		{
			BSTreeListSearch(currentNode->m_pRight,lroot,currentListNode,m);
		}
		
	}
}

struct ListNode *BSTreeToList(BSTreeNode *root)
{
	ListNode *P=NULL;
	ListNode *Q=NULL;//保证链表头结点的左向指针为空
	ListNode **currentListNode=&Q;
	ListNode **lroot=&P;
	int m=0;
	BSTreeListSearch(root,lroot,currentListNode,m);
	return *lroot;
}

void ListNodePrint(ListNode *head)
{
	while(head !=NULL)
	{
		std::cout<data<m_right;
	}
}

void BSTree::BSTreeNodePrint_leftfirst(BSTreeNode *root)//左根右,中序遍历
{
	BSTreeNode *currentNode=root;
	if(root!=NULL)
	{
		if (currentNode->m_pLeft!=NULL)
		{
			BSTreeNodePrint_leftfirst(currentNode->m_pLeft);
		}
		std::cout<m_nValue<m_pRight!=NULL)
		{
			BSTreeNodePrint_leftfirst(currentNode->m_pRight);
		}
	}
}

void BSTree::BSTreeNodePrint_midfirst(BSTreeNode *root)//根左右,前序遍历
{
	BSTreeNode *currentNode=root;
	if(currentNode!=NULL)
	{
		std::cout<m_nValue<m_pLeft!=NULL)
	{
		BSTreeNodePrint_midfirst(currentNode->m_pLeft);
	}
	if (currentNode->m_pRight!=NULL)
	{
		BSTreeNodePrint_midfirst(currentNode->m_pRight);
	}
}


void BSTree::BSTreeNodePrint_rightfirst(BSTreeNode *root)//左右根,后序遍历
{
	BSTreeNode *currentNode=root;
	if (root!=NULL)
	{
		if (currentNode->m_pLeft!=NULL)
		{
			BSTreeNodePrint_rightfirst(currentNode->m_pLeft);
		}
		if (currentNode->m_pRight!=NULL)
		{
			BSTreeNodePrint_rightfirst(currentNode->m_pRight);
		}
		std::cout<m_nValue<

运行结果:

把二元查找树转变成排序的双向链表_第1张图片



你可能感兴趣的:(数据结构/算法,C/C++,C#,MFC)