2016-03-29 20 views
-1

Bir kod yapıp, Ekle işlevi düğümündeki bir Düğüm Ağacı değerine T * insertT (nodeT ** pp, int iValue) değerlerini yazdım. Değerler sırasının önemi olmadan, İşlevim, değerleri ağaçtan çıkarmak, kopyaları önlemek ve düzgün yazdırmaktır. Ben girdi değerleri sipariş değilkenİkili ağaç kodu düzgün çalışmayacak

Insert Values for Binary Tree(-1 to stop)4 6 8 10 12 14 16 -1 
The contents of the tree are: 
4 6 8 10 12 14 16 
Input a number whitin the range of the tree to receive the kth Smallest 
5 
the smallest 5th element is: 12 
count of node: 7 

Ancak, fonksiyonlar CountNode ve KTH en küçük elemanı arzu çıkışını döndürmez. Dahası, İkili Ağacın görüntüsü doğrudur. Örnek

Insert Values for Binary Tree(-1 to stop)10 14 6 8 12 4 16 -1 
The contents of the tree are: 
4 6 8 10 12 14 16 
Input a number whitin the range of the tree to receive the kth Smallest 
5 
the smallest 5th element is: 0 
count of node: 3 

için aşağıdaki fonksiyonları, kesici uç kullanılan

nodeT *insertT(nodeT **pp, int iValue) 
{ 

    if(*pp == NULL) 
    { 
     *pp = allocateNodeT(iValue); 
     return *pp; 
    } 
    if(iValue == (*pp)->iValue) 
    { 
     return *pp; 
    } 

    if(iValue < (*pp)->iValue) 
    { 
     return insertT(&(*pp)->pLeft, iValue); 
    } 
    else //(iValue > (*pp)->iValue) 
    { 
     return insertT(&(*pp)->pRight, iValue); 
    } 

} 
int k_smallest(nodeT *pRoot, int iKey) 
{ 
    int iSave; 
    if(pRoot) 
    { 
     nodeT* pTraverse; 
     pTraverse =pRoot; 
     while(pTraverse) 
     { 
      if((pTraverse->iCount +1) == iKey) 
      { 
       iSave = pTraverse->iValue; 
       break; 
      } 
      else if(iKey > pTraverse->iCount) 
      { 
       iKey = iKey-(pTraverse->iCount +1); 
       pTraverse = pTraverse->pRight; 
      } 
      else 
      { 
       pTraverse = pTraverse->pLeft; 
      } 
     } 
    } 
    return iSave; 
} 
+0

Bize ikili ağaç ve düğümlerin kodunu göstermeniz mümkün mü? – jrhee17

+0

@ jrhee17 –

cevap

0

k_smallest aşağıdaki Bu kod bir BST oluşturmak için bir çözümdür. Hata olmadan çalışır ve kodun belleği doğru şekilde ayırdığına inanıyorum. Bununla birlikte, sözde dediğim gibi, değer girmediğimde ve k_smallest ve height işlevlerini çağırdığımda, çıktı değerleri sırayla girdiğim kadar farklıdır (ilk gönderide bir örneğim var).

int main(int argc, char*argv[]) 
    { 
     char szKey; 
     int iValue, iStop, iKey, iKth, iCount, iSumPath, iK; 
     iStop = -1; 
     nodeT *t1, **t2; 
     t1 = NULL; 
     t2 = &t1; 
    Tree tree = newTree(); 

    //Ask for values for binary tree and stop with -1 
    printf("Insert Values for Binary Tree(-1 to stop)"); 
    do 
    { 
     scanf("%d", &iValue); 
     if(iValue > 0) 
     { 
      insertT(t2,iValue); 
     } 
    }while(iValue != iStop); 

    // Show contents of the tree callinf function DisplayTree 
    printf("The contents of the tree are: \n"); 
    prettyPrintT(t1, 0); 

    //input a number within range of tree for kth smallest 
    printf("\nInput a number whitin the range of the tree to receive the kth Smallest\n"); 
    scanf("%d", &iKey); 
    iKth = k_smallest(t2, iKey); 
    //Print the kth smallest element 
    printf("the smallest %dth element is: %d \n", iKey, iKth); 

    //Count the nodes 
    iCount = iheight(t2); 
    printf("count of node: %d\n", iCount); 

    printf("\n"); 
    freeNode(t2); 
} 
    /******************** allocateNodeT ******************************************** 
     NodeT *allocateNodeT(Element value)) 
     Purpose: 
      This function allocates a binary tree node, assigns the element value, 
      and initializes the pointers to NULL. 
      It returns a pointer to the newly allocated node.. 
     Parameters: 
      I Element value value where address is search. 
     Notes: 

     Return Value: 
      pNew 
     **********************************************************************/ 
     nodeT *allocateNodeT(int iValue) 
     { 
      nodeT *pNew = (nodeT*) malloc(sizeof(nodeT)); 
      pNew->iValue = iValue; 
      pNew->pLeft = NULL; 
      pNew->pRight =NULL; 
      return pNew; 
     } 
     /******************** insertT ******************************************** 
     nodeT *insertT(nodeT **pp, int iValue)) 
     Purpose: 
      insert iValue into the tree recursively 
     Parameters: 
      I nodeT *pp     Specifies the Root 
      I int iValue    value to be inserted on the tree. 
     Notes: 

     **********************************************************************/ 
     nodeT *insertT(nodeT **pp, int iValue) 
     { 

      if(*pp == NULL) 
      { 
       *pp = allocateNodeT(iValue); 
       return *pp; 
      } 
      if(iValue == (*pp)->iValue) 
      { 
       return *pp; 
      } 

      if(iValue < (*pp)->iValue) 
      { 
       return insertT(&(*pp)->pLeft, iValue); 
      } 
      else //(iValue > (*pp)->iValue) 
      { 
       return insertT(&(*pp)->pRight, iValue); 
      } 

     } 
+1

kodunun geri kalanını gönderdim. Lütfen sorduğunuz sorunun çözümü için bir çözüm olup olmadığını açıklığa kavuşturmak için bir metin ekleyin ya da sorunun bir parçası olarak daha fazla kod gönderiyor musunuz? Bir yoruma yanıt olarak daha fazla kod gönderiyorsanız, lütfen orijinal soruyu düzenleyin ve bir yanıtta yayınlamak yerine ekleyin. – gariepy

+0

@gariepy bu metni daha iyi açıklıyor mu? –

İlgili konular