02
17
34
40
为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我的程序添加完节点打出来的全是0

和老师的程序一模一样 ,结果一运行全是0

正在回答

代码语言

1 回答

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
不会啊,我就是按老师的代码敲的
 
/************************************************************
二叉树(链表表示)
课程要求:完成二叉树的基本操作
    1,树的创建和销毁
    2,树中结点的搜索
    3,树中结点的添加与删除
    4,树中结点的遍历
     
    Tree();   //创建树
    ~Tree();               //销毁树
    Node *SearchNode(Tree *pTree,int nodeindex);  //根据索引寻找结点 
    bool AddNode(Tree *pTree,int nodeindex,int direction,Node *pNode);  //添加结点
    bool DeleteNode(Tree *pTree,int nodeindex,Node *pNode);            //删除结点
    void preorderTraverse();               //先序遍历二叉树
    void InorderTraverse();               //中序遍历二叉树
    void PosorderTraverse();               //后序遍历二叉树
 
结点要素:索引、数据、左孩子指针、右孩子指针、父结点指针 
     
            3(0)
             
     5(1)          8(2)
      
2(3)     6(4)   9(5)   7(6)
     
那个direction是“0”时表示插入到左孩子,是“1”时表示插入到右孩子 
先序遍历结果(根----左----右)0 1 3 4 2 5 6  
中序遍历结果(左----根----右)3 1 4 0 5 2 6    
后序遍历结果(左----右----根)3 4 1 5 6 2 0
 
*************************************************************/
 
#include<iostream>
#include<stdio.h>
 
using namespace std;
 
class Node
{
public:
    Node();//构造函数 
    Node *SearchNode(int nodeindex);
    void DeleteNode();
    void preorderTraverse();               //先序遍历二叉树
    void InorderTraverse();               //中序遍历二叉树
    void PosorderTraverse();               //后序遍历二叉树
    int index;
    int data;
    Node *pLChild;
    Node *pRChild;
    Node *pParent;    
};
 
class Tree
{
public:
    Tree();                                //创建树 
    ~Tree();                               //销毁树 
    Node *SearchNode(int nodeindex);       //搜索结点 
    bool AddNode(int nodeindex,int direction,Node *pNode);   //添加结点 
    bool DeleteNode(int nodeindex,Node *pNode);              //删除结点 
    void preorderTraverse();               //先序遍历二叉树
    void InorderTraverse();               //中序遍历二叉树
    void PosorderTraverse();               //后序遍历二叉树
private:
    Node *m_pRoot;   
};
 
