Dsa Unit 2
Dsa Unit 2
# BASIC TERMINOLOGIES:
1. Node:
Every element in the tree is called as node which contains data and links to other nodes.
2. Root:
The node which is placed at the top of tree is called as root node.
3. Leaf Nodes:
The nodes which does not have any child node are called as leaf nodes.
4. Degree of Node:
The total number of child nodes of any particular node is called as degree of that node.
5. Level of Node:
The "level" of a node refers to its distance from the root node. The root node is considered to be at level 0, and each
child node of the root is at level 1.
6. Height of Tree:
The maximum level of any leaf node in the tree is called depth or height of the tree.
7. Edge:
The connecting link between any two nodes is called as Edge.
8. Path:
Sequence of edges from one node to another is called as path.
9. Parent:
The node which has child node is called as Parent node.
10. Child:
The node which has a link from its parent node is called Child Node.
11. General Tree:
A general tree is a tree where each node may have zero or more children.
12. Forest:
Forest is an arbitrary set of trees.
Q) What is Binary tree? How is it different than a basic tree? Explain with figures.
1. A binary tree is a type of tree data structure where each node has at most two children, referred to as the left child and the
right child. It is different from a basic tree, which can have any number of children per node.
2. One of the main advantages of using a binary tree is that it allows for efficient searching of elements. Because the tree is
sorted, we can quickly find an element by recursively traversing the left or right subtree depending on the value of the
element we're searching for. Additionally, binary trees can be used to implement a variety of algorithms and data structures,
such as binary heaps and binary search trees.
3. Example of a binary tree:
5
/\
3 7
/\
2 4
4. Example of a basic tree:
1
/|\
2 3 4
/|\
5 6 7
|
8
/\
9 10
• Types Of Binary Tree
1. Strictly Binary Tree:
A strictly binary tree is a type of binary tree in which each node has either 0 or 2 children. In other words, every node in
a strictly binary tree has either 0 children (leaf node) or exactly 2 children.
However, a strictly binary tree does not allow any nodes to have only one child.
Example of a strictly binary tree:
1
/ \
2 3
/\
4 5
Q) Explain traversal of binary tree. Explain different types of traversals of binary tree with example.
1. Traversal of binary trees refers to the process of visiting each node in the tree exactly once in some order.
2. There are three main types of traversal for binary trees:
i. Inorder traversal:
In inorder traversal, we visit the left subtree, then the root node, and then the right subtree.
Algorithm:
1. Traverse the left subtree, i.e., call Inorder (left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder (right-subtree)
Step 2: Compare, the search element with the value of root node in the tree.
Step 3: If both are equal, then display "element found" and exit from the function
Step 4: If both are not equal, then check whether search element is smaller or larger than that node value.
Step 5: If search element is smaller, then continue the search process in left sub-tree.
Step 6: If search element is larger, then continue the search process in right sub-tree.
Step 8: If we reach to the node with search value, then display "Element found" and exit from the function.
Step 9: If we reach to a leaf node and it is also not matching, then display "Element not found" and exit from the function.
Step 9: If we reach to a leaf node and it is also not matching, then display "Element not found" and exit from the function.
Step 4: If the tree is Not Empty, then check whether value of newNode is smaller or larger than the node (here it is root
node).
Step 5: If newNode is smaller than or equal to the node, then move to its left child. If newNode is larger than the node, then
move to its right child.
Step 6: Repeat the above step until we reach to a leaf node (i.e. reach to NULL).
Step 7: After reaching a leaf node, insert the newNode as left child if newNode is smaller or equal to that leaf else insert it as
right child.
3. Select two min. nodes and add new internal node with frequency: 1 + 3 = 4
4
/ \
1 3
4 5 6
6 9
/ \
06 19
C
/ \
04 15
A
/ \
01 13
B D
Character Code
A 11
B 100
C 0
D 101