Professional Documents
Culture Documents
UNIT-5 ADA
UNIT-5 ADA
UNIT-5 ADA
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.
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