Binary Search Tree

Binary Search Tree




200px-Binary_search_tree.svg















Binary Search Trees (BST), biasanya disebut ordered atau sorted binary trees, ini sebuah kontainer, sebuah data struktur yang menyimpan item seperti angka ataupun nama. BST adalah binary tree yang rooted, yang dimana internal nodes menyimpan sebuah kunci, dan satu kunci ada 2 sub trees, yang biasanya disebut left and right.

Code:
// C function to search a given key in a given BST
struct node* search(struct node* root, int key)
{
    // Base Cases: root is null or key is present at root
    if (root == NULL || root->key == key)
       return root;
     
    // Key is greater than root's key
    if (root->key < key)
       return search(root->right, key);
  
    // Key is smaller than root's key
    return search(root->left, key);
}

Comments