2014-04-29 3 views
-1

트리의 각 노드의 부모 및 형제에 대해 인쇄하는 데 몇 가지 문제가 있습니다. 다음은 코드의 전체 블록바이너리 정렬 트리 - 메모리 위치에서 값 가져 오기

/* 
--------------- 
Binary sort tree 
*/ 

#include <iostream> 
#include <cstdlib> 
#include <cstdint> 
using namespace std; 

class BinSearchT 
{ 
    private: 
     struct tr_node 
     { 
      tr_node* left; 
      tr_node* right; 
      tr_node* parent; 
      int data; 
     }; 
     tr_node* root; 
    public: 
     BinSearchT() 
     { 
      root = NULL; 
     } 
     bool isEmpty() const { return root==NULL; } 
     void pInorder(); 
     void inorder(tr_node*); 
     void insert(int); 
     void remove(int); 
}; 


void BinSearchT::insert(int d) 
{ 
    tr_node* t = new tr_node; 
    //tr_node* parent; 
    t->data = d; 
    t->left = NULL; 
    t->right = NULL; 
    t->parent = NULL; 
    // is this a new tree? 
    if(isEmpty()) root = t; 
    else 
    { 
    //Note: ALL insertions are as leaf nodes 
    tr_node* curr; 
    curr = root; 
    // Find the Node's parent 
    while(curr) 
    { 
     t->parent = curr; 
     if(t->data > curr->data) curr = curr->right; 
     else curr = curr->left; 
    } 

    if(t->data < t->parent->data) 
     t->parent->left = t; 
    else 
     t->parent->right = t; 
    } 
} 



void BinSearchT::pInorder() 
{ 
    inorder(root); 
} 

void BinSearchT::inorder(tr_node* p) 
{ 
    if(p != NULL) 
    { 
     if(p->left) inorder(p->left); 
     //int left=*reinterpret_cast<int *>(p->left); 
     //int right=*reinterpret_cast<int *>(p->right); 
     //int parent=*reinterpret_cast<int *>(p->parent); 
     cout<<"\n "<<p->data<<" Left: "<<p->left->data<<" Right: "<<p->right->data<<" Parent: "<<p->parent->data<<endl; 


     if(p->right) inorder(p->right); 
    } 
    else return; 
} 

int main() 
{ 
    BinSearchT b; 
    int ch,tmp,tmp1; 
    while(1) 
    { 
     cout<<endl<<endl; 
     cout<<" BinSearchTOps "<<endl; 
     cout<<" ----------------------------- "<<endl; 
     cout<<" 1. Insertion/Creation "<<endl; 
     cout<<" 2. In-Order Traversal "<<endl; 
     cout<<" 3. Exit "<<endl; 
     cout<<" Enter your choice : "; 
     cin>>ch; 
     switch(ch) 
     { 
      case 1 : cout<<" Enter Number to be inserted(just one) : "; 
        cin>>tmp; 
        b.insert(tmp); 
        break; 
      case 2 : cout<<endl; 
        cout<<" In-Order Traversal "<<endl; 
        cout<<" -------------------"<<endl; 
        b.pInorder(); 
        break; 
      case 3 : system("pause"); 
        return 0; 
        break; 
     } 
    } 
} 

그래서 난 데 문제는 값이 모두 0 출력한다 null이 있어야하는 경우 부모와 형제 자매가 메모리 위치로 밖으로 인쇄하고 있는지이다 저를 그것이 작동하고 있으며, 그 기억 장소를 얻는 방법을 찾아야한다고 생각하게 만듭니다.

지금 나는이 내가

//int left=*reinterpret_cast<int *>(p->left); 
//int right=*reinterpret_cast<int *>(p->right); 
//int parent=*reinterpret_cast<int *>(p->parent); 

을 시도했다 작동하지 않았기 때문에 내가 프로그램에서 그 부분을 주석 것입니다 몇 가지 검색을하고 유래 C++ - Get value of a particular memory address

에이 게시물을 발견했다. 내가 얻을 수있는 도움을 주시면 감사하겠습니다. 입력 : 10 20 8 4 5 15 17 2 출력을 게시 할 수 없으므로 포스트 이미지가 없습니다.

편집 : OUTPUT

2 Left: 00000000 Right:00000000 Parent:00484C58 
4 Left: 00484F98 Right:00484CA8 Parent:00484C08 
5 Left: 00000000 Right:00000000 Parent:00484C58 
8 Left: 00484C58 Right:00000000 Parent:00484BB8 
10 Left: 00484C08 Right:00484EF8 Parent:00000000 
15 Left: 00000000 Right:00484F48 Parent:00484BB8 
17 Left: 00000000 Right:00000000 Parent:008A4F48 
20 Left: 008A4F48 Right:00000000 Parent:008A4BB8 

그러나 숫자가 메모리 위치와 일치 이제까지 또한 단지 필요가 작동하고 있다는 사실을 지원하는 일치하는 곳에서 볼 수 있듯이 그것은

2 Left: Null  Right:Null  Parent:4 
4 Left: 2  Right:5  Parent:8 
5 Left: Null  Right:Null  Parent:4 
8 Left: 4  Right:Null  Parent:10 
10 Left: 8  Right:20  Parent:Null  
15 Left: Null  Right:17  Parent:20 
17 Left: Null  Right:Null  Parent:15 
20 Left: 15  Right:Null  Parent:10 

해야한다 숫자가 아닌 메모리 주소를 얻으려면. 당신은 단지 포인터가 유효한지 여부를 확인하지 않는

:이 대답처럼 파일의 맨 위에있는

+0

왜 당신은 그냥 읽을 것'P-> data' 방금 그 위치로 INT를 쓴 후? 'reinterpret_cast (p-> 왼쪽)'이해야 할 것은 무엇입니까? –

+0

여기에 쓰여진 내용에서 알 수 있듯이 -> http://stackoverflow.com/questions/12298208/c-get-value-of-a-particular-memory-address 메모리 주소를 추측한다고 가정합니다. – user3348907

+0

주석 처리 된 코드가 인쇄 된 첫 번째 줄은 무엇입니까? – Matthew

답변

1

추가 #include <iomanip>은 출력 형식입니다. 자식 노드에 대한 포인터가 NULL이면 아직 할당 된 메모리가 없으므로 data 속성에 액세스 할 수 없습니다.

또한 간단한 포인터 대신 메모리의 원시 주소에 액세스해야한다는 것을 알고 있다면 실수/잘못된 방법으로 수행하는 것이 가장 가능성이 높습니다.

자식 노드/부모 노드가 null인지 아닌지 확인하기 만하면됩니다.

setw(4)left 너비를 4로 설정하고 출력의 왼쪽 정렬을 지정합니다. 삽입 후

#include <iomanip> 

... 

void BinSearchT::inorder(tr_node* p) 
{ 
    if(p != NULL) 
    { 
     if(p->left) inorder(p->left); 

     if (p->left) 
      cout << "Left: " << setw(4) << left << p->left->data << "\t"; 
     else 
      cout << "Left: Null\t"; 

     if (p->right) 
      cout << "Right: " << setw(4) << left << p->right->data << "\t"; 
     else 
      cout << "Right: Null\t"; 

     if (p->parent) 
      cout << "Parent: " << setw(4) << left << p->parent->data << endl; 
     else 
      cout << "Parent: Null" << endl; 

     if(p->right) inorder(p->right); 
    } 
    else 
     return; 
} 

:

BinSearchTOps 
----------------------------- 
1. Insertion/Creation 
2. In-Order Traversal 
3. Exit 
Enter your choice : 2 

In-Order Traversal 
------------------- 
Left: Null Right: Null Parent: 4 
Left: 2  Right: 5 Parent: 8 
Left: Null Right: Null Parent: 4 
Left: 4  Right: Null Parent: 10 
Left: 8  Right: 20 Parent: Null 
Left: Null Right: 17 Parent: 20 
Left: Null Right: Null Parent: 15 
Left: 15 Right: Null Parent: 10 
+0

감사합니다. 교수님과 이야기 할 수 있었고, 그녀는 제가 누락 된 데이터와 Null을 확인하지 않은 것과 같은 것을 지적했습니다. 도움에 감사드립니다. – user3348907

0

명확 코드의이 부분에서 문제가 있습니다 p->left가 null

if(p->left) inorder(p->left); 
     //int left=*reinterpret_cast<int *>(p->left); 
     //int right=*reinterpret_cast<int *>(p->right); 
     //int parent=*reinterpret_cast<int *>(p->parent); 
     cout<<"\n "<<p->data<<" Left: "<<p->left->data<<" Right: "<<p->right->data<<" Parent: "<<p->parent->data<<endl; 

경우 코드가 독방 감금 오류를 줄 것이다 또는 다른 이상한 행동. 귀하의 코드에 좋은 테스트를 추가하면 그것은 저에게 효과적입니다 ...예를 들어 :

if (p->left && p->right) 
cout<<"\n "<<p->data<<" Left: "<<p->left->data<<" Right: "<<p->right->data<<" Parent:" <<p->parent->data<<endl; 

또는

if(p->left) { 
    std::cout << " Left: "<<p->left->data<< std::endl ; 
    inorder(p->left); 
} 

    if(p->right) { 
    std::cout << " Right: "<<p->right->data<< std::endl ; 
    inorder(p->right) ; 
}