Node::Node()
{
    index=0;
    data=0;
    pLChild=NULL;
    pRChild=NULL;
    pParent=NULL;    
 
Tree::Tree()
{
    m_pRoot=new Node();
}
 
Tree::~Tree()
{
    //DeleteNode(0,NULL);//方法一 
    m_pRoot->DeleteNode();//方法二 
}
 
Node *Node::SearchNode(int nodeindex)
{
    if(this->index==nodeindex)
        return this;
    Node *temp=NULL;
    if(this->pLChild!=NULL)
    {
        if(this->pLChild->index==nodeindex)
            return this->pLChild;
        else
            temp=this->pLChild->SearchNode(nodeindex);
         
    }
    if(this->pRChild!=NULL)
    {
        if(this->pRChild->index==nodeindex)
            return this->pRChild;
        else
            temp=this->pRChild->SearchNode(nodeindex);
        if(temp!=NULL)
            return temp;
    }
    return NULL;
}
 
Node *Tree::SearchNode(int nodeindex)
{
    return m_pRoot->SearchNode(nodeindex);
}
 
bool Tree::AddNode(int nodeindex,int direction,Node *pNode)
{
    Node *temp=SearchNode(nodeindex);
    if(temp==NULL)
        return false;
    Node *node=new Node();
    if(node==NULL)
        return false;
    node->index=pNode->index;
    node->data=pNode->data;
     
    node->pParent=temp;
    if(direction==0)
        temp->pLChild=node;
    if(direction==1)
        temp->pRChild=node;
    return true;
}
 
bool Tree::DeleteNode(int nodeindex,Node *pNode)
{
    Node *temp=SearchNode(nodeindex);
    if(temp==NULL)
        return false;
    if(pNode!=NULL)
    {
        pNode->data=temp->data;
    }
    temp->DeleteNode();
    return true;
}
 
void Node::DeleteNode()
{
    if(this->pLChild!=NULL)
        this->pLChild->DeleteNode();
    if(this->pRChild!=NULL)
        this->pRChild->DeleteNode();
    if(this->pParent!=NULL)
    {
        if(this->pParent->pLChild==this)
            this->pParent->pLChild=NULL;
        if(this->pParent->pRChild==this)
            this->pParent->pRChild=NULL;
    }
    delete this;
}
 
void Node::preorderTraverse()
{
    cout << this->index << "   " << this->data << endl;
    if(this->pLChild!=NULL)
        this->pLChild->preorderTraverse();
    if(this->pRChild!=NULL)
        this->pRChild->preorderTraverse();
 
void Node::InorderTraverse()
{
    if(this->pLChild!=NULL)
        this->pLChild->InorderTraverse();
         
    cout << this->index << "   " << this->data << endl;
     
    if(this->pRChild!=NULL)
        this->pRChild->InorderTraverse();
}
void Node::PosorderTraverse()
{
    if(this->pLChild!=NULL)
        this->pLChild->PosorderTraverse();
     
    if(this->pRChild!=NULL)
        this->pRChild->PosorderTraverse();
         
    cout << this->index << "   " << this->data << endl;
}
 
void Tree::preorderTraverse()
{
    m_pRoot->preorderTraverse();
 
void Tree::InorderTraverse()
{
    m_pRoot->InorderTraverse();
 
void Tree::PosorderTraverse()
{
    m_pRoot->PosorderTraverse();
 
int main()
{
    Node *node1=new Node();
    node1->index=1;
    node1->data=5;
     
    Node *node2=new Node();
    node2->index=2;
    node2->data=8;
     
    Node *node3=new Node();
    node3->index=3;
    node3->data=2;
     
    Node *node4=new Node();
    node4->index=4;
    node4->data=6;
     
    Node *node5=new Node();
    node5->index=5;
    node5->data=9;
     
    Node *node6=new Node();
    node6->index=6;
    node6->data=7;
     
    Tree *tree=new Tree();
     
    tree->AddNode(0,0,node1);
    tree->AddNode(0,1,node2);
     
    tree->AddNode(1,0,node3);
    tree->AddNode(1,1,node4);
     
    tree->AddNode(2,0,node5);
    tree->AddNode(2,1,node6);
     
    //printf("删除最后一个结点:\n");
    //tree->DeleteNode(6,NULL);
     
    printf("删除中间某个结点:\n");
    tree->DeleteNode(2,NULL);
//    printf("前序遍历的结果:\n");
//    tree->preorderTraverse();
     
//    printf("中序遍历的结果:\n");
//    tree->InorderTraverse(); 
     
    printf("后序遍历的结果:\n");
    tree->PosorderTraverse(); 
    delete tree;
    return 0;
}


1 回复 有任何疑惑可以回复我~
#1

慕UI8082421 提问者

非常感谢!问题解决啦
2016-11-10 回复 有任何疑惑可以回复我~
#2

Squirre_lMan 回复 慕UI8082421 提问者

if (this->plnode != NULL) { this->PreorderTravers(); } 这样就导致程序调试后不断的打印0,几秒钟后出现异常"stack overflow"。 解决的办法就是用this的左节点继续调用PreorderTravers(): if (this->plnode != NULL) { this->plnode->PreorderTravers(); }
2017-02-19 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

为什么我的程序添加完节点打出来的全是0

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号