陳越-數據結構-二叉樹筆記(中)

一、二叉搜索樹

在這裏插入圖片描述

二叉搜索樹相關操作函數

  1. Find,查找
    在這裏插入圖片描述
TreeNode* Find(ElementType x,TreeNode* BST)
{
    while(BST){
        if(x > BST->data)
            BST = BST->right;
        else if(x < BST->data)
            BST = BST->left;
        else
            return BST;
    }
    return NULL;        
}    
  1. FindMin and FindMax,尋找最大最小元素
TreeNode* FindMin( TreeNode* BST)
{
    if(BST){
        while( BST->left)  BST = BST->left;
    }
    return BST;
}
  1. insert ,插入
    在這裏插入圖片描述
TreeNode* Insert( TreeNode* BST, ElementType X )
{
    if( !BST ){ /* 若原樹爲空,生成並返回一個結點的二叉搜索樹 */
       BST = new TreeNode(X);
    }
    else { /* 開始找要插入元素的位置 */
        if( X < BST->Data )
            BST->Left = Insert( BST->Left, X );   /*遞歸插入左子樹*/
        else  if( X > BST->Data )
            BST->Right = Insert( BST->Right, X ); /*遞歸插入右子樹*/
        /* else X已經存在,什麼都不做 */
    }
    return BST;
}

4.Delete,刪除
考慮有三種情況的刪除,①要刪除的是葉節點,直接刪除,並再修改其父節點指針,置爲NULL。 ②要刪除的結點有一個子節點:將其父結點的指針指向要刪除的結點的孩子結點。 ③要刪除的結點有左、右兩棵子樹:用另一結點代替被刪除結點:左子樹的最大元素(FindMax)或右子樹的最小元素(FindMin)。

TreeNode* Delete( TreeNode* BST, ElementType X ) 
{ 
    TreeNode* Tmp; 
 
    if( !BST ) 
        printf("要刪除的元素未找到"); 
    else {
        if( X < BST->Data ) 
            BST->Left = Delete( BST->Left, X );   /* 從左子樹遞歸刪除 */
        else if( X > BST->Data ) 
            BST->Right = Delete( BST->Right, X ); /* 從右子樹遞歸刪除 */
        else { /* BST就是要刪除的結點 */
            /* 如果被刪除結點有左右兩個子結點 */ 
            if( BST->Left && BST->Right ) {
                /* 從右子樹中找最小的元素填充刪除結點 */
                Tmp = FindMin( BST->Right );
                BST->Data = Tmp->Data;
                /* 從右子樹中刪除最小元素 */
                BST->Right = Delete( BST->Right, BST->Data );
            }
            else { /* 被刪除結點有一個或無子結點 */
                Tmp = BST; 
                if( !BST->Left )       /* 只有右孩子或無子結點 */
                    BST = BST->Right; 
                else                   /* 只有左孩子 */
                    BST = BST->Left;
                delete Tmp;//釋放Tmp內存
            }
        }
    }
    return BST;//每次迭代都返回當前的BST。
}

二、平衡二叉樹

在這裏插入圖片描述
在這裏插入圖片描述

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

typedef struct AVLNode *Position;
typedef Position AVLTree; /* AVL樹類型 */
struct AVLNode{
    ElementType Data; /* 結點數據 */
    AVLTree Left;     /* 指向左子樹 */
    AVLTree Right;    /* 指向右子樹 */
    int Height;       /* 樹高 */
};
 
int Max ( int a, int b )
{
    return a > b ? a : b;
}
AVLTree SingleRightRotation ( AVLTree A )
{ /* 注意:A必須有一個右子結點B */
  /* 將A與B做右單旋,更新A與B的高度,返回新的根結點B */     
 
    AVLTree B = A->Right;
    A->Right = B->Left;
    B->Left = A;
    A->Height = Max( GetHeight(A->Left), GetHeight(A->Right) ) + 1;
    B->Height = Max( GetHeight(B->Left), A->Height ) + 1;
  
    return B;
}

AVLTree SingleLeftRotation ( AVLTree A )
{ /* 注意:A必須有一個左子結點B */
  /* 將A與B做左單旋,更新A與B的高度,返回新的根結點B */     
 
    AVLTree B = A->Left;
    A->Left = B->Right;
    B->Right = A;
    A->Height = Max( GetHeight(A->Left), GetHeight(A->Right) ) + 1;
    B->Height = Max( GetHeight(B->Left), A->Height ) + 1;
  
    return B;
}
 
AVLTree DoubleLeftRightRotation ( AVLTree A )
{ /* 注意:A必須有一個左子結點B,且B必須有一個右子結點C */
  /* 將A、B與C做兩次單旋,返回新的根結點C */
     
    /* 將B與C做右單旋,C被返回 */
    A->Left = SingleRightRotation(A->Left);
    /* 將A與C做左單旋,C被返回 */
    return SingleLeftRotation(A);
}
 
AVLTree DoubleRightLeftRotation ( AVLTree A )
{ /* 注意:A必須有一個右子結點B,且B必須有一個左子結點C */
  /* 將A、B與C做兩次單旋,返回新的根結點C */
     
    /* 將B與C做左單旋,C被返回 */
	A->Right = SingleLeftRotation(A->Right);
    /* 將A與C做左單旋,C被返回 */
    return SingleRightRotation(A);
}

AVLTree Insert( AVLTree T, ElementType X )
{ /* 將X插入AVL樹T中,並且返回調整後的AVL樹 */
    if ( !T ) { /* 若插入空樹,則新建包含一個結點的樹 */
        T = (AVLTree)malloc(sizeof(struct AVLNode));
        T->Data = X;
        T->Height = 0;
        T->Left = T->Right = NULL;
    } /* if (插入空樹) 結束 */
 
    else if ( X < T->Data ) {
        /* 插入T的左子樹 */
        T->Left = Insert( T->Left, X);
        /* 如果需要左旋 */
        if ( GetHeight(T->Left)-GetHeight(T->Right) == 2 )
            if ( X < T->Left->Data ) 
               T = SingleLeftRotation(T);      /* 左單旋 */
            else 
               T = DoubleLeftRightRotation(T); /* 左-右雙旋 */
    } /* else if (插入左子樹) 結束 */
     
    else if ( X > T->Data ) {
        /* 插入T的右子樹 */
        T->Right = Insert( T->Right, X );
        /* 如果需要右旋 */
        if ( GetHeight(T->Left)-GetHeight(T->Right) == -2 )
            if ( X > T->Right->Data ) 
               T = SingleRightRotation(T);     /* 右單旋 */
            else 
               T = DoubleRightLeftRotation(T); /* 右-左雙旋 */
    } /* else if (插入右子樹) 結束 */
 
    /* else X == T->Data,無須插入 */
 
    /* 別忘了更新樹高 */
    T->Height = Max( GetHeight(T->Left), GetHeight(T->Right) ) + 1;
     
    return T;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章