您的位置:首页 > 理论基础 > 数据结构算法

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

2013-12-29 20:46 302 查看
把二元查找树转变成排序的双向链表

 题目:

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

  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 <iostream>
#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<<"前序遍历:"<<std::endl;
tp1.BSTreeNodePrint_midfirst(Root);

std::cout<<"中序遍历:"<<std::endl;
tp1.BSTreeNodePrint_leftfirst(Root);

std::cout<<"后序遍历:"<<std::endl;
tp1.BSTreeNodePrint_rightfirst(Root);

//按中序遍历的顺序将二元查找树转换成双向链表
lroot=BSTreeToList(Root);
//显示链表中的内容
std::cout<<"链表显示:"<<std::endl;
ListNodePrint(lroot);
return 1;
}


BST.h

#ifndef _BST_H_
#define _BST_H_

#include <iostream>

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;i<num;i++)
{
BSTreeNode *z=(struct BSTreeNode*)malloc(sizeof(struct BSTreeNode));//结构体指针需要初始化

z->m_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_nValue<x->m_nValue)
{
x=x->m_pLeft;
}
else
{
x=x->m_pRight;
}
}
if (y==NULL)
{
root=z;
}
else if (z->m_nValue<y->m_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<<head->data<<std::endl;
head=head->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<<currentNode->m_nValue<<std::endl;
if (currentNode->m_pRight!=NULL)
{
BSTreeNodePrint_leftfirst(currentNode->m_pRight);
}
}
}

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

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息