Binary search tree

1.特 性

  • L < V < R (Inoder traversal)

  • 執行查找, 刪除, 插入等操作的時間複雜度為O(log n)

    • 一個由N個節點組成, 隨機構造的BST其高度為logN

    • 但是一棵具有N個節點的線性鏈, 則此操作最壞情況運行時間為O(N)

2.操作

  • Search

TreeNode* BST::Search(int KEY){
           TreeNode *current = root;               // 將curent指向root作為traversal起點
           while (current != NULL && KEY != current->key) {
           // 兩種情況跳出迴圈:
           // 1.沒找到 2.有找到
           if (KEY < current->key{
               current = current->leftchild;   // 向左走
           } else {
               current = current->rightchild;  // 向右走
           }
       }
       return current;
   }
  • Insert

Last updated