二叉搜尋樹

來源:互聯網
上載者:User

標籤:style   blog   color   資料   re   c   

以下是二叉搜尋樹中尋找、插入、刪除的遞迴和非遞迴演算法

資料類型設計:

1 struct BSTNode 2 {3     ElementType data;               // 結點元素值4     struct Node *leftChild;         // 左子樹根結點5     struct Node *rightChild;        // 右子樹根結點6 };

尋找資料:

 1 // 遞迴演算法 2 bool findBSTree(BSTNode *root, ElementType item) 3 { 4     if(root == NULL)                                           // 若二叉搜尋樹為空白,返回假,結束尋找 5         return false;            6     else {                                                     // 若二叉搜尋樹不為空白,則item與根結點的元素值比較 7         if(item == root->data)                                 // 若等於根結點的元素值 8             return true;            9         else if(item < root->data)           10             return findBSTree(root->leftChild, item);          // 若小於根結點的元素值,則遞迴搜尋根結點的左子樹11         else           12             return findBSTree(root->rightChild, item);         // 若大於根結點的元素值,則遞迴搜尋根結點的右子樹13     }           14 }           15            16 // 非遞迴演算法           17 bool findBSTree(BSTNode *root, ElementType item)           18 {           19     if(root == NULL)                                           // 若二叉搜尋樹為空白,則尋找失敗20         return false;           21     BSTNode *current = root;           22     while(current != NULL) {                                   /* 重複搜尋,直到葉子結點 */23         if(item == current->data)                              // 若尋找到24             return true;           25         else if(item < current->data)                          // 若目標元素小於當前結點,則在左子樹中尋找26             current = current->leftChild;           27         else                                                   // 若目標元素大於當前結點,則在右子樹中尋找28             current = current->rightChild;29     }30     return false;31 }

插入資料:


 1 // 遞迴演算法 2 void insertBSTreeNode(BSTNode *BST, ElementType item) 3 { 4     if(BST == NULL) {                                                /* 若二叉搜尋樹為空白,則新結點作為根結點插入 */ 5         BSTNode *temp = new BSTNode; 6         temp->data = item; 7         temp->leftChild = temp->rightChild = NULL; 8         BST = temp; 9     } else if(item < BST->data)                                      /* 新結點插入到左子樹中 */10         insertBSTreeNode(BST->leftChild, item);11     else                                                             /* 新結點插入到右子樹中 */12         insertBSTreeNode(BST->rightChild, item);13 }14 15 // 非遞迴演算法16 void insertBSTreeNode(BSTNode *BST, ElementType item)17 {18     BSTNode *newNode = new BSTNode;19     newNode->data = item;20     newNode->leftChild = NULL;21     newNode->rightChild = NULL;22 23     BSTNode *current = BST;24     /* 若二叉搜尋樹為空白,則新結點為根結點 */25     if(current == NULL) {            26         BST = newNode;27         return ;28     }29     /* 重複尋找插入位置,直到新結點插入 */30     while(current->leftChild != newNode && current->rightChild != newNode) {31         if(item < current->data) {                                            // 新元素小於當前比較結點值,向左子樹尋找32             if(current->leftChild != NULL)                                    // 若左孩子存在,使左孩子成為當前結點33                 current = current->leftChild;34             else35                 current->leftChild = newNode;                                 // 左孩子不存在,則新結點為當前結點的左孩子36         } else {                                                              // 新元素不小於當前比較結點,則向右子樹尋找37             if(current->rightChild != NULL)                                   // 若右孩子存在,則使右孩子成為當前結點38                 current = current->rightChild;39             else40                 current->rightChild = newNode;                                // 若右孩子不存在,則新結點為當前結點的右孩子41         }42     }43 }

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.