2017-03-24 1 views
-1

이다 내가 주에서이 함수를 (호출 할)하지만 문제는 간단한에서 노드 유형 인수 를받는 동안 어떤 매개 변수 내가 전달해야한다는 것입니다 함수를 호출하는 방법을 모른다 나는 단지 내 나무의 높이를보고 싶다. 그래서 높이는 노드 유형 매개 변수를 필요로의 함수 호출시 직면하는 오류를 제거, 나를 도와 그리고 내가 통과해야하는지 아무 생각하시기 바랍니다. 여기에 전체 코드는내가 그 매개 변수 노드 유형

#include <iostream> 
using namespace std; 
// Binary Search Tree 
// 
class Node 
{ 
    public: 
      int data; 
      Node *left,*right; 
      Node(Node *l=NULL ,int d=0, Node *r=NULL) 
      { 
      left=l; 
      data=d; 
      right=r; 
      } 
}; 

// Binary Search Tree 
class Tree 
{ 
    Node *root; 

    public : 
     Tree() 
     { 
     root=NULL; 
     } 

     bool isEmpty() 
     { 
     if(root==NULL) 
      return true; 
     else 
      return false; 
     } 
     ///////////////////////////////////////////////////////////////////////////////////////////////// 
     // insert funcation 
     ///////////////////////////////////////////////////////////////////////////////////////////////// 
     void insert(int val) 
     { 
     if(isEmpty()) 
     { 
      root=new Node(NULL,val,NULL); 

     } 
     else if(val < root->data && root->left==NULL) 
     { 
      Node *p=new Node(NULL ,val,NULL); 
      root->left=p; 
     } 
     else if(val > root->data && root->right==NULL) 
     { 
      Node *p=new Node (NULL ,val,NULL); 
      root->right=p; 
     } 

     else if(val < root->data) 
     insert(val ,root->left); 
     else 
     insert(val ,root->right); 
     } 
     //////////////////////////////////////// 
     void insert(int val,Node *n) 
     { 
     if(val>n->data && n->right==NULL) 
     { 
      Node *p=new Node(NULL,val,NULL); 
      n->right=p; 
     } 
     else if(val > n->data) 
      insert(val,n->right); 
     else if(val <n->data && n->left==NULL) 
     { 
      Node *p=new Node(NULL,val,NULL); 
      n->left=p; 
     } 
     else 
      insert(val,n->left); 
     } 
     ////////////////////////////////////////////////////////////////////////////////////// 
     // pre Order all data display 
     ////////////////////////////////////////////////////////////////////////////////////// 

     void preOrder(void) 
     { 
     if(isEmpty()) 
     cout<<"Tree is Empty\n"; 
     else 
      preOrder(root); 
     } 
     void preOrder(Node *n) 
     { 
     if(n!=NULL) 
     { 
      cout<<n->data<<endl; 
      preOrder(n->left); 
      preOrder(n->right); 
     } 
     } 
     ////////////////////////////////////////////////////////////////////////////////////// 
     //   in fix Order all data display 
     ////////////////////////////////////////////////////////////////////////////////////// 
     void inOrder() 
     { 
     if(isEmpty()) 
     cout<<"Tree is Empty\n"; 
     else 
      inOrder(root); 
     } 
     void inOrder(Node *n) 
     { 
     if(n!=NULL) 
     { 
      inOrder(n->left); 
      cout<<n->data<<endl; 
      inOrder(n->right); 
     } 
     } 
    ////////////////////////////////////////////////////////////////////////////////////// 
    // post Order all data display 
    ////////////////////////////////////////////////////////////////////////////////////// 

     void posOrder() 
     { 
     if(isEmpty()) 
     cout<<"Tree is Empty\n"; 
     else 
      posOrder(root); 
     } 
     void posOrder(Node *n) 
     { 
     if(n!=NULL) 
     { 
      posOrder(n->left); 
      posOrder(n->right); 
      cout<<n->data<<endl; 
     } 
     } 

     ///////////////////////////////////////////////////////////////////////////////////////////////// 
     //     Search funcation 
     ///////////////////////////////////////////////////////////////////////////////////////////////// 
     void search(int val) 
     { 
     if(isEmpty()) 
        cout<<"Tree is Empty\n"; 
     else 
      search(val,root); 
     } 
     void search(int v,Node *p) 
     {  
      if(v==p->data) 
        cout<<"val : "<<p->data<<endl;   
      else if(v < p->data && p->left!=NULL) 
        search(v,p->left); 
      else if(v>p->data && p->right!=NULL) 
       search(v,p->right); 
      else 
        cout<<"Data Not Found \n"; 
     } 

    Node *l; 
    int deleteKey(int val) 
    { 
     if(isEmpty()) 
     cout<<"Tree is Empty\n"; 
     else if(root->data==val &&(root->left==NULL&&root->right==NULL)) 
     { 
     int temp=root->data; 
     delete root; 
     return temp; 
     } 
     else 
     deleteKey(val,root); 
     } 

     int deleteKey(int v,Node *p) 
     { 
      if(v == p->data) 
      { 

        if(p->left==NULL && p->right==NULL) 
        { 
        if(l->right==p) 
        { 
         int temp=p->data; 
         delete p; 
         l->right=NULL; 
         return temp; 
        } 
        else 
        { 
         int temp=p->data; 
         delete p; 
         l->left=NULL; 
         return temp; 
        } 
        } 
        else if(p->right!=NULL) 
        { 
         int temp=p->data; 
        deleteKey(p,p->right); 
        return temp; 
        } 
        else 
        { 
        int temp=p->data; 
         cout<<"Left : "<<p->data<<endl; 
        deleteKey(p,p->left,v); 
        return temp; 
        } 
      } 
      else if(v < p->data && p->left!=NULL) 
      { 
        l=p; 
        deleteKey(v,p->left); 
      } 
      else if(v>p->data &&p->right!=NULL) 
      {  
        l=p; 
       deleteKey(v,p->right); 
      } 
      else 
        cout<<"Data Not Found ----\n"; 
     } 

     int deleteKey(Node *find ,Node *next) 
     { 
      if(next->left == NULL && next->right != NULL) 
      { 
       find->data = next->data; 
       deleteKey(find->right , next->right); 
      } 
      else if(next->left == NULL&& next->right==NULL) 
      { 
       find->data = next->data; 
       delete next; 
       find->right=NULL; 
      } 
      else 
      { 
       Node *q; 
       while(next->left!=NULL) 
       { 
        q=next; 
        next=next->left; 
       } 
       find->data=next->data; 
       delete next; 
       q->left=NULL; 
      } 
     } 
      int deleteKey(Node* find,Node *next,int v) 
     { 
      if(next->right == NULL && next->left != NULL) 
      { 
       find->data = next->data; 
       deleteKey(find->left , next->left,v); 
      } 
      else if(next->right == NULL&& next->left==NULL) 
      { 
       find->data = next->data; 
       delete next; 
       find->left=NULL; 
      } 
      else 
      { 
       Node *q; 
       while(next->right!=NULL) 
       { 
        q=next; 
        next=next->right; 
       } 
       find->data=next->data; 
       delete next; 
       q->right=NULL; 
      } 
     } 

     ~Tree() 
     { 
     dist(); 
     } 

      void dist() 
      { 
       dist(root); 
      } 
       void dist(Node *n) 
       { 
       if(n!=NULL) 
       { 
        dist(n->left); 
        dist(n->right); 
        delete n; 
       } 
      } 
      int height(Node *root) 
      { 
       int h=0; 
       if (isEmpty()) 
       { 
        cout<<"Tree is Empty\n";  
       } 
       else 
       { 
        int left_height=height(root->left); 
        int right_height=height(root->right); 
        h=1+max(left_height, right_height); 

       } 
       return h; 
      } 

}; 

int main() 
{ 
    Tree obj; 
    obj.height(); 
} 

답변

1

그럼이 기능에, 당신의 트리의 루트를 통과하지만, 더 좋은 방법은 당신이 어떤 매개 변수없이 또 하나 개의 기능을 할 경우하고 그 기능을 공개하고이를 호출입니다 this-> root를 전달하여 private 함수를 호출한다.

public: 
int getHeight() 
{ 
return height(this->root); //pass your Tree class root 
} 

및 효율성을 위해, 수업 시간에 그 기능이 비공개 :

은 여기에서 볼 수 있습니다.

또 다른 접근법은 클래스에서 getRoot() 함수를 만들고 main에서 Tree 클래스의 Root를 얻고 height 함수에 전달하는 것입니다. 그러나 첫 번째 방법이 더 좋을 것입니다.

+0

선생님 나는이 obj.height 같은 INT 주에서이 함수를 호출하고 (-); 괄호 안에 무엇을 전달해야합니까? 감사합니다. –

+0

'Tree'라는 클래스가 있다고 가정하면 (OP는 그러한 주장을하지 않았습니다), 이것은 좋은 길일 것입니다. OP가 그러한 클래스를 가지고 있지 않다면, 그 답은 단순히'Node *'가 트리를 뿌리 내리는 것만 전달하면된다. 관련이 없으므로 노드 포인터 매개 변수는 이상적으로'const'이어야합니다. 아무것도 바뀌지 않으므로 그렇게 말하십시오. – WhozCraig

+0

@MaharAsifLak는 '루트'포인터를 호스팅하는 "트리"클래스의 인스턴스 인'obj'입니까? 그렇다면이 대답은 완벽합니다. – WhozCraig