二叉尋找樹(4),二叉尋找樹

來源:互聯網
上載者:User

二叉尋找樹(4),二叉尋找樹
假設樹的節點定義如下,尋找一個指定值的前驅以及後繼節點。如果樹中沒有找到指定值,則返回它所在區間的邊界值。

structNode{   intkey;   Node *left,*right ;};

下面是實現此操作的演算法,採用遞迴:

輸入: 根節點, 鍵值輸出: 前驅節點,後繼節點1. If root is NULL      then return2. if key is found then    a. If its left subtree is not null        Then predecessor will be the right most         child of left subtree or left child itself.    b. If its right subtree is not null        The successor will be the left most child         of right subtree or right child itself.    return3. If key is smaller then root node        set the successor as root        search recursively into left subtree    else        set the predecessor as root        search recursively into right subtree

下面是基於上面演算法的C++代碼實現:

// BST中尋找前驅和後繼的C++程式#include <iostream>struct Node{int key;Node *left;Node *right;};//在BST中尋找指定值的前驅和後繼,分別儲存在pre和suc中。void findPreSuc(Node* root, int key, Node*& pre, Node*& suc){// 空樹if (root == NULL)return;// 找到匹配的節點if (root->key == key){// 左子樹中最大值為前驅if (root->left != NULL){Node* tmp = root->left;while (tmp->right)tmp = tmp->right;pre = tmp;}// 右子樹中最小值為後繼if (root->right != NULL){Node* tmp = root->right;while (tmp->left)tmp = tmp->left;suc = tmp;}return;}// 指定值小於根節點值,繼續尋找左子樹if (root->key > key){suc = root;findPreSuc(root->left, key, pre, suc);}else //尋找右子樹{pre = root;findPreSuc(root->right, key, pre, suc);}}// 建立一個新的BST節點Node *createNewNode(int item){Node *temp = new Node;temp->key = item;temp->left = temp->right = NULL;return temp;}//插入新節點至二叉搜尋樹中Node* insert(Node* node, int key){//空樹if (node == NULL)return createNewNode(key);//遞迴插入。如果已存在指定值,則不插入if (key < node->key)node->left = insert(node->left, key);else if (key > node->key)node->right = insert(node->right, key);//返回未修改的node指標return node;}// 中序遍曆二叉搜尋樹void inorder(Node *root){if (root != NULL){inorder(root->left);std::cout << " " << root->key << " ";inorder(root->right);}}int main(){/* 構建一顆如下所示的BST     55    /     \       33      77         /  \    /  \       22   44  66   88*/Node *root = NULL;root = insert(root, 55);insert(root, 33);insert(root, 22);insert(root, 44);insert(root, 77);insert(root, 66);insert(root, 88);Node* pre = NULL, *suc = NULL;int key = 59;findPreSuc(root, key, pre, suc);if (pre != NULL)std::cout << "Predecessor is " << pre->key << std::endl;elsestd::cout << "No Predecessor\n";if (suc != NULL)std::cout << "Successor is " << suc->key << std::endl;elsestd::cout << "No Successor\n";return 0;}
輸出:
Predecessor is 55
Successor is 66

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.