2011-12-05 4 views
0

시도 할 때 문제가 발생했습니다. (1) 원본 트리 루트의 왼쪽 하위 트리에서 가장 큰 정보 필드를 찾습니다. (2) 원래 트리의 루트의 오른쪽 하위 트리.원본 트리의 하위 트리

내 코드가 컴파일되지만 실행시 오류가 있으며 maxleftsubtree() 및 minrightsubtree() 함수에서 어떤 일이 발생하는지 잘 모르겠습니다. 모든 제안을 부탁드립니다.

내 현재 코드 : 당신의 maxleftsubtree (maxtrightsubtree) 함수에 오류가 있습니다

#include <iostream> 
#include <string> 

using namespace std; 

class Tnode { 
    public: 
     Tnode *left; 
     string info; 
     Tnode *right; 
     Tnode(string info = "", Tnode* left = NULL, Tnode* right = NULL) : 
      info(info), left(left), right(right) {} 
}; 
class BST { 
    public: 
     BST() : theroot(NULL) {} 
     void insert(string x); 
     void inorderprint(); 
     void preorderprint(); 
     void postorderprint(); 
     void maxstring(); 
     void minstring(); 
     void maxleftsubtree(); 
     void minrightsubtree(); 
    private: 
     void inorderprint(Tnode *p); 
     void preorderprint(Tnode *p); 
     void postorderprint(Tnode *p); 
     void maxstring(Tnode *p); 
     void minstring(Tnode *p); 
     void maxleftsubtree(Tnode *p); 
     void minrightsubtree(Tnode *p); 
     void insertleft(Tnode *place, string newval); 
     void insertright(Tnode *place, string newval); 
     Tnode *theroot; 
}; 

// add a new node (with x as info) to tree that has theroot as root 
void BST::insert(string x) 
{ 
    // if the tree is initially empty, put x at the root 
    if (theroot==NULL) { 
     theroot = new Tnode(x); 
     return; 
    } 

    Tnode *p, *q; 

    // otherwise, find where x belongs in the tree 
    p = theroot; 
    q = theroot; 
    while (q != NULL) { 
     p = q; 
     if (x < p-> info) 
      q = p-> left; 
     else 
      q = p-> right; 
    } 

    // to get here, we found the correct place to store x, 
    // as a child of node p  Q: is it left or right? 

    if (x < p-> info) 
     insertleft(p,x); 
    else 
     insertright(p,x); 

    return; 

} 

//insert a new node (with info newval) as left child of place 
void BST::insertleft(Tnode *place, string newval) 
{ 
    Tnode *p = new Tnode(newval); 

    place -> left = p; 
    return; 

} 

//insert a new node (with info newval) as right child of place 
void BST::insertright(Tnode *place, string newval) 
{ 
    Tnode *p = new Tnode(newval); 

    place -> right = p; 
    return; 

} 
...................... 
............... 
............... 

// 
// 
void BST::maxleftsubtree() 
{ 
    maxleftsubtree(theroot); 
} 

// 
// 
void BST::minrightsubtree() 
{ 
    minrightsubtree(theroot); 
} 

..................................... 
................................. 
......................... 

// 
// 
void BST::maxleftsubtree(Tnode *p) 
{ 
    while (p -> left) 
     p = p -> right; 
    cout << p -> info << " \n"; 
    return; 
} 

// 
// 
void BST::minrightsubtree(Tnode *p) 
{ 
    while (p -> right) 
     p = p -> left; 
    cout << p -> info << " \n"; 
    return; 
} 
+0

어떤 오류가 있습니까? – Beginner

+0

제안 사항으로, 하위 트리에 최대 및 최소 요소를 찾는 함수를 작성한 다음 루트의 왼쪽 및 오른쪽 하위 요소를 각각 호출하는 것이 더 쉽습니다. –

+0

오류는 "Assign6_BST.exe가 작동을 멈췄습니다"라고 말합니다. – 123me

답변

1

. 먼저 루트의 왼쪽 (오른쪽) 하위 트리를 선택하고 오른쪽 (왼쪽) 분기를 따라 끝까지 이동해야합니다. 다음은 수정 된 버전입니다.

void BST::maxleftsubtree(Tnode *p) 
{ 
    Tnode* left = NULL; 
    if (p != NULL) { 
     left = p->left; 
    } 
    if (left != NULL) { 
     while (left->right) 
      left = left -> right; 
     cout << left -> info << " \n"; 
    } 
    return; 
} 
+0

함수의 첫 번째 코드 줄에서 원래 루트를 가리키는 p를 트리의 왼쪽 노드에 할당하고 있습니까? – 123me

+0

@ 123me 아니요, p가 NULL이 아닌 경우 p의 왼쪽 노드를 포인터 "left"에 할당 할 것입니다. –

+0

그래, 나 혼란스러워했다. 첫 번째 코드 줄을 작성하는 대신에 – 123me

관련 문제