0% found this document useful (0 votes)
18 views7 pages

Dsa Unit 2

Tree data structures can be represented using arrays or linked lists. Binary trees have at most two children per node, unlike general trees which can have any number. There are different types of binary trees depending on their structure, such as full, complete, skewed trees. Traversal of binary trees involves visiting each node exactly once in a specific order, such as inorder (left, root, right), preorder (root, left, right), and postorder (left, right, root).

Uploaded by

priyankanarode16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

Dsa Unit 2

Tree data structures can be represented using arrays or linked lists. Binary trees have at most two children per node, unlike general trees which can have any number. There are different types of binary trees depending on their structure, such as full, complete, skewed trees. Traversal of binary trees involves visiting each node exactly once in a specific order, such as inorder (left, root, right), preorder (root, left, right), and postorder (left, right, root).

Uploaded by

priyankanarode16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DSA UNIT 2

Tuesday, April 4, 2023 1:29 AM

Q) What is Tree Explain in detail


1. A tree may be defined as a finite set T of one or more nodes such that there is a node designated as the root of the tree
and the other nodes are divided into n>= 0 disjoint sets T1, T2, T3, T4.... Tn are called the sub trees or children of the root.
2. The tree structure has a hierarchical organization, with one node at the top, called the root, and the remaining nodes
arranged in a hierarchy below the root.
3. Each node in a tree has a value or data, and it may or may not have child nodes.
4. The nodes that have no children are called leaf nodes. Nodes that are not leaf nodes are called internal nodes, and they
can have one or more child nodes.
5. Insertion and searching can be done efficiently in trees.

# 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

2. Complete Binary Tree:


A binary tree in which every internal node has exactly two child nodes and all the leaf nodes are at same level is called
Complete Binary Tree.
Example of a Complete Binary Tree:
18
/ \
15 30
/ \ / \
40 50 100 6

3. Almost Complete Binary Tree:


An almost complete binary tree is a binary tree where all levels of the tree are completely filled with nodes except
possibly for the last level, which may be partially filled from left to right.
Example:
18
/ \
15 30
/ \
40 50
4. Skewed Binary Tree:
The Binary Tree in which either only left branches are present or only right branches are present is called as Skewed
Binary Tree.
Example:
8
\
9
\
10
5. Extended Binary Tree:
The full binary tree obtained by adding dummy nodes to a binary tree is called as Extended Binary Tree.
Example: 1
1 / \
/ \ 2 3
2 3 / \ / \
/\ 4 5
4 5 /\ / \

6. Full binary tree:


A full 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
full binary tree has either 0 children (leaf node) or exactly 2 children.
Example of a full binary tree:
1
/ \
2 3
/\
4 5
Q) Explain the concept of representation of Binary tree using an Array.
1. In sequential or array representation, binary tree is represented in sequence in memory with the help of single dimensional
array.
2. A binary tree of level L may comprise at most 2L - 1 nodes, hence an array of maximum size 2L - 1 is used for representing
such a tree.
3. All the nodes of the tree are assigned a sequence number from 0 to 2L - 1 level by level.
4. That is, the root node at level 0 is assigned a sequence number 0, and then nodes at level 1 are assigned sequence
number in ascending order from left to right, and so on.
M0
/ \
1G R2
/ \ / \
3D 4I P 5 T6
5. The number assigned to the node indicates the position (index value) of an array at which that particular node is stored.

A. Advantages Of Sequential Representation of Binary Tree:


(i) This representation is very easy to understand.
(ii) This is the best representation for complete and full binary tree representation.
(iii) Programming is very easy.
(iv) It is very easy to move from a child to its parents and vice versa.

B. Dis-Advantages Of Sequential Representation of Binary Tree:


(i) Lot of memory area is wasted.
(ii) Insertion and deletion of nodes needs lot of data movement
(iii) Execution time is high.
(iv) This is not suited for trees other than full and complete tree.
Q) Explain the concept of representation of Binary tree using Linked List.
In the linked list representation of a binary tree, each node of the binary tree is represented as a node in a linked list.
Each node in the linked list contains a pointer to the corresponding node in the binary tree, as well as a pointer to the next
node in the linked list.
The binary tree can be represented by a pointer to the root node of the tree.
Each node in the binary tree has a left child and a right child, which can be represented by pointers to the corresponding
nodes.
Example of a binary tree and its linked list representation:

A. Advantages Of Linked Representation of Binary Tree:


(i) It is easy to add new node at any location in the tree.
(ii) It is possible to insert or delete node directly without data movements.
(iii) It is best for any type of trees.
(iv) It is flexible because the system takes care of allocating and freeing of nodes.

B. Dis-Advantages Of Linked Representation of Binary Tree:


(i) The mechanism is difficult to understand.
(ii) There is need of additional memory to store pointers.
(iii) Accessing a particular node is not complicated.
Q) Compare between General Tree and Binary Tree
GENERAL TREE BINARY TREE
1. A general tree is a data structure in which each 1. A Binary tree is a data structure in which each
node can have infinite number of children. node has at most two nodes left and right.
2. A General tree can't be empty. 2. A Binary tree can be empty.
3. Sub-trees of general tree are not ordered. 3. Sub-trees of binary tree are ordered.
4. In general tree, root have in-degree 0 and 4. In binary tree, root have in-degree 0 and
maximum out-degree n. maximum out-degree 2.
5. In general tree, each node has in-degree one 5. In binary tree, each node has in-degree one
and maximum out-degree n. and maximum out-degree 2.
6. Eg. 6. Eg.
1 5
/|\ /\
2 3 4 3 7
/|\ /\
5 6 7 2 4
|
8
/\
9 10

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)

ii. Preorder traversal:


 In preorder traversal, we visit the root, then the left subtree, and then the right subtree.
 Algorithm:
