0% found this document useful (0 votes)
2 views22 pages

12.Binary Search Tree & Threaded Binary Tree

Binary Search Trees (BST) are efficient data structures for searching, inserting, and deleting data, where all left subtree items are less than the root and all right subtree items are greater or equal. Key operations include traversal, search, insertion, and deletion, with specific strategies for handling nodes with different child configurations. Threaded binary trees enhance traversal efficiency by replacing null pointers with pointers to successor nodes, allowing for faster non-recursive traversals, though they complicate insertion and deletion processes.

Uploaded by

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

12.Binary Search Tree & Threaded Binary Tree

Binary Search Trees (BST) are efficient data structures for searching, inserting, and deleting data, where all left subtree items are less than the root and all right subtree items are greater or equal. Key operations include traversal, search, insertion, and deletion, with specific strategies for handling nodes with different child configurations. Threaded binary trees enhance traversal efficiency by replacing null pointers with pointers to successor nodes, allowing for faster non-recursive traversals, though they complicate insertion and deletion processes.

Uploaded by

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

Binary Search Tree

Binary Search Trees

Binary search trees provide an excellent structure for searching a list and at the same
time for inserting and deleting data into the list.

A binary search tree (BST) is a binary tree with following properties:

•All items in the left of the tree are less than the root.

•All items in the right subtree are greater than or equal to the root.

•Each subtree is itself a binary search tree.

Data Structures: A Pseudocode Approach


2
with C, Second Edition
Data Structures: A Pseudocode Approach
3
with C, Second Edition
BST Operations
There are four basic BST operations: traversal, search, insert, and delete.

• Traversal
• Search
• Insertion
• Deletion

Data Structures: A Pseudocode Approach


4
with C, Second Edition
Traversal

Preorder Traversal : 23 18 12 20 44 35 52
Post order Traversal: 12 20 8 35 52 44 23
In order Traversal: 12 18 20 23 35 44 52

Data Structures: A Pseudocode Approach


5
with C, Second Edition
Find smallest element

Data Structures: A Pseudocode Approach


6
with C, Second Edition
Find largest element

Data Structures: A Pseudocode Approach


7
with C, Second Edition
Search

Data Structures: A Pseudocode Approach


8
with C, Second Edition
Insertion

Data Structures: A Pseudocode Approach


9
with C, Second Edition
Data Structures: A Pseudocode Approach
10
with C, Second Edition
Binary search tree Insertion

1. Insert 42, 10, 22, 3, 8, 7, 9, 4, 2

2. Insert 39, 22, 99, 67, 42, 60, 42, 77, 86, 14, 5, 13

3. Insert 3,7,9,2,3,4,6,11,1

Data Structures: A Pseudocode Approach


11
with C, Second Edition
Deletion
To delete a node from a binary search tree, we must first locate it. There are four cases for deletion:
1. The node to be deleted has no children. All we need to do is delete the node.
2. The node to be deleted has only a right subtree. We need to delete the node and attach the right
subtree to the deleted node’s parent.
3. The node to be deleted has only a left subtree. We need to delete the node and attach the left
subtree to the deleted node’s parent.
4. The node to deleted has two subtrees. It is possible to delete a node from the middle of a tree,
but the result tends to a very unbalanced trees. Rather, we try to maintain existing structure by
finding data to take place of the deleted data.

Data Structures: A Pseudocode Approach


12
with C, Second Edition
When the node to deleted has two subtrees.

1. We can find the largest node in the deleted node’s


left subtree and move its data to replace the
deleted node’s data
2. Or we can find the smallest node in the deleted
node’s right subtree and move its data to replace
the deleted node’s data

Regardless of which logic we choose, we will be


moving data from a leaf or leaf-like node that can
be deleted.
Data Structures: A Pseudocode Approach
15
with C, Second Edition
1. Construct Binary Search Tree for the list of numbers given below :
53,55,54,65,44,25,46,82,60,17,28,41

2. Delete 17
3. Delete 54

Write inorder and preorder traversal

Data Structures: A Pseudocode Approach


16
with C, Second Edition
Threaded Binary tree
▪ depth-first traversal algorithms are written using either recursion or stacks
▪ If the tree must be traversed frequently, using stacks rather than recursion may be more efficient
▪ Third alternative is a In a threaded tree.
▪ Using a stack for each call makes the binary tree traversal relatively inefficient, particularly if the tree must be
traversed frequently
▪ For example, in the inorder traversal of a binary tree, we must traverse the left subtree, the node, and the right subtree
▪ Because an inorder traversal is a depth-first traversal, we follow left pointers to the far-left leaf.
▪ When we find the far-left leaf, we must begin backtracking to process the right subtrees we passed on our way down
▪ This is especially inefficient when the parent node has no right subtree
▪ Hence treaded binary tree is used
Threaded Binary tree
To facilitate the traversal of a BST, threaded BSTs were created.

➢ In a threaded BST, null pointers are replaced with pointers to the successor node
in a specific traversal order.

Inorder traversal of a Binary tree can either be done using recursion or with the use of stack.
The idea of threaded binary trees is to make inorder traversal faster and do it without stack and without
recursion.
A binary tree is made threaded by making all right child pointers that would normally be NULL point to the
inorder successor of the node (if it exists).

Data Structures: A Pseudocode Approach


19
with C, Second Edition
• To build a threaded tree, first build a standard binary search tree.
• Then traverse the tree, changing the null right pointers to point to their successors.
• The traversal for a threaded tree is straightforward. Once you locate the far-left node, following the thread
(the right pointer) to the next node.
• No recursion or stack is needed.
• When you find a null thread (rightpointer), the traversal is complete.

Data Structures: A Pseudocode Approach


20
with C, Second Edition
Data Structures: A Pseudocode Approach
21
with C, Second Edition
Advantages of threaded binary tree:
Threaded binary trees have numerous advantages over non-threaded binary trees listed as below:
▪ The traversal operation is more faster than that of its unthreaded version, because with threaded binary tree non-
recursive implementation is possible which can run faster and does not require the botheration of stack
management.
▪ The second advantage is more understated with a threaded binary tree, we can efficiently determine the
predecessor and successor nodes starting from any node. In case of unthreaded binary tree, however, this task is
more time consuming and difficult
Disadvantages of threaded binary tree:
Insertion and deletion from a threaded tree are very time consuming operation compare to non-threaded binary tree.
This tree require additional bit to identify the threaded link.

Data Structures: A Pseudocode Approach


22
with C, Second Edition

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy