




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of binary search trees (bsts), a type of binary tree with a special organization of data. Bsts offer o(log n) complexity for searches, insertions, and deletions in certain types of balanced trees. The binary search algorithm, tree organization rules, and implementation in c code.
Typology: Slides
1 / 28
This page cannot be seen from the preview
Don't miss anything!





















Binary Search algorithm of an array of sorted items reduces the search space by one half after each comparison
Docsity.com
Searching in the BST
method search(key)
in the tree
etc.)
The search starts at the root. It probes down, comparing the
values in each node with the target, till it finds the first item equal
to the target. Returns this item or null if there is none.
BST Operations: Search
Ptnode search(ptnode root, int key) { / return a pointer to the node that contains key. If there is no such node, return NULL /
if (!root) return NULL; if (key == root->key) return root; if (key < root->key) return search(root->left,key); return search(root->right,key); }
method insert(key)
places a new item near the frontier of the BST while retaining its organization of data:
starting at the root it probes down the tree till it finds a node whose left or right pointer is empty and is a logical place for the new value uses a binary search to locate the insertion point is based on comparisons of the new item and values of nodes in the BST Elements in nodes must be comparable!
BST Operations: Insertion
if tree is empty create a root node with the new key else compare key with the top node if key = node key replace the node with the new value else if key > node key compare key with the right subtree: if subtree is empty create a leaf node else add key in right subtree else key < node key compare key with the left subtree: if the subtree is empty create a leaf node else add key to the left subtree
Insertion in BST - Pseudocode
Insertion into a BST: C code
void insert (ptnode node, int key) { ptnode ptr, temp = search(node, key); if (temp || !(node)) { ptr = (ptnode) malloc(sizeof(tnode)); if (IS_FULL(ptr)) { fprintf(stderr, “The memory is full\n”); exit(1); } ptr->key = key; ptr->left = ptr->right = NULL; if (node) if (key<temp->key) temp->left=ptr; else temp->right = ptr; else node = ptr; } }* Docsity.com
removes a specified item from the BST and adjusts the tree
uses a binary search to locate the target item:
starting at the root it probes down the tree till it finds the target or reaches a leaf node (target not in the tree)
removal of a node must not leave a ‘gap’ in the tree,
BST Operations: Removal
method remove (key) I if the tree is empty return false II Attempt to locate the node containing the target using the binary search algorithm if the target is not found return false else the target is found, so remove its node: Case 1: if the node has 2 empty subtrees replace the link in the parent with null
Case 2: if the node has a left and a right subtree
Removal in BST - Pseudocode
Case 1 : removing a node with 2 EMPTY SUBTREES
parent
cursor
Removal in BST: Example
Removing 4 replace the link in the parent with null
Case 2 : removing a node with 2 SUBTREES
cursor cursor
Removing 7
Removal in BST: Example
What other element can be used as replacement?