一个bug啊!!!怎么查不出来啊!!!简单的树结构问题!!!

深夜代码党 2011-04-04 07:52:42
编july出的第一道题,出现小bug却怎么也查不出来啊!!!谁帮我看一下?
原题:
=====================================================
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<iostream>
#include<Windows.h>
using namespace std;
struct Node{
int value;
Node* left;
Node* right;
};
struct Tree{
Node* parent;
};

int minimum(const Node* node){
while(node->left)
node=node->left;
return node->value;
}

int maximum(const Node* node){
while(node->right)
node=node->right;
return node->value;
}

void insert(Tree* &tree, int value){
Node* pNode=tree->parent;
if(pNode==NULL){
Node* nNode=new Node;
nNode->left=NULL;
nNode->right=NULL;
nNode->value=value;
tree->parent=nNode;
return;
}
while(1){
if(value<pNode->value){
if(pNode->left)
pNode=pNode->left;
else{
Node* nNode=new Node;
nNode->left=NULL;
nNode->right=NULL;
nNode->value=value;
pNode->left=nNode;
return;
}
}
else{
if(pNode->right)
pNode=pNode->right;
else{
Node* nNode=new Node;
nNode->left=NULL;
nNode->right=NULL;
nNode->value=value;
pNode->right=nNode;
return;
}
}
}
}

int inSearch(const Node* node, int value){
if(node==NULL)
return -1;
if(node->value==value)
return value;
if(value<node->value)
return inSearch(node->left, value);
else
return inSearch(node->right, value);
}

int search(const Tree* tree, int value){
return inSearch(tree->parent,value);
}

void internalPreOrder(const Node* node){
if(node==NULL)
return;
cout<<node->value<<'\t';
internalPreOrder(node->left);
internalPreOrder(node->right);
}

void preOrder(const Tree* tree){
internalPreOrder(tree->parent);
}

void internalInOrder(const Node* node){
if(node==NULL)
return;
internalInOrder(node->left);
cout<<node->value<<'\t';
internalInOrder(node->right);
}

void inOrder(const Tree* tree){
internalInOrder(tree->parent);
}

void internalPostOrder(const Node* node){
if(node==NULL)
return;
internalPostOrder(node->left);
internalPostOrder(node->right);
cout<<node->value<<'\t';
}

void postOrder(const Tree* tree){
internalPostOrder(tree->parent);
}

void convert(Node* &node, Node* &nodeList){
node->left=nodeList;
if(nodeList!=NULL){
nodeList->right=node;
}
cout<<"convert"<<endl;
nodeList=node;
nodeList->right=node->right;
cout<<node->value<<endl;
}

void internalInOrderConv(Node* &treeNode, Node* &nodeList){
if(treeNode==NULL){
cout<<"this is null"<<endl;
return;
}
if(treeNode->left!=NULL){
cout<<treeNode->value<<"+left"<<endl;
Sleep(100);
internalInOrderConv(treeNode->left, nodeList);
}
convert(treeNode, nodeList);
if(treeNode->right!=NULL){
cout<<treeNode->value<<"+right"<<endl;
internalInOrderConv(treeNode->right, nodeList);
}
}

void inOrderConv(Tree* &tree, Node* &nodeList){
internalInOrderConv(tree->parent, nodeList);
}


void main(){
Tree* tree=new Tree;
tree->parent=NULL;
insert(tree, 10);
insert(tree, 6);
insert(tree, 14);
insert(tree, 4);
insert(tree, 5);
insert(tree, 13);
insert(tree, 16);
postOrder(tree);
cout<<endl;
inOrder(tree);
cout<<endl;
preOrder(tree);
cout<<endl;
Node* pNode=NULL;
inOrderConv(tree, pNode);
system("pause");
}
...全文
122 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ulfsaar 2011-04-05
  • 打赏
  • 举报
回复
感觉你这个Convert函数有问题,我改了下,你试试看看

void convert(Node* &node, Node* &nodeList)
{
node->left=nodeList;
cout<<"convert"<<endl;
if(nodeList != NULL)
{
nodeList->right=node;
}
else
{
nodeList=node;
nodeList->right=node->right;
}
cout<<node->value<<endl;
}
qq120848369 2011-04-05
  • 打赏
  • 举报
回复
10 6 14 4 8 12 16
中序遍历结果:
4 6 8 10 12 14 16
双向链表结果:
4--6--8--10--12--14--16--NULL
反向链表结果:
16 14 12 10 8 6 4
请按任意键继续. . .

#include <iostream>
using namespace std;

typedef struct node
{
struct node *left,*right;
int n;
}Node;

typedef struct binTree
{
Node *root;
int size;
}BinTree;

void initNode(Node *pNode,int n)
{
pNode->left=pNode->right=NULL;
pNode->n=n;
}

void initTree(BinTree *tree)
{
tree->root=NULL;
tree->size=0;
}

void insert(Node *pNode,BinTree *tree)
{
Node *cur=tree->root,*pre=NULL;
bool isLeft;

while(cur!=NULL)
{
pre=cur;

if(pNode->n<cur->n)
{
cur=cur->left;
isLeft=true;
}
else
{
cur=cur->right;
isLeft=false;
}
}

if(pre==NULL)
{
tree->root=pNode;
}
else
{
if(isLeft)
{
pre->left=pNode;
}
else
{
pre->right=pNode;
}
}

++tree->size;
}

void print(Node *pNode)
{
if(pNode!=NULL)
{
print(pNode->left);
cout<<pNode->n<<" ";
print(pNode->right);
}
}

Node* createList(Node *pNode)
{
if(pNode!=NULL)
{
Node *leftHead=createList(pNode->left);
Node *rightHead=createList(pNode->right);

Node *pre=NULL,*cur=leftHead;

while(cur!=NULL)
{
pre=cur;
cur=cur->right; //相当于cur=cur->next
}

pNode->left=pre;
pNode->right=rightHead;

if(pre)
{
pre->right=pNode;
}

if(rightHead)
{
rightHead->left=pNode;
}

if(leftHead == NULL)
{
return pNode;
}
else
{
return leftHead;
}
}
else
{
return NULL;
}
}

int main()
{
BinTree tree;
initTree(&tree);

Node input[7];
int k;

for(int i=0;i<7;++i)
{
cin>>k;
initNode(input+i,k);
insert(input+i,&tree);
}

cout<<"中序遍历结果:"<<endl;
print(tree.root);
cout<<endl;

cout<<"双向链表结果:"<<endl;

Node *list=createList(tree.root),*pre=NULL;

while(list!=NULL)
{
cout<<list->n<<"--";
pre=list;
list=list->right;
}

cout<<"NULL"<<endl;

cout<<"反向链表结果:"<<endl;

while(pre!=NULL)
{
cout<<pre->n<<" ";
pre=pre->left;
}

cout<<endl;

return 0;
}


哥就是思路那么尖锐,一步到位。
qq120848369 2011-04-05
  • 打赏
  • 举报
回复
不就是中序遍历的过程中构造链表么.

递归定义很简单: 构造root树的链表,相当于先构造左子树的链表,然后把root接在后边,然后构造右子树的链表,接在root后边。
luohaohahaha 2011-04-04
  • 打赏
  • 举报
回复
lz ,你这树结构好像不简单哦。
luciferisnotsatan 2011-04-04
  • 打赏
  • 举报
回复
真长......
单步调试是程序员必须学会的技术
Ulfsaar 2011-04-04
  • 打赏
  • 举报
回复
按“左根右”的方式遍历二叉树就可以了吧
遍历的顺序就是链表中各节点的顺序,也不用判断大小

64,652

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