UNIT-5 ADA

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

UNIT-5

1. All about Different types of trees


2. Binary Search Trees
3. AVL (Height Balanced Trees)
4. 2-3 Trees
5. B-Trees
6. Tree Search Techniques (Inorder, Preorder, Postorder) (Numericals)
7. Graph Search Techniques( BFS , DFS)(Numericals)
8. Class P, NP, NP-Hard and NP-Complete

1. Binary Search Trees


A Binary Search Tree is a data structure used in computer science for organizing and
storing data in a sorted manner. Each node in a Binary Search Tree has at most two
children, a left child and a right child, with the left child containing values less than
the parent node and the right child containing values greater than the parent node.
This hierarchical structure allows for efficient searching, insertion,
and deletion operations on the data stored in the tree.
Binary Search Tree is a data structure used in computer science for organizing and
storing data in a sorted manner. Binary search tree follows all properties of binary
tree and its left child contains values less than the parent node and the right child
contains values greater than the parent node. This hierarchical structure allows for
efficient Searching, Insertion, and Deletion operations on the data stored in the tree.

What is Binary Search Tree?


Binary Search Tree (BST) is a special type of binary tree in which the left child of a
node has a value less than the node’s value and the right child has a value greater
than the node’s value. This property is called the BST property and it makes it
possible to efficiently search, insert, and delete elements in the tree.
Properties of Binary Search Tree:
 The left subtree of a node contains only nodes with keys lesser than the node’s key.
 The right subtree of a node contains only nodes with keys greater than the node’s
key.
 This means everything to the left of the root is less than the value of the root and
everything to the right of the root is greater than the value of the root. Due to this
performing, a binary search is very easy.
 The left and right subtree each must also be a binary search tree.
 There must be no duplicate nodes(BST may have duplicate values with different
handling approaches)
Handling duplicate values in the Binary Search Tree:
 We must follow a consistent process throughout i.e. either store duplicate value at
the left or store the duplicate value at the right of the root, but be consistent with
your approach.

Basic Operations on Binary Search Tree:


1. Searching a node in BST:
Searching in BST means to locate a specific node in the data structure. In Binary
search tree, searching a node is easy because of its a specific order. The steps of
searching a node in Binary Search tree are listed as follows –
1. First, compare the element to be searched with the root element of the tree.
o If root is matched with the target element, then return the node’s location.
o If it is not matched, then check whether the item is less than the root
element, if it is smaller than the root element, then move to the left subtree.
o If it is larger than the root element, then move to the right subtree.
2. Repeat the above procedure recursively until the match is found.
3. If the element is not found or not present in the tree, then return NULL.
2. Insert a node into a BST:
A new key is always inserted at the leaf. Start searching a key from the root till a leaf
node. Once a leaf node is found, the new node is added as a child of the leaf node.
Time Complexity: O(N), where N is the number of nodes of the BST

3. Delete a Node of BST:


It is used to delete a node with specific key from the BST and return the new BST.
Different scenarios for deleting the node:
Node to be deleted is the leaf node :
Its simple you can just null it out.

Node to be deleted has one child :


You can just replace the node with the child node.
Node to be deleted has two child :
Here we have to delete the node is such a way, that the resulting tree follows the
properties of a BST. The trick is to find the inorder successor of the node. Copy
contents of the inorder successor to the node, and delete the inorder successor.

Take Care of following things while deleting a node of a BST:


1. Need to figure out what will be the replacement of the node to be deleted.
2. Want minimal disruption to the existing tree structure
3. Can take the replacement node from the deleted nodes left or right subtree.
4. If taking if from the left subtree, we have to take the largest value in the left subtree.
5. If taking if from the right subtree, we have to take the smallest value in the right
subtree.

4. Traversal (Inorder traversal of BST) :


In case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing
order. We visit the left child first, then the root, and then the right child.
Time Complexity: O(N), where N is the number of nodes of the BST

Applications of BST:
 Graph algorithms: BSTs can be used to implement graph algorithms, such as in
minimum spanning tree algorithms.
 Priority Queues: BSTs can be used to implement priority queues, where the element
with the highest priority is at the root of the tree, and elements with lower priority are
stored in the subtrees.
 Self-balancing binary search tree: BSTs can be used as a self-balancing data structures
such as AVL tree and Red-black tree.
 Data storage and retrieval: BSTs can be used to store and retrieve data quickly, such
as in databases, where searching for a specific record can be done in logarithmic time.
Advantages:
 Fast search: Searching for a specific value in a BST has an average time complexity
of O(log n), where n is the number of nodes in the tree. This is much faster than
searching for an element in an array or linked list, which have a time complexity of
O(n) in the worst case.
 In-order traversal: BSTs can be traversed in-order, which visits the left subtree, the
root, and the right subtree. This can be used to sort a dataset.
 Space efficient: BSTs are space efficient as they do not store any redundant
information, unlike arrays and linked lists.
Disadvantages:
 Skewed trees: If a tree becomes skewed, the time complexity of search, insertion, and
deletion operations will be O(n) instead of O(log n), which can make the tree
inefficient.
 Additional time required: Self-balancing trees require additional time to maintain
balance during insertion and deletion operations.
 Efficiency: BSTs are not efficient for datasets with many duplicates as they will waste
space.

1. What is a Binary Search Tree?


A binary search tree (BST) is a binary tree where every node in the left subtree is less
than the root, and every node in the right subtree is of a value greater than the root.
The properties of a binary search tree are recursive: if we consider any node as a
“root,” these properties will remain true.
2. What is the Binary Search Tree Operation?
There are major three operations in Binary Search Tree: 1. Insertion, 2. Deletion, 3.
Searching. Because of its properties its efficient to search any element in Binary
Search Tree.
3. What is Binary Search Tree and AVL tree?
Binary Search Tree: A binary search tree (BST) is a binary tree where every node in
the left subtree is less than the root, and every node in the right subtree is of a value
greater than the root.
AVL Tree: Binary search trees (BSTs) that self-balance and rotate automatically are
known as AVL trees.
4. What are the uses of Binary Search Tree?
Binary search trees can be used to implement abstract data types such as dynamic
sets, lookup tables and priority queues, and used in sorting algorithms such as tree
sort.
5. What is the difference between binary search tree and binary tree ?
A Binary search tree is a tree that follows some order to arrange the elements,
whereas the binary tree does not follow any order.

2. Tree Traversal
What is Tree Traversal?
Traversing a tree means visiting and outputting the value of each node in a particular
order. In this tutorial, we will use the Inorder, Preorder, and Post order tree traversal
methods.
The major importance of tree traversal is that there are multiple ways of carrying out
traversal operations unlike linear data structures like arrays, bitmaps, matrices where
traversal is done in a linear order.
Each of these methods of traversing a tree have a particular order they follow:
 For Inorder, you traverse from the left subtree to the root then to the right subtree.
 For Preorder, you traverse from the root to the left subtree then to the right subtree.
 For Post order, you traverse from the left subtree to the right subtree then to
the root.
Here is another way of representing the information above:
Inorder => Left, Root, Right.
Preorder => Root, Left, Right.
Post order => Left, Right, Root.
How to Traverse a Tree Using Inorder Traversal
Remember that the values of the nodes on the left subtree are always smaller than
the value of the root node. Also, the values of the nodes on the right subtree are
larger than the value of the root node.
Example:

Inorder traversal
Recall that the order for inorder traversal is Left, Root, Right.
D, B, E, A, F, C, G

Preorder traversal
The order here is Root, Left, Right.
A, B, D, E, C, F, G

Postorder traversal
Using the same diagram above, we have:
D, E, B, F, G, C, A

3. Graph Traversals

You might also like