1. Visit the root
2. Traverse the left subtree, i.e., call preorder (left-subtree)
3. Traverse the right subtree, i.e., call preorder (right-subtree)
 1, 2, 4, 5, 3, 6, 7

iii. Postorder traversal:


 In postorder traversal, we visit the left subtree, then the right subtree, and then the root node.
 Algorithm:
1. Traverse the left subtree
2. Traverse the right subtree
3. Visit the root.
3. Eg.
1
/ \
2 3
/\ /\
4 5 6 7
Inorder traversal: 4, 2, 5, 1, 6, 3, 7
Preorder traversal: 1, 2, 4, 5, 3, 6, 7
Postorder traversal: 4, 5, 2, 6, 7, 3, 1
Q) Explain Depth first & Breadth first operation in detail.
1. In graph traversal, depth-first search (DFS) and breadth-first search (BFS) are two well-known methods for visiting and
exploring all the vertices or nodes of a graph.
2. Depth-first operation: In depth-first operation, we visit the nodes in the tree in a depth-first order, which means we visit the
nodes in each subtree as deeply as possible before backtracking to visit the remaining nodes. There are two common ways
to perform depth-first operation: using a preorder traversal or a postorder traversal.
3. Breadth-first operation: In breadth-first operation, we visit the nodes in the tree in a level-by-level order, visiting all nodes at
one level before moving on to the next level. To perform a breadth-first operation on this tree, we can use a queue to keep
track of the nodes we still need to visit. We start by adding the root node to the queue, and then repeatedly dequeue the next
node and enqueue its child nodes (if any) until the queue is empty
4. Eg.
1
/ \
2 3
/\ /\
4 5 6 7
i. There are two common ways to perform depth-first operation: using a preorder traversal or a postorder traversal.
 Preorder traversal:
With a preorder traversal, we visit the current node first, then recursively visit the left and right subtrees. The
depth-first operation with a preorder traversal of this tree would be:
1, 2, 4, 5, 3, 6, 7
 Postorder traversal:
With a postorder traversal, we recursively visit the left and right subtrees first, then visit the current node. The
depth-first operation with a postorder traversal of this tree would be:
4, 5, 2, 6, 7, 3, 1
ii. The breadth-first operation of this tree would be:
1, 2, 3, 4, 5, 6, 7

Q) Define binary search tree and its operations?


1. A binary search tree (BST) is a binary tree data structure where each node has at most two child nodes and every node's left
child is less than its value and every node's right child is greater than its value.
2. The operations that can be performed on a BST include:
1) Insertion: To insert a new node into a BST, we compare its value with the root node. If it is less than the root node, we
insert it into the left subtree, otherwise, we insert it into the right subtree.
2) Deletion: To delete a node from a BST, we first search for the node to be deleted. If it is a leaf node, we simply remove it.
If it has only one child node, we replace the node with its child. If it has two child nodes, we find its in-order successor
(i.e., the smallest node in its right subtree), replace the node's value with the successor's value, and then delete the
successor node.
3) Search: To search for a value in a BST, we compare the value with the root node. If the value is less than the root node,
we search in the left subtree, otherwise, we search in the right subtree. We continue this process recursively until we find
the value or reach a leaf node.

 Algorithm for Searching an element in BST:


Step 1: Read the data (search element) from user to search.

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 7: Repeat the same until we found exact element

or we completed with a leaf node.

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.

 Algorithm for Inserting an element in BST:


Step 1: Create a newNode with given value and set its left and right to NULL.

Step 2: Check whether tree is Empty.

Step 3: If the tree is Empty, then set root to newNode.

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.

Q) Explain Threaded Binary Search Tree with an Example.


1. A threaded binary tree is a binary tree that has threads connecting certain nodes to their inorder predecessors or successors,
allowing for efficient traversal of the tree without the need for recursion or a stack.
2. In a threaded binary tree, the null pointers of the tree are replaced by threads, which point to the inorder predecessor or
successor of the node.
3. A node in the tree can have two types of threads: a left-thread, which points to the inorder predecessor of the node, and a
right-thread, which points to the inorder successor of the node.
4. One-Way Threaded Binary Tree:
i. In this type of TBT the NULL pointers of the leaf node of the Binary Tree is set to inorder predecessor or inorder
successor.
ii. If the left pointer of leaf node points to its inorder predecessor the TBT is called as Left-In-Threaded Binary Tree.
iii. If the right pointer of leaf node points to its inorder successor the TBT is called as Right-In-Threaded Binary Tree.

5. Two-Way Threaded Binary Tree:


In this type of TBT the right as well as the left NULL pointer are made to point inorder successor and inorder predecessor
respectively.
Q) Explain Huffman Coding with an example.
Huffman coding is a lossless data compression technique that uses variable length codes to represent characters based on
their frequency of occurrence in the input data.
The idea is to assign variable-length codes to input characters, lengths of the assigned codes are based on the frequencies of
corresponding characters.
The most frequent character gets the smallest code and the least frequent character gets the largest code.
Eg. BCAADDDCCACACAC
1. Calculate frequency of each character in the string
1 6 5 3
B C A D
2. Sort the characters in increasing order of the frequency
1 3 5 6
B D A C

3. Select two min. nodes and add new internal node with frequency: 1 + 3 = 4
4
/ \
1 3

4 5 6

4. Repeat Step 3 with frequency 4+5=9


9
/ \
4 5
/ \
1 3

6 9

5. Repeat Step 3 with frequency 6+9=15


015

/ \
06 19
C
/ \
04 15
A
/ \
01 13
B D

6. As all frequencies are used, to get Huffman Code from tree:


Traverse the tree from starting from root. Mark Left Child as 0 and Right child 1.

Character Code
A 11
B 100
C 0
D 101

You might also like