Vzhledem k a BST , úkolem je vyhledat uzel v tomto BST .
Chcete-li hledat hodnotu v BST, považujte ji za seřazené pole. Nyní můžeme snadno provádět vyhledávací operaci v BST pomocí Binární vyhledávací algoritmus .
Algoritmus pro hledání klíče v daném binárním vyhledávacím stromu:
Řekněme, že chceme vyhledat číslo X, Začínáme u kořene. Pak:
- Hodnotu, která se má hledat, porovnáme s hodnotou kořene.
- Pokud se rovná, jsme s hledáním hotovi, pokud je menší, víme, že musíme přejít do levého podstromu, protože v binárním vyhledávacím stromu jsou všechny prvky v levém podstromu menší a všechny prvky v pravém podstromu jsou větší.
- Opakujte výše uvedený krok, dokud již nebude možné přejíždění
- Pokud je v jakékoli iteraci klíč nalezen, vraťte True. Jinak Falešný.
Ilustrace vyhledávání v BST:
Pro lepší pochopení se podívejte na níže uvedený obrázek:
Doporučený postupVyhledejte uzel v BSTTry It!
co je $home linux
Program pro implementaci vyhledávání v BST:
C++
// C++ function to search a given key in a given BST> #include> using> namespace> std;> struct> node {> > int> key;> > struct> node *left, *right;> };> // A utility function to create a new BST node> struct> node* newNode(> int> item)> {> > struct> node* temp> > => new> struct> node;> > temp->klíč = položka;> > temp->vlevo = teplota->vpravo = NULL;> > return> temp;> }> // A utility function to insert> // a new node with given key in BST> struct> node* insert(> struct> node* node,> int> key)> {> > // If the tree is empty, return a new node> > if> (node == NULL)> > return> newNode(key);> > // Otherwise, recur down the tree> > if> (key key)> > node->left = insert(uzel->left, key);> > else> if> (key>uzel->klíč)> > node->right = insert(uzel->right, key);> > // Return the (unchanged) node pointer> > return> node;> }> // Utility function to search a key in a BST> struct> node* search(> struct> node* root,> int> key)> > > // Base Cases: root is null or key is present at root> > if> (root == NULL> // Driver Code> int> main()> {> > struct> node* root = NULL;> > root = insert(root, 50);> > insert(root, 30);> > insert(root, 20);> > insert(root, 40);> > insert(root, 70);> > insert(root, 60);> > insert(root, 80);> > // Key to be found> > int> key = 6;> > // Searching in a BST> > if> (search(root, key) == NULL)> > cout << key <<> ' not found'> << endl;> > else> > cout << key <<> ' found'> << endl;> > key = 60;> > // Searching in a BST> > if> (search(root, key) == NULL)> > cout << key <<> ' not found'> << endl;> > else> > cout << key <<> ' found'> << endl;> > return> 0;> }> |
>
>
C
// C function to search a given key in a given BST> #include> #include> struct> node {> > int> key;> > struct> node *left, *right;> };> // A utility function to create a new BST node> struct> node* newNode(> int> item)> {> > struct> node* temp> > = (> struct> node*)> malloc> (> sizeof> (> struct> node));> > temp->klíč = položka;> > temp->vlevo = teplota->vpravo = NULL;> > return> temp;> }> // A utility function to insert> // a new node with given key in BST> struct> node* insert(> struct> node* node,> int> key)> {> > // If the tree is empty, return a new node> > if> (node == NULL)> > return> newNode(key);> > // Otherwise, recur down the tree> > if> (key key)> > node->left = insert(uzel->left, key);> > else> if> (key>uzel->klíč)> > node->right = insert(uzel->right, key);> > // Return the (unchanged) node pointer> > return> node;> }> // Utility function to search a key in a BST> struct> node* search(> struct> node* root,> int> key)> > // Driver Code> int> main()> {> > struct> node* root = NULL;> > root = insert(root, 50);> > insert(root, 30);> > insert(root, 20);> > insert(root, 40);> > insert(root, 70);> > insert(root, 60);> > insert(root, 80);> > // Key to be found> > int> key = 6;> > // Searching in a BST> > if> (search(root, key) == NULL)> > printf> (> '%d not found
'> , key);> > else> > printf> (> '%d found
'> , key);> > key = 60;> > // Searching in a BST> > if> (search(root, key) == NULL)> > printf> (> '%d not found
'> , key);> > else> > printf> (> '%d found
'> , key);> > return> 0;> }> |
>
>
Jáva
// Java program to search a given key in a given BST> class> Node {> > int> key;> > Node left, right;> > public> Node(> int> item) {> > key = item;> > left = right => null> ;> > }> }> class> BinarySearchTree {> > Node root;> > // Constructor> > BinarySearchTree() {> > root => null> ;> > }> > // A utility function to insert> > // a new node with given key in BST> > Node insert(Node node,> int> key) {> > // If the tree is empty, return a new node> > if> (node ==> null> ) {> > node => new> Node(key);> > return> node;> > }> > // Otherwise, recur down the tree> > if> (key node.left = insert(node.left, key); else if (key>node.key) node.right = insert(uzel.right, key); // Vrátí (nezměněný) ukazatel uzlu return node; } // Funkce nástroje pro hledání klíče při hledání uzlu BST (kořen uzlu, klíč int) // Kód ovladače public static void main(String[] args) { BinarySearchTree tree = new BinarySearchTree(); // Vložení uzlů tree.root = tree.insert(tree.root, 50); tree.insert(strom.kořen, 30); tree.insert(strom.kořen, 20); tree.insert(strom.kořen, 40); tree.insert(strom.kořen, 70); tree.insert(strom.kořen, 60); tree.insert(strom.kořen, 80); // Klíč k nalezení int key = 6; // Hledání v BST if (tree.search(tree.root, key) == null) System.out.println(key + ' not found'); else System.out.println(klíč + ' nalezeno'); klíč = 60; // Hledání v BST if (tree.search(tree.root, key) == null) System.out.println(key + ' not found'); else System.out.println(klíč + ' nalezeno'); } }> |
>
>
Python3
# Python3 function to search a given key in a given BST> class> Node:> > # Constructor to create a new node> > def> __init__(> self> , key):> > self> .key> => key> > self> .left> => None> > self> .right> => None> # A utility function to insert> # a new node with the given key in BST> def> insert(node, key):> > # If the tree is empty, return a new node> > if> node> is> None> :> > return> Node(key)> > # Otherwise, recur down the tree> > if> key node.left = insert(node.left, key) elif key>node.key: node.right = insert(node.right, key) # Vrátit (nezměněný) ukazatel uzlu návratový uzel # Funkce nástroje pro hledání klíče v BST def search(root, key): # Základní případy: root is null nebo klíč je přítomen v rootu, pokud je root Žádný nebo root.key == klíč: return root # Klíč je větší než root, pokud root.key return search(root.right, klíč) # Klíč je menší než root 's key return search(root.left, key) # Driver Code if __name__ == '__main__': root = Žádný root = insert(root, 50) insert(root, 30) insert(root, 20) insert (kořen, 40) insert(kořen, 70) insert(kořen, 60) insert(kořen, 80) # Klíč k nalezení klíč = 6 # Hledání v BST, pokud je hledání(kořen, klíč) Žádné: print(klíč, 'not found') else: print(key, 'found') key = 60 # Hledání v BST, pokud je search(root, key) None: print(key, 'not found') else: print(klíč, 'nalezeno')> |
>
>
C#
// C# function to search a given key in a given BST> using> System;> public> class> Node {> > public> int> key;> > public> Node left, right;> }> public> class> BinaryTree {> > // A utility function to create a new BST node> > public> Node NewNode(> int> item)> > {> > Node temp => new> Node();> > temp.key = item;> > temp.left = temp.right => null> ;> > return> temp;> > }> > // A utility function to insert> > // a new node with given key in BST> > public> Node Insert(Node node,> int> key)> > {> > // If the tree is empty, return a new node> > if> (node ==> null> )> > return> NewNode(key);> > // Otherwise, recur down the tree> > if> (key node.left = Insert(node.left, key); else if (key>node.key) node.right = Insert(uzel.right, key); // Vrátí (nezměněný) ukazatel uzlu return node; } // Funkce nástroje pro hledání klíče ve veřejném BST Hledání uzlu (kořen uzlu, klíč int) // Základní případy: root je null nebo klíč je přítomen v rootu if (root == null // Driver Code public static void Main () { Kořen uzlu = null; BinaryStrom (kořen) , 40 bt.Insert(kořen, 70); bt.Insert(kořen, 80); bt.Search(kořen, klíč) == null) Console.WriteLine(klíč + ' nenalezeno' else Console.WriteLine(klíč + ' nalezen'); // Hledání v BST if (bt.Search(kořen, klíč) == null) Console.WriteLine(klíč + ' nenalezeno' else Console.WriteLine(klíč + ' nalezeno'); |
>
// Javascript function to search a given key in a given BST>
class Node {>
>
constructor(key) {>
>
this>
.key = key;>
>
this>
.left =>
null>
;>
>
this>
.right =>
null>
;>
>
}>
}>
// A utility function to insert>
// a new node with given key in BST>
function>
insert(node, key) {>
>
// If the tree is empty, return a new node>
>
if>
(node ===>
null>
) {>
>
return>
new>
Node(key);>
>
}>
>
// Otherwise, recur down the tree>
>
if>
(key node.left = insert(node.left, key); } else if (key>node.key) { node.right = insert(uzel.right, key); } // Vrátí (nezměněný) ukazatel uzlu return node; } // Funkce nástroje pro hledání klíče ve funkci BST search(root, key) { // Základní případy: root je null nebo klíč je přítomen v root if (root === null || root.key === klíč ) { return root; } // Klíč je větší než root's key if (root.key return search(root.right, key); } // Key je menší než root's key return search(root.left, key); } // Kód ovladače let = null vlož(kořen, 30); vlož(kořen, 70); 60); insert(kořen, 80) // Klíč k nalezení let klíč = 6; nalezeno'); } else { console.log(key + ' found'); } key = 60; key + ' not found' } else { console.log(key + ' found'); }>
>>Výstup6 not found 60 found>Časová složitost: O(h), kde h je výška BST.
Pomocný prostor: O(h), kde h je výška BST. Důvodem je, že maximální množství prostoru potřebného k uložení rekurzního zásobníku by bylo h .Související odkazy:
- Operace vložení binárního vyhledávacího stromu
- Operace odstranění binárního vyhledávacího stromu