第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

為什么我的程序添加完節(jié)點(diǎn)打出來(lái)的全是0

和老師的程序一模一樣 ,結(jié)果一運(yùn)行全是0

正在回答

1 回答

不會(huì)啊,我就是按老師的代碼敲的

/************************************************************
二叉樹(shù)(鏈表表示)
課程要求:完成二叉樹(shù)的基本操作
????1,樹(shù)的創(chuàng)建和銷(xiāo)毀
????2,樹(shù)中結(jié)點(diǎn)的搜索
????3,樹(shù)中結(jié)點(diǎn)的添加與刪除
????4,樹(shù)中結(jié)點(diǎn)的遍歷
????
????Tree();???//創(chuàng)建樹(shù)
????~Tree();???????????????//銷(xiāo)毀樹(shù)
????Node?*SearchNode(Tree?*pTree,int?nodeindex);??//根據(jù)索引尋找結(jié)點(diǎn)?
????bool?AddNode(Tree?*pTree,int?nodeindex,int?direction,Node?*pNode);??//添加結(jié)點(diǎn)
????bool?DeleteNode(Tree?*pTree,int?nodeindex,Node?*pNode);????????????//刪除結(jié)點(diǎn)
????void?preorderTraverse();???????????????//先序遍歷二叉樹(shù)
????void?InorderTraverse();???????????????//中序遍歷二叉樹(shù)
????void?PosorderTraverse();???????????????//后序遍歷二叉樹(shù)

結(jié)點(diǎn)要素:索引、數(shù)據(jù)、左孩子指針、右孩子指針、父結(jié)點(diǎn)指針?
????
????????????3(0)
????????????
?????5(1)??????????8(2)
?????
2(3)?????6(4)???9(5)???7(6)
????
那個(gè)direction是“0”時(shí)表示插入到左孩子,是“1”時(shí)表示插入到右孩子?
先序遍歷結(jié)果(根----左----右)0?1?3?4?2?5?6??
中序遍歷結(jié)果(左----根----右)3?1?4?0?5?2?6????
后序遍歷結(jié)果(左----右----根)3?4?1?5?6?2?0

*************************************************************/

#include<iostream>
#include<stdio.h>

using?namespace?std;

class?Node
{
public:
????Node();//構(gòu)造函數(shù)?
????Node?*SearchNode(int?nodeindex);
????void?DeleteNode();
????void?preorderTraverse();???????????????//先序遍歷二叉樹(shù)
????void?InorderTraverse();???????????????//中序遍歷二叉樹(shù)
????void?PosorderTraverse();???????????????//后序遍歷二叉樹(shù)
????int?index;
????int?data;
????Node?*pLChild;
????Node?*pRChild;
????Node?*pParent;????
};

class?Tree
{
public:
????Tree();????????????????????????????????//創(chuàng)建樹(shù)?
????~Tree();???????????????????????????????//銷(xiāo)毀樹(shù)?
????Node?*SearchNode(int?nodeindex);???????//搜索結(jié)點(diǎn)?
????bool?AddNode(int?nodeindex,int?direction,Node?*pNode);???//添加結(jié)點(diǎn)?
????bool?DeleteNode(int?nodeindex,Node?*pNode);??????????????//刪除結(jié)點(diǎn)?
????void?preorderTraverse();???????????????//先序遍歷二叉樹(shù)
????void?InorderTraverse();???????????????//中序遍歷二叉樹(shù)
????void?PosorderTraverse();???????????????//后序遍歷二叉樹(shù)
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("刪除最后一個(gè)結(jié)點(diǎn):\n");
????//tree->DeleteNode(6,NULL);
????
????printf("刪除中間某個(gè)結(jié)點(diǎn):\n");
????tree->DeleteNode(2,NULL);
//????printf("前序遍歷的結(jié)果:\n");
//????tree->preorderTraverse();
????
//????printf("中序遍歷的結(jié)果:\n");
//????tree->InorderTraverse();?
????
????printf("后序遍歷的結(jié)果:\n");
????tree->PosorderTraverse();?
????delete?tree;
????return?0;
}


1 回復(fù) 有任何疑惑可以回復(fù)我~
#1

慕UI8082421 提問(wèn)者

非常感謝!問(wèn)題解決啦
2016-11-10 回復(fù) 有任何疑惑可以回復(fù)我~
#2

Squirre_lMan 回復(fù) 慕UI8082421 提問(wèn)者

if (this->plnode != NULL) { this->PreorderTravers(); } 這樣就導(dǎo)致程序調(diào)試后不斷的打印0,幾秒鐘后出現(xiàn)異常"stack overflow"。 解決的辦法就是用this的左節(jié)點(diǎn)繼續(xù)調(diào)用PreorderTravers(): if (this->plnode != NULL) { this->plnode->PreorderTravers(); }
2017-02-19 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

為什么我的程序添加完節(jié)點(diǎn)打出來(lái)的全是0

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)