Binary Search Trees: Data Structure and Algorithm, Slides of Data Structures and Algorithms

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

2012/2013

Uploaded on 04/27/2013

shareeka_555
shareeka_555 🇮🇳

4

(6)

74 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures &
Algorithm Analysis
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Binary Search Trees: Data Structure and Algorithm and more Slides Data Structures and Algorithms in PDF only on Docsity!

Data Structures &

Algorithm Analysis

A Taxonomy of Trees

• General Trees – any number of children /

node

• Binary Trees – max 2 children / node

  • Heaps – parent < (>) children
  • Binary Search Trees

Binary Search Trees

  • Binary Search Trees (BST) are a type of Binary

Trees with a special organization of data.

  • This data organization leads to O(log n)

complexity for searches, insertions and deletions

in certain types of the BST (balanced trees).

  • O(h) in general

Binary Search algorithm of an array of sorted items reduces the search space by one half after each comparison

Binary Search Algorithm

Docsity.com

Binary Tree

typedef struct tnode *ptnode;

typedef struct node {

short int key;

ptnode right, left;

Searching in the BST

method search(key)

  • implements the binary search based on comparison of the items

in the tree

  • the items in the BST must be comparable (e.g integers, string,

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

Search in a BST: C code

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

  • replace the node's value with the max value in the left subtree
  • delete the max node in the left 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

  • replace the node's value with the max value in the left subtree
  • delete the max node in the left subtree

Removing 7

Removal in BST: Example

What other element can be used as replacement?