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

為了賬號安全,請及時綁定郵箱和手機立即綁定

析構(gòu)函數(shù)內(nèi)存釋放問題

程序如下,望大佬指點

// data_structure_explore_tree.cpp : 定義控制臺應(yīng)用程序的入口點。
//數(shù)組實現(xiàn)二叉樹的儲存,利用父親結(jié)點下標*2+1是左孩子結(jié)點 父親結(jié)點*2+2是右孩子結(jié)點
//任務(wù):樹的創(chuàng)建和銷毀
//樹中結(jié)點的搜索
//樹中結(jié)點的添加與刪除
//樹中結(jié)點的遍歷
#include "stdafx.h"
#include <iostream>
using namespace std;

class Tree {
public:
?Tree(int size,int *pRoot);
?~Tree();
?bool AddNode(int nodeIndex, int direction, int *pNode);
?int *SearchNode(int nodeIndex);//nodeIndex是結(jié)點的標識,記錄結(jié)點位置
?bool DeleteNode(int nodeIndex, int *pNode);
?void TreeTraverse();
?int *m_pTree;//記錄結(jié)點的一個數(shù)組
?int m_iSize;
};
Tree::Tree(int size,int *pRoot) {//pRoot表示根結(jié)點的值
?m_iSize = size;
?m_pTree = new int[size];
?for (int i = 0;i < size;i++) {
??m_pTree[i] = 0;
?}
?m_pTree[0] = *pRoot;
}
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) {
?//direction標識插入的結(jié)點是左還是右孩子,0表示左,1表示右
?//NodeIndex表示要插入的根節(jié)點位置
?if (nodeIndex < 0 || nodeIndex >= m_iSize||m_pTree[nodeIndex]==0) {
??return false;
?}
?if (direction == 0) {
??if (nodeIndex * 2 + 1 >= m_iSize||m_pTree[nodeIndex*2+1]!=0) {
???return false;
??}
??m_pTree[nodeIndex*2+1] = *pNode;
?}
?if (direction == 1) {
??if (nodeIndex * 2 + 2 >= m_iSize || m_pTree[nodeIndex * 2 + 2] != 0) {
???return false;
??}
??m_pTree[nodeIndex * 2 + 2] = *pNode;
?}
?return true;
}
bool Tree::DeleteNode(int nodeIndex, int *pNode) {//question:刪掉這個結(jié)點要不要把孩子節(jié)點刪掉啊
?if (nodeIndex<0 || nodeIndex>m_iSize || 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()
{
?int root = 3;
?int node1 = 5, node2 = 8;
?Tree *pTree = new Tree(10,&root);
?pTree->AddNode(0, 0,&node1);
?pTree->AddNode(0, 1, &node2);

?int node3 = 2, node4 = 6;
?pTree->AddNode(1, 0, &node3);
?pTree->AddNode(1, 1, &node4);

?int node5 = 9, node6 = 7;
?pTree->AddNode(2, 0, &node5);
?pTree->AddNode(2, 1, &node6);

?pTree->TreeTraverse();
?delete[]pTree;
??? return 0;
}


正在回答

1 回答

最后的delete []pTree,改成delete pTree.../(ㄒoㄒ)/~~

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

舉報

0/150
提交
取消

析構(gòu)函數(shù)內(nèi)存釋放問題

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

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

幫助反饋 APP下載

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

公眾號

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