DS Trees Short Notes
DS Trees Short Notes
DS Trees Short Notes
We observe that the root node key (27) has all less-valued keys on the left sub-tree and the higher valued keys on
the right sub-tree.
Basic Operations:
Following are the basic operations of a Binary Search Tree −
• Search − Searches an element in a tree.
• Insert − Inserts an element in a tree.
• Delete- Delete an element in a tree.
• Pre-order Traversal − Traverses a tree in a pre-order manner.
• In-order Traversal − Traverses a tree in an in-order manner.
• Post-order Traversal − Traverses a tree in a post-order manner.
Defining a Node
Define a node that stores some data, and references to its left and right child nodes.
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
Search Operation
Whenever an element is to be searched, start searching from the root node. Then if the data is less than the key
value, search for the element in the left subtree. Otherwise, search for the element in the right subtree. Follow the
same algorithm for each node.
Algorithm
1. START
2. Check whether the tree is empty or not
3. If the tree is empty, search is not possible
4. Otherwise, first search the root of the tree.
5. If the key does not match with the value in the root,
search its subtrees.
6. If the value of the key is less than the root value, search the left subtree
7. If the value of the key is greater than the root value, search the right subtree.
8. If the key is not found in the tree, return unsuccessful search.
9. END
Now, let's understand the searching in binary tree using an example. We are taking the binary search tree formed
above. Suppose we have to find node 20 from the below tree.
Insertion Operation
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.
Algorithm
1. START
2. If the tree is empty, insert the first element as the root node of the tree. The following elements are added as
the leaf nodes.
3. If an element is less than the root value, it is added into the left subtree as a leaf node.
4. If an element is greater than the root value, it is added into the right subtree as a leaf node.
5. The final leaf nodes of the tree point to NULL values as their child nodes.
6. END
Example of creating a binary search tree, Now, let's see the creation of binary search tree using an example.
Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50
Inorder Traversal
The inorder traversal operation in a Binary Search Tree visits all its nodes in the following order −
• Firstly, we traverse the left child of the root node/current node, if any.
• Next, traverse the current node.
• Lastly, traverse the right child of the current node, if any.
Algorithm
1. START
2. Traverse the left subtree, recursively
3. Then, traverse the root node
4. Traverse the right subtree, recursively.
5. END
Preorder Traversal
The preorder traversal operation in a Binary Search Tree visits all its nodes. However, the root node in it is first
printed, followed by its left subtree and then its right subtree.
Algorithm
1. START
2. Traverse the root node first.
3. Then traverse the left subtree, recursively
4. Later, traverse the right subtree, recursively.
5. END
Postorder Traversal
Like the other traversals, postorder traversal also visits all the nodes in a Binary Search Tree and displays them.
However, the left subtree is printed first, followed by the right subtree and lastly, the root node.
Algorithm
1. START
2. Traverse the left subtree, recursively
3. Traverse the right subtree, recursively.
4. Then, traverse the root node
5. END
Zig rotation
The zig rotations are performed when the operational node is either the root node or the left child node of the
root node. The node is rotated towards its right.
The operational node becomes the root node after the shift −
Zig-Zig rotation
The zig-zig rotations are performed when the operational node has both parent and a grandparent. The node is
rotated two places towards its right.
The first rotation will shift the tree to one position right −
The second right rotation will once again shift the node for one position. The final tree after the shift will look
like this −
Zag-Zag rotation
The zag-zag rotations are also performed when the operational node has both parent and a grandparent. The
node is rotated two places towards its left.
Then the final tree after the second rotation is given as follows. However, the operational node is still not the
root so the splaying is considered incomplete. Hence, other suitable rotations are again applied in this case until
the node becomes the root.
Zig-Zag rotation
The zig-zag rotations are performed when the operational node has both a parent and a grandparent. But the
difference is the grandparent, parent and child are in LRL format. The node is rotated first towards its right
followed by left.
After second rotation, the final tree is given as below. However, the operational node is not the root node yet so
one more rotation needs to be performed to make the said node as the root.