Question: Part 1 : Trees ( 5 0 Points ) Trees are hierarchical data structures with a root node and child nodes. In this assignment, we'll
Part : Trees Points
Trees are hierarchical data structures with a root node and child nodes. In this assignment, we'll
work with binary trees, which are trees where each node has at most two children.
Basic Tree Operations Points
You will create a simple binary tree that supports three main operations: inserting nodes,
searching for a specific value, and deleting a node.
Example :
If you insert the values into the binary tree, the tree should look like this:
Tasks :
Write a function to insert nodes Points
Write a function to search for a value in the tree Points
Write a function to delete a node from the tree Points
Hints :
For insertion, start from the root and find the correct position for the new node.
For searching, traverse the tree and return True if the value is found, else return False
For deletion, handle three cases: leaf node deletion, node with one child, and node with two
children.
Tree Traversals Points
Tree traversal means visiting all the nodes in a particular order. Youll write code to visit nodes in
three different ways: Preorder, Inorder, and Postorder.
Example :
For the tree above, the traversals will look like:
Preorder rootleftright:
Inorder leftrootright:
Postorder leftrightroot:
Tasks :
Write a function for Preorder traversal Points
Write a function for Inorder traversal Points
Write a function for Postorder traversal Points
Height and Depth of a Tree Points
The height of a tree is the longest path from the root to a leaf. The depth of a node is how far
it is from the root.
Example :
In the above tree, the height of the tree is and the depth of node is because you
move two levels from the root to reach
Tasks :
Write a function to calculate the height of the tree Points
Write a function to calculate the depth of a specific node Points
Part : Graphs Points
Graphs are made of nodes or vertices connected by edges. Well represent a graph using an
adjacency list, which is a simple way of showing which nodes are connected.
Graph Representation Points
Youll write a Python class to create a graph using an adjacency list. This will help us represent
connections between different nodes.
Example :
If you have the following edges:
A BA CB DC D
the adjacency list will look like this:
A: B C
B: D
C: D
D:
Tasks :
Write a function to add a vertex Points
Write a function to add an edge between two vertices Points
Write a function to display the adjacency list Points
Test your functions by creating a simple graph and displaying it Points
Graph Traversals Points
Just like trees, you can traverse graphs. The two most common methods are DepthFirst Search
DFS and BreadthFirst Search BFS
Example :
For the graph above, starting from node A the traversals will look like this:
DFS: A B D C
BFS: A B C D
Tasks :
Write a function for DFS Points
Write a function for BFS Points
Detecting Cycles in a Graph Points
A cycle in a graph occurs when you can start at a node, travel along edges, and return to the
same node. You will write a function to detect cycles in both directed and undirected graphs.
Tasks :
Write a function to detect cycles in an undirected graph Points
Write a function to detect cycles in a directed graph Points
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
