二叉線索樹實現

LZEP2015發表於2014-10-23

直接上程式碼:



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

typedef int ElemType;
typedef struct BSTree
{
     ElemType data;
     struct BSTree* left;
     struct BSTree* right;
     struct BSTree* parent;
}BSTRee,*BS_Tree;

void Insert_BSTree (BS_Tree *T,BS_Tree parent,ElemType e)
{
     if((*T)==NULL)
     {
         BS_Tree temp=(BS_Tree)malloc(sizeof(BSTree));
         temp->data=e;
         temp->left=NULL;
         temp->right=NULL;
         temp->parent=parent;
         *T=temp;
     }
     else
     {
         if((*T)->data==e)
         {
           return ;
         }
         else
         {
             if((*T)->data<e)
             {
                Insert_BSTree(&((*T)->right),*T,e);
             }
             else
                Insert_BSTree(&((*T)->left),*T,e);
         }
     }
}
void Print_BSTree(BS_Tree T)
{
     if(!T) return;
     else
     {
        printf("%d ",T->data);
        Print_BSTree(T->left);
        Print_BSTree(T->right);
     }
}

bool state=false;
bool Search(BS_Tree T,ElemType e)
{
     if(T)
     {
       if(T->data==e)
       {
           printf("T->data=%d,e=%d\n",T->data,e);           
           state=true;
       } 
       else
       {
         if(T->data<e)
         Search(T->right,e);
         else
         Search(T->left,e);  
       }
     }
    // else
     //{
         return state;
     //}
}
int Tree_Height(BS_Tree T)
{
    if(!T)
    return -1;
    else
    {
        int left_height=Tree_Height(T->left);
        int right_height=Tree_Height(T->right);
        return ((left_height>right_height)?(left_height+1):(right_height+1));
    }
}
int Tree_Node_Num(BS_Tree T)
{
    static int num=0;
    if(T)
    {
      num=num+1;
      Tree_Node_Num(T->left);
      Tree_Node_Num(T->right);
    }
    return num;
}
void Get_Parent_Node(BS_Tree T,ElemType e)
{
   if(!T)
   {
      printf("parent node is null\n");
      return ;
   }
   else
   {
       if(T->data==e)
       {
         if(T->parent)
         {
         printf("parent node is not null,data=%d\n",T->parent->data);
         if(T->parent->data<e)
         printf("right node data=%d\n",e);
         else
         printf("left node data=%d\n",e);
         }
         else
         printf("parent node is null\n");
         return ;
       }
       else
       {
           if(T->data<e) Get_Parent_Node(T->right,e);
           else Get_Parent_Node(T->left,e);
       }
   }
}
void add_brackets(BS_Tree T)
{
     if(T)
     {
          printf("%d[",T->data);
          add_brackets(T->left);
          printf(",");
          add_brackets(T->right);
          printf("]");
     }
     else
     {
          printf("#");
     }
}
BS_Tree Tree_Minimum(BS_Tree x)
{
  while(x->left)
   x=x->left;
  return x;
}
BS_Tree Tree_Maximum(BS_Tree x)
{
  while(x->right)
   x=x->right;
  return x;
}
BS_Tree Tree_Successor(BS_Tree T)
{
  if(T->right!=NULL)
  {
     return Tree_Minimum(T->right);
  } 
  BS_Tree y=T->parent;
  
  while(y!=NULL&&T==y->right) 
  {
     T=y;
     y=y->parent;
  } 
  return y;
}
BS_Tree Tree_Predecessor(BS_Tree T)
{
  if(T->left!=NULL)
  {
     return Tree_Maximum(T->left);
  } 
  BS_Tree y=T->parent;
  if(y!=NULL&&T==y->right) 
  {
     return y;
  } 
  else
     return NULL;
}
void delete_element(BS_Tree *T,ElemType e)
{
   //1. 不含有子節點
   //2. 含有一個子節點
   //3. 含有2個子節點  
}
int main(int argc, char *argv[])
{
    BS_Tree T=NULL;
    int A[10]={4,1,5,2,6,8,3,7,10,9};
    //int A[10]={2,4,4,4,4,4,4,4,5,4};
    printf("start create BSTree\n");
    for(int i=0;i<10;i++)
    {
       Insert_BSTree(&T,NULL,A[i]);
    }
    printf("finish create BSTree\n");
    printf("start print!\n");
    Print_BSTree(T);
    printf("\nfinish print!\n");
    
    printf("start search\n");
    bool ret=Search(T,2);
    if(ret) printf("search success\n");
    else printf("search failed\n");
    printf("Tree Height:\n");
    int height=Tree_Height(T);
    printf("%d\n",height);
    printf("Finish Compute Tree Height:\n");
    printf("Tree Node Num:\n");
    int num=Tree_Node_Num(T);
    printf("%d\n",num);
    printf("Finish Compute Tree Node num\n");
    printf("get parent node\n");
    Get_Parent_Node(T,2);
    printf("finish get parent node\n");
    printf("add brackets\n");
    add_brackets(T);
    printf("\nfinish add brackets\n");
    printf("start compute Predecessor:\n"); 
    BS_Tree p=Tree_Predecessor(T->left);
    if(p==NULL)
    printf("NO Predecessor\n");
    else
    printf("Predecessor data is %d\n",p->data);
    
    printf("start compute successor:\n"); 
    p=Tree_Successor(T->right->right->right->right);
    if(p==NULL)
    printf("No successor\n");
    else
    printf("successor data is %d\n",p->data);
    
    system("PAUSE");
    return EXIT_SUCCESS;
}



相關文章