0% found this document useful (0 votes)
61 views33 pages

PPT6 - Binary Search Tree

- The document discusses binary search trees and threaded binary trees. It covers binary search tree operations like search, insertion, and deletion. It also explains the concept of threaded binary trees where null pointers are replaced with pointers to predecessors and successors, allowing linear traversal. Operations on binary search trees include recursively searching for a key, inserting new keys by comparing them to node values, and deleting nodes by considering cases for leaf nodes and nodes with one or two children.

Uploaded by

Rapik Hardianto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views33 pages

PPT6 - Binary Search Tree

- The document discusses binary search trees and threaded binary trees. It covers binary search tree operations like search, insertion, and deletion. It also explains the concept of threaded binary trees where null pointers are replaced with pointers to predecessors and successors, allowing linear traversal. Operations on binary search trees include recursively searching for a key, inserting new keys by comparing them to node values, and deleting nodes by considering cases for leaf nodes and nodes with one or two children.

Uploaded by

Rapik Hardianto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

COMP6601 – Data Structures

Week 6 – Binary Search Tree


Sub Topics
• Binary Search Tree Concept
• Operations: Search, Insertion, Deletion
• Program Examples
Binary Search Tree
Operations
• Binary Search Tree has the following basic
operations:
 find(x) : find key x in the BST
 insert(x) : insert new key x into BST
 remove(x) : remove key x from BST
Operations:
Search
• Because of the property of BST, finding/searching in
BST is easy.

• Let the key that we want to search is X.


 We begin at root
 If the root contains X then search terminates successfully.
 If X is less than root’s key then search recursively on left sub
tree, otherwise search recursively on right sub tree.
Operations:
Search
struct node* search (struct node *curr, int X) {
if ( curr == NULL ) return NULL;

// X is found
if ( X == curr->data ) return curr;

// X is located in left sub tree


if ( X < curr->data ) return find(curr->left, X);

// X is located in right sub tree


return find(curr->right, X);
}
Operations:
Insertion
• Inserting into BST is done recursively.

• Let the new node’s key be X,


 Begin at root
 If X is less than node’s key then insert X into left sub tree,
otherwise insert X into right sub tree
 Repeat until we found an empty node to put X (X will always
be a new leaf)
Operations:
Insertion
Algorithm:
Step 1:
IF TREE = NULL, then
Allocate memory for TREE
SET TREE->DATA = VAL
SET TREE->LEFT = TREE ->RIGHT = NULL
ELSE
IF VAL < TREE->DATA
Insert(TREE->LEFT, VAL)
ELSE
Insert(TREE->RIGHT, VAL)
[END OF IF]
[END OF IF]
Step 2: End
Operations:
Insertion – Example

• Inserting new
• key (35)
Operations:
Insertion – Example
Operations:
Insertion – Example
Operations:
Insertion – Example
Operations:
Deletion
• There are 3 cases which should be considered:
 If the key is in a leaf, just delete that node
 If the key is in a node which has one child, delete that node
and connect its child to its parent
 If the key is in a node which has two children, find the right
most child of its left sub tree (node P), replace its key with
P’s key and remove P recursively. (or alternately you can
choose the left most child of its right sub tree)
Operations:
Deletion
Algorithm:
Step 1: IF TREE = NULL, then
Write “VAL not found in the tree”
ELSE IF VAL < TREE->DATA
Delete(TREE->LEFT, VAL)
ELSE IF VAL > TREE->DATA
Delete(TREE->RIGHT, VAL)
ELSE IF TREE->LEFT AND TREE->RIGHT
SET TEMP = findLargestNode(TREE->LEFT)
SET TREE->DATA = TEMP->DATA
Delete(TREE->LEFT, TEMP->DATA)
Operations:
Deletion
Algorithm:
ELSE
SET TEMP = TREE
IF TREE->LEFT = NULL AND TREE ->RIGHT = NULL
SET TREE = NULL
ELSE IF TREE->LEFT != NULL
SET TREE = TREE->LEFT
ELSE
SET TREE = TREE->RIGHT
FREE TEMP
Step 2: End
Operations:
Deletion – Example

• Deleting key (21)


Operations:
Deletion – Example

• Deleting key (21),


• (continue)
Operations:
Deletion – Example

• Deleting key (37)


Operations:
Deletion – Example

• Deleting key (37),


• (continue)
Operations:
Deletion – Example

• Deleting key (30)


Operations:
Deletion – Example

• Deleting key (30),


• (continue)
Program Example
Program Example
Program Example
Threaded Binary Tree
Concept
• A threaded binary tree is same as that of a binary tree
but with a difference in storing NULL pointers.
• Binary Tree:
Threaded Binary Tree
Concept
• In the linked representation, a number of nodes
contain a NULL pointer either in their left or right fields
or in both.

• This space that is wasted in storing a NULL pointer can


be efficiently used to store some other useful peace of
information.
Threaded Binary Tree
Concept
• For example, the NULL entries can be replaced to store
a pointer to the in-order predecessor, or the in-order
successor of the node.
• These special pointers are called thread and binary
trees containing thread are called threaded trees.
• In the linked representation of a threaded binary tree,
threads will be denoted using dotted lines
Threaded Binary Tree
Concept
 Binary Tree without threading

Linked 
representation of
the binary tree
Threaded Binary Tree
Concept
• In one way threading, a thread will appear either in the right
field or the left field of the node.
• A one way threaded tree is also called a single threaded tree.
• If the thread appears in the left field, then the left field will be
made to point to the in-order predecessor of the node.
• Such a one way threaded tree is called a left threaded binary
tree.
• On the contrary, if the thread appears in the right field, then it
will point to the in-order successor of the node. Such a one
way threaded tree is called a right threaded binary tree.
Threaded Binary Tree
Concept

Binary tree with


one-way
threading
Threaded Binary Tree
Example

Binary tree with


two-way
threading
Advantages
• It enables linear traversal of elements in the tree
• Linear traversal eliminates the use of stacks which in turn
consume a lot of memory space and computer time
• It enables to find the parent of a given element without explicit
use of parent pointers
• Since nodes contain pointers to in-order predecessor and
successor, the threaded tree enables forward and backward
traversal of the nodes as given by in-order fashion
Summary
• A threaded binary tree is same as that of a binary tree
but with a difference in storing NULL pointers
Thank You

You might also like