Data Structure
  • 資料結構自學筆記
  • 1 - Stack & Queue
    • 1.1 - Stack
    • 1.2 - Queue
    • 1.3 - Stack and Queue
  • 2 - Tree & Binary Tree
    • 2.1 - Tree
    • 2.2 - Binary Tree
    • 2.3 - Binary Tree Traversal
    • 2.4 - Binary Search Tree
    • 2.5 - Heap
    • 2.6 - Thread Binary Tree
    • 2.7 - Tree and Binary Tree Conversion
    • 2.8 Advanced Trees
      • 2.8.1 - Min-Max Heap
      • 2.8.2 - Deap
      • 2.8.3 - Symmetric Min-Max Heap
      • 2.8.4 - Extended Binary Tree
      • 2.8.5 - AVL Tree
      • 2.8.6 - M-Way Search Tree
      • 2.8.7 - B Tree
      • 2.8.8 - Red-Black Tree
      • 2.8.9 - Optimal Binary Search Tree
      • 2.8.10 - Splay Tree
      • 2.8.11 - Leftest Heap
      • 2.8.12 - Binomial Heap
  • 3 - Search & Sort
    • 3.1 - Searching
    • 3.2 - Elementary Sorting
      • 3.2.1 - Insertion Sort
      • 3.2.2 - Selection Sort
      • 3.2.3 - Bubble Sort
      • 3.2.4 - Shell Sort
    • 3.3 - Sophisticated Sorting
      • 3.3.1 - Quick Sort
      • 3.3.2 - Merge Sort
      • 3.3.3 - Heap Sort
      • 3.3.4 - Radix Sort
      • 3.3.5 - Bucket Sort
      • 3.3.6 - Counting Sort
    • 3.4 - Summary
  • 4 - Graph
    • 4.1 - Intro
    • 4.2 - Graph Traversal
    • 4.3 - Spanning Tree
      • 4.3.1 - Kruskal's algorithm
      • 4.3.2 - Prim's algorithm
      • 4.3.3 - Sollin's algorithm
    • 4.4 - Shortest Path Length
      • 4.4.1 - Dijkstra's algorithm
      • 4.4.2 - Bellman-Ford algorithm
      • 4.4.3 - Floyd-Warshall algorithm
    • 4.5 - AOV Network
    • 4.6 - AOE Network
    • 4.7 - Others
Powered by GitBook
On this page
  • 1. 定義
  • 2. 相關演算法
  1. 2 - Tree & Binary Tree

2.4 - Binary Search Tree

二元搜尋樹介紹及相關演算法

1. 定義

  • 是一個binary tree,可以為空

  • left-children < root

  • right-children > root

  • left-children和right-children都是binary search tree

     10
    /  \
   5   12
  / \    \
 3   8    15
         /
        14

2. 相關演算法

2.1 在二元搜尋樹中尋找x

  1. 若b是空樹,搜尋失敗。

  2. 若x等於b的根節點的資料域之值,尋找成功。

  3. 若x小於b的根節點的資料域之值,搜尋左子樹。

  4. 若x大於b的根節點的資料域之值,尋找右子樹。

bool SearchBST(bt_tree *bst, int x){
    if (bst != NULL){
        if (x > bst->Data) SearchBST(bt->rchild, x);
        if (x < bst->Data) SearchBST(bt->lchild, x);
        if (x == bst->Data) return True;
    }
    return False;
}

時間複雜度分析:

  1. worst case: O(n) in skewed binary tree

  2. best case: O(logn) in complete/full binary tree

2.2 在二元搜尋樹中刪除節點x

  1. 在BST中尋找x的位置

  2. 如果x是leaf,直接刪除

  3. 如果x是degree為1的節點

    1. 刪除x

    2. 將x的父點指向x的pointer指向x的子點

  4. 如果x是degree為2的節點

    1. 找到x的左子樹中的最大值/右子樹中的最小值y

    2. y回到step2/step3

void DeleteBST(bt_tree *bst, int x){
    if (bst != NULL){
        if (x > bst->Data) SearchBST(bt->rchild, x);
        if (x < bst->Data) SearchBST(bt->lchild, x);
        if (x == bst->Data) return Delete(bst);
    }
}

void Delete(bt_tree *bst){
    bt_tree *temp;
    if (bst->lchild == NULL && bst->child == NULL){
        bst = NULL;
    }
    else if (bst->lchild != NULL && bst->rchild == NULL){
        temp = bst->lchild;
        bst->Data = temp->Data;
        bst->lchild = temp->lchild;
        bst->rchild = temp->rchild;
        temp = NULL;
    }
    else if (bst->lchild == NULL && bst->rchild != NULL){
        temp = bst->rchild;
        bst->Data = temp->Data; //將子節點的值取代x
        bst->lchild = temp->lchild;
        bst->rchild = temp->rchild;
        temp = NULL;
    }
    else{ //以左子樹的最大值取代x
        bt_tree *parent = bst;
        temp = bst->lchild;
        while(temp->rchild != NULL){
            parent = temp;
            temp = temp->rchild; //左子樹的最大值
        }
        bt->Data = temp->Data;
        if (bst != parent){ 
            parent->rchild = temp->lchild;
        }
        else{  //左子樹是skewed binary tree
            parent->lchild = temp->lchild;
        }
    }
}

2.3 在二元搜尋樹中加入節點x

  1. 若是空樹,則將x作為root插入

  2. 若x小於根節點,則把x插入到左子樹中

  3. 若x大於根節點,則把x插入到右子樹中(新插入節點總是在leaf)

void insertBST(bt_tree *bst, int x){
    if (bst == NULL){
        bst->Data = x;
    }
    else{
        if (x > bst->Data){
            insertBST(bst->rchild, x);
        }
        else{
            insertBST(bst->lchild, x);
        }
    }
}
Previous2.3 - Binary Tree TraversalNext2.5 - Heap

Last updated 6 years ago