C++ stack问题

shuaiwang_01 2011-03-07 08:11:01

#include <iostream>
#include <stack>
using namespace std;

class Node
{
public:
char* data;
Node* left;
Node* right;
int balance;
int count;
Node()
{
data = NULL;
left = right = NULL;
balance = count = 0;
}
Node(char* s)
{
data = new char[30];
int i;
for(i = 0; i < strlen(data); i++)
data[i] = s[i];
data[i] = '\0';
left = NULL;
right = NULL;
balance = 0;
count = 1;
}
};


class AVLTree
{
private:
Node* root;

public:
int vertexNumber;
AVLTree()
{
root = NULL;
vertexNumber = 0;
}


AVLTree(Node* r)
{
root = r;
}
int depth(Node* node)
{
if(node == NULL)
return -1;
int l = depth(node->left);
int r = depth(node->right);

return (l < r)?(r+1):(l+1);
}

void insert(char* &str)
{
Node* child = root;
Node* parent = NULL;
Node* grandpa = NULL;
int flag;
stack<Node* > st;

if(root == NULL)
{
Node* no = new Node(str);
root = no;
return;
}

while(child != NULL)
{
if(strcmp(str, child->data) == 0)
{
(child->count)++;
delete str;
return;
}

parent = child;
st.push(parent);
if(strcmp(str, child->data) < 0)
child = parent->left;
else
child = parent->right;
}
Node* node = new Node(str);
if(strcmp(node->data, parent->data) < 0)
parent->left = node;
else
parent->right = node;

while(!st.empty())
{
parent = st.top();
st.pop();
if(parent->left == node)
parent->balance += 1;
else
parent->balance -= 1;

if(parent->balance == 0)
break;
else if(parent->balance == -1 || parent->balance == 1)
node = parent;
else
{
flag = (parent->balance < 0)? -1 : 1;
if(node->balance == flag)
{
if(flag == -1)
leftRotate(parent);
else
rightRotate(parent);
}
else
{
if(parent->balance == -2)
rightLeftRotate(parent);
else
leftRightRotate(parent);
}
break;
}
}

if(st.empty())
root = parent;
else
{
grandpa = st.top();
if(grandpa->data > parent->data)
grandpa->left = parent;
else
grandpa->right = parent;
}
}

void leftRotate(Node* &node)
{
Node* temp = node;
node = node->right;
temp->right = node->left;
node->left = temp;
temp->balance = node->balance = 0;
}

void rightRotate(Node* &node)
{
Node* temp = node;
node = node->left;
temp->left = node->right;
node->right = temp;
temp->balance = node->balance = 0;
}

void leftRightRotate(Node* &node)
{
Node* rightChild = node;
Node* leftChild = node->left;

node = leftChild->right;
leftChild->right = node->left;
node->left = leftChild;

if(node->balance <0 )
leftChild->balance = 1;
else
leftChild->balance = 0;

rightChild->left = node->right;
node->right = rightChild;

if(node->balance <=0)
rightChild->balance = 0;
else
rightChild->balance = -1;

node->balance = 0;
}

void rightLeftRotate(Node* &node)
{
Node* leftChild = node;
Node* rightChild = node->right;

node = rightChild->left;
rightChild->left = node->right;
node->right = rightChild;

if(node->balance <= 0 )
rightChild->balance = 0;
else
rightChild->balance = -1;

leftChild->right = node->left;
node->left = leftChild;

if(node->balance <0)
leftChild->balance = 1;
else
leftChild->balance = 0;

node->balance = 0;
}

void print(Node* node)
{
if(node == NULL)
return;
print(node->left);
printf("%s %.4f\n", node->data, (double)(node->count)*100/vertexNumber);
print(node->right);
}

Node* getRoot()
{
return root;
}
};

int main()
{
AVLTree* avlTree = new AVLTree();

char* str = new char[30];
int i = 0;
while(gets(str) != NULL)
{
avlTree->insert(str);
(avlTree->vertexNumber)++;

if(++i == 12)
break;
}
avlTree->print(avlTree->getRoot());
return 0;
}


请大家帮我看一看,这里是输入字符串,当我运行时,输入到第三个字符串时,程序就不响应了。

然后我在main函数的
avlTree->insert(str);
处设置断点,并在每次运行时进入这个函数,但等到第三个插入时,便发现insert函数的stack<Node* > st;这里报错,说是什么堆被破坏了,我在别的程序里测试stack,用的都挺好的,在这里就出问题了,而且莫名其妙。
...全文
299 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
大头猫 2011-03-07
  • 打赏
  • 举报
回复
写的程序太昏了,给点注释,错了也好找。。
愤怒的熊猫007 2011-03-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 doox8086 的回复:]
看了遍有两个疑问


C/C++ code

Node(char* s)
{
data = new char[30];
int i;
for(i = 0; i < strlen(data); i++) // 这样输入方式感觉很怪
data[i] = s[i]; //一不小心 s[] ……
[/Quote]
strlen(data)该为strlen(s)??
dooX8086 2011-03-07
  • 打赏
  • 举报
回复
看了遍有两个疑问


Node(char* s)
{
data = new char[30];
int i;
for(i = 0; i < strlen(data); i++) // 这样输入方式感觉很怪
data[i] = s[i]; //一不小心 s[] 就越界了,且copy一些无用数据给 data


二. 只见 new 没看到有 delete 操作

// 用到 C 的库,都好还是 #include 相关的库
look_back 2011-03-07
  • 打赏
  • 举报
回复
看的我头晕,不过你就设那个断点能做什么?
LinuxBirdMan 2011-03-07
  • 打赏
  • 举报
回复
自己调试吧。。一步步跟踪。。这样才有进步
shuaiwang_01 2011-03-07
  • 打赏
  • 举报
回复
大家不用看程序的其他部分,在输入时,请总是输入相同的字符串。然后调试insert函数,由于字符串相同,所以只会执行insert函数的前几句。

65,189

社区成员

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

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