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

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

關(guān)于數(shù)據(jù)結(jié)構(gòu)中的樹,james老師的授課內(nèi)容

我打印出來的是 3 5 8 2 6 9 0 0 0 0 120,后面有個(gè)120是怎么回事啊

#include<iostream>
#include<stdlib.h>?
using?namespace?std;

class?Tree
{
??public:
??	Tree(int?size,int?*pRoot);//創(chuàng)建樹?
	~Tree();//銷毀樹?
	int?*SearchNode(int?nodeIndex);?//根據(jù)索引尋找結(jié)點(diǎn)?
	bool?AddNode(int?nodeIndex,int?direction,int?*pNode);//添加結(jié)點(diǎn)
	bool?DeleteNode(int?nodeIndex,int?*pNode);//刪除結(jié)點(diǎn)
	void?TreeTraverse();//遍歷
??
??private:
??	int?*m_pTree;
??	int?m_iSize;//記錄數(shù)組的大小?
};

Tree::Tree(int?size,int?*pRoot)
{
	m_iSize=size;?
	m_pTree?=?new?int[size];//申請內(nèi)存?
	for(int?i;i<size;i++)
	{
		m_pTree[i]=0;
	}
	m_pTree[0]=*pRoot;//初始化根節(jié)點(diǎn)?
}

Tree::~Tree()
{
	delete?[]m_pTree;
	m_pTree=NULL;
}

int?*Tree::SearchNode(int?nodeIndex)
{
	if(nodeIndex<0||nodeIndex>=m_iSize)
	{
		return?NULL;
	}
	if(m_pTree[nodeIndex]==0)
	{
		return?NULL;
	}
	return?&m_pTree[nodeIndex];
}

bool?Tree::AddNode(int?nodeIndex,int?direction,int?*pNode)
{
	if(nodeIndex<0||nodeIndex>=m_iSize)
	{
		return?false;
	}
	if(m_pTree[nodeIndex]==0)
	{
		return?false;
	}
	if(direction==0)
	{
		if(nodeIndex*2+1>=m_iSize)
	????{
		????return?false;
	????}
	????if(m_pTree[nodeIndex*2+1]!=0)
	????{
		????return?false;
	????}
		m_pTree[nodeIndex*2+1]=*pNode;
	}
	if(direction==1)
	{
		if(nodeIndex*2+2>=m_iSize)
	????{
		????return?false;
	????}
	????if(m_pTree[nodeIndex*2+2]!=0)
	????{
		????return?false;
	????}
		m_pTree[nodeIndex*2+2]=*pNode;
	}
	return?true;
}

bool?Tree::DeleteNode(int?nodeIndex,int?*pNode)
{
	if(nodeIndex<0||nodeIndex>=m_iSize)
	{
		return?false;
	}
	if(m_pTree[nodeIndex]==0)
	{
		return?false;
	}
	*pNode=m_pTree[nodeIndex];
	m_pTree[nodeIndex]=0;
	return?true;
}

void?Tree::TreeTraverse()
{
	for(int?i=0;i<=m_iSize;i++)
	{
		cout<<m_pTree[i]<<"?";
	}
}

int?main(void)
{
	int?root=3;?
	Tree?*pTree?=?new?Tree(10,&root);
	
	int?node1=5;
	int?node2=8;
	pTree->AddNode(0,0,&node1);
	pTree->AddNode(0,1,&node2);
	
	int?node3=2;
	int?node4=6;
	pTree->AddNode(1,0,&node3);
	pTree->AddNode(1,1,&node4);
	
????int?node5=9;
	int?node6=7;
	pTree->AddNode(2,0,&node5);
	pTree->AddNode(2,1,&node6);
	
	int?node=0;
	pTree->DeleteNode(6,&node);
????cout<<"?node?="<<node<<endl;
	pTree->TreeTraverse();
	
	int?*p=pTree->SearchNode(2);
	cout<<endl;
	cout<<"?node?="<<*p<<endl;
	
	delete?pTree;
	
	
	system("pause");
	return?0;
}


正在回答

1 回答

你的代碼在遍歷那里有錯(cuò)啊,for(int?i=0;i<=m_iSize;i++)這里去掉等于號(hào)for(int?i=0;i<m_iSize;i++),還有構(gòu)造函數(shù)那里for(int?i;i<size;i++)的i要初始化for(int?i=0;i<size;i++)。這樣就好了

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

舉報(bào)

0/150
提交
取消

關(guān)于數(shù)據(jù)結(jié)構(gòu)中的樹,james老師的授課內(nèi)容

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

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

幫助反饋 APP下載

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

公眾號(hào)

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