BST - Practical Writeup
BST - Practical Writeup
Problem Statement:
Beginning with an empty binary search tree, construct binary search tree by inserting the values in
the order given. After constructing a binary tree –
i. Insert new node
ii. Find number of nodes in longest path from root
iii. Minimum data value found in the tree
iv. Change a tree so that the roles of the left and right pointers are swapped at every node
v. Search a value
Learning Objectives:
To understand concept of Tree & Binary Tree.
To analyze the working of various Tree operations.
Learning Outcome:
Students will be able to use various set of operations on Binary search Tree.
Theory:
Tree
Tree represents the nodes connected by edges also a class of graphs tha
thatt is acyclic is termed as
trees. Trees are useful in describing any structure that involves hierarchy
BinaSry Search Tree
It is a node-based
based binary tree data structure which has the following properties:
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.
Advantages of Binary search tree
1. Searching an element in the Binary search tree is easy as we always have a hint that which
subtree has the desired element.
2. As compared to array and linked lists, insertion and deletion operations are faster in BST.
The major advantage of binary search trees over other data structures is that the related
sorting algorithms and search algorithms such as in-order traversal can be very efficient; they are
also easy to code. Binary search trees are a fundamental data structure used to construct more
abstract data structures such as sets, multisets, and associative arrays.
Insert Operation
The very first insertion creates the tree. Afterwards, whenever an element is to be inserted, first
locate its proper location. Start searching from the root node, then if the data is less than the key
value, search for the empty location in the left subtree and insert the data. Otherwise, search for the
empty location in the right subtree and insert the data
Change a tree so that the roles of the left and right pointers are swapped at every node
Algorithm
Write answer according to example given above
v. Search a value
Algorithm
Write answer according to example given above .
Time and Space Complexity
Note: Student must write algorithm of all operations mentioned in problem Statement
Input:
Output:
Conclusion: