#include "tree.h"
#include <iostream>
#include <fstream>
using namespace std;
int counter;
tree::tree()
{
root = NULL;
}
int tree::height(node *temp)
{
int h = 0;
if (temp != NULL)
{
int l_height = height (temp->left);
int r_height = height (temp->right);
int max_height = max (l_height, r_height);
h = max_height + 1;
}
return h;
}
int tree::difference(node *temp)
{
int l_height = height (temp->left);
int r_height = height (temp->right);
int sum= l_height - r_height;
return sum;
}
void tree::READ_DATA()
{
ifstream f;
f.open("input.txt", ios::in);
while(true)
{
int a;
int b;
f >> a;
f >> b;
root = insert(root,a,b);
if (f.eof())
{
break;
}
}
f.close();
}
node *tree::insert(node *root,int x,int b)
{
if (root == NULL)
{
root = new node;
root->data = x;
root->left = NULL;
root->right = NULL;
root->linked = NULL;
cout<<root->data<<endl;
root = link(b,root);
return root;
}
else if(x < root->data)
{
root->left = insert(root->left,x,b);
root = balance(root);
}
else if(x > root->data)
{
root->right = insert(root->right,x,b);
root = balance(root);
}
else if (x == root->data)
{
root = link(b,root);
}
return root;
}
void inorder(node *root)
{
if (root==NULL)
{
return;
}
inorder(root->left);
ofstream k;
k.open("output.txt",ios::app);
k<<root->data<<" ";
while (true)
{
if (root->linked != NULL)
{
root = root->linked->left;
}
else
{
while (root->linked->right != NULL)
{
cout << root->linked->data;
root = root->linked->right;
}
break;
}
}
cout << endl;
k.close();
inorder(root->right);
}
void tree::WRITE_INDEX()
{
inorder(root);
}
//left rotate
node *tree::l_rotation(node *parent)
{
//cout << "tost"<<endl;
node *temp;
temp = parent->left;
parent->left = temp->right;
temp->right = parent;
return temp;
}
//right rotate
node *tree::r_rotation(node *parent)
{
//cout << "krepa"<<endl;
node *temp;
temp = parent->right;
parent->right = temp->left;
temp->left = parent;
return temp;
}
//right-left rotate
node *tree::rl_rotation(node *parent)
{
//cout << "rl"<<endl;
node *temp;
temp = parent->right;
parent->right = l_rotation(temp);
return r_rotation(parent);
}
//left-right rotate
node *tree::lr_rotation(node *parent)
{
//cout << "lr"<<endl;
node *temp;
temp = parent->left;
parent->left = r_rotation(temp);
return l_rotation(parent);
}
//balancing the tree
node *tree::balance(node *temp)
{
int diff = difference(temp);
if (diff > 1)
{
if (difference(temp->left)>0)
temp = l_rotation(temp);
else
temp = lr_rotation(temp);
}
else if (diff < -1)
{
if (difference(temp->right) > 0)
temp = rl_rotation(temp);
else
temp = r_rotation(temp);
}
return temp;
}
node *tree::link(int b,node *temp)
{
if (temp->linked == NULL)
{
temp->linked = new node;
temp->linked->left = NULL;
temp->linked->right = NULL;
temp->linked->data = b;
return temp;
}
else
{
cout << "tost";
link2(b,temp->linked);
}
}
node *tree::link2(int b,node *temp)
{
cout << b;
if (b > temp->data)
{
link2(b,temp->right);
}
else if (b < temp->data)
{
link2(b,temp->left);
}
else
{
temp = new node;
temp->left = NULL;
temp->right = NULL;
temp->data = b;
return temp;
}
}
AVL 트리에 정보를 저장해야하는이 함수가 있습니다. 주에서 나는 READ_DATA와 WRITE_INDEX를 호출한다 link2가 실행될 때 "if (b> temp-> data)"세그먼트 화 오류가 발생한다. 도움이 되었습니까?AVL 트리 자식 프로젝트의 분할 오류
명백한 이유는 'temp'가 유효하지 않은 포인터 값이라는 것입니다. 컴파일러와 함께 제공되는 디버거를 사용하여 왜 그런 식으로 끝내야하는지 판단해야합니다. – PaulMcKenzie
글쎄,이 문제를 해결하기 위해 일부 인쇄물을 사용해 보았는데 모든 것이 완벽하게 괜찮아 보인다. –
"print"문이 아닌 디버거를 사용한다. 프로그램 실행시 'temp'가 유효하지 않다는 것을 보여주고있다. 그 포인터가 NULL 또는 "마술"나쁜 값이 아닌 한, 값을보고 포인터가 유효한지 여부를 알 수있는 방법이 없습니다. "불량"포인터 값은 "양호"값만큼 유효 할 수 있습니다. – PaulMcKenzie