Question

In: Computer Science

2. Let BT Node be the class we often use for binary-tree nodes. Write the following...

2. Let BT Node be the class we often use for binary-tree nodes. Write the following recursive methods: (a) numLeaves: a method that takes a BT Node T as parameter and returns the number of leaves in the tree rooted at T. (b) isEven: a boolean method that takes a BT Node T and checks whether its tree is strictly binary: every node in the tree has an even number of children.

3. Suppose you want to improve Merge Sort by first applying Heap Sort to a number of consecutive subarrays. Given an array A, your algorithm subdivides A into subarrays A1, A2 · · · Ak, where k is some power of 2, and applies Heap Sort on each subarray Ai alone. The algorithm proceeds into merging pairs of consecutive subarrays until the array is sorted. For example, if k = 4, you first apply Heap Sort to sort each Ai and then you merge A1 with A2 and A3 with A4, then you apply the merge function once to get the sorted array. (a) Does the proposed algorithm improve the asymptotic running time of Merge Sort when k = 2? How about the case k = log n (or a power of 2 that is closest to log n)? Justify. (b) Is the proposed algorithm stable? Is it in-place? Prove your answers.

4. Write a clear pseudocode for Breadth-First Search (BFS) in undirected graphs. How would you modify your code to compute the number of connected components in the input graph? Give details about the used data structures and the running time.

5. Write a clear pseudocode for Depth-First Search (DFS) in graphs. How would you modify your code to check whether the graph is acyclic?

6. The centrality of a vertex v in an undirected graph G is measured as follows: c(v) = n1(v) + n2(v) + · · · nd(v) where ni(v) is the number of vertices at distance i from v (e.g., n1(v) is the number of neighbors of v) and d is the maximum distance between v and a vertex of the same connected component as v in G (so d = 0 if v is isolated). Write the pseudocode of a most-efficient algorithm that computes the centrality of a vertex v in a given graph G. What is the running time of your algorithm? Prove your answer.

Solutions

Expert Solution

2. a)

static int getfullCount(Node root)  
{  
    if (root == null)  
    return 0;  
  
    int res = 0;  
    if (root.left != null && root.right != null)  
    res++;  
  
    res += (getfullCount(root.left) + getfullCount(root.right));  
    return res;  
}  

b) Since it is a "BINARY" tree it will always nodes that have even number of children.

4.

A standard BFS implementation puts each vertex of the graph into one of two categories:

  • Visited
  • Not Visited

The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.

The algorithm works as follows:

  • Start by putting any one of the graph's vertices at the back of a queue.
  • Take the front item of the queue and add it to the visited list.
  • Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to the back of the queue.
  • Keep repeating steps 2 and 3 until the queue is empty.

The graph might have two different disconnected parts so to make sure that we cover every vertex, we can also run the BFS algorithm on every node

5.

Pseudocode:

procedure DFS(G,v):
  label v as discovered
  for all edges from v to w in G.adjacentEdges(v) do
    if vertex w is not labeled as discovered then
      recursively call DFS(G,w)

For checking cycles in graph:

Algorithm:

  • Create the graph using the given number of edges and vertices.
  • Create a recursive function that initializes the current index or vertex, visited, and recursion stack.
  • Mark the current node as visited and also mark the index in recursion stack.
  • Find all the vertices which are not visited and are adjacent to the current node. Recursively call the function for those vertices, If the recursive function returns true, return true.
  • If the adjacent vertices are already marked in the recursion stack then return true.
  • Create a wrapper class, that calls the recursive function for all the vertices and if any function returns true return true. Else if for all vertices the function returns false return false.

Related Solutions

. Let BT Node be the class we often use for binary-tree nodes. Write the following...
. Let BT Node be the class we often use for binary-tree nodes. Write the following recursive methods: (a) numLeaves: a method that takes a BT Node T as parameter and returns the number of leaves in the tree rooted at T. (b) isEven: a boolean method that takes a BT Node T and checks whether its tree is strictly binary: every node in the tree has an even number of children.
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes (data, x, y). The binary tree need to have function "add()" to add new node into the tree. After added all 10 nodes, it will be sorted and turn into a balanced binary search tree.
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
a tree is binary, if every node has at most two children nodes. prove that the...
a tree is binary, if every node has at most two children nodes. prove that the maximum of nodes in a binary tree of height h is 2^(h+1)-1
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition. If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode *find(int item) { //implement code here return nullptr; } int main() {    root = new...
Write a solution to return the count of the number of nodes in a binary tree....
Write a solution to return the count of the number of nodes in a binary tree. Your method will be passed one parameter, a copy of a pointer to the root node of the tree (Node *) and will return an int that is the count of nodes. If the tree is empty, return 0 (this is the recursive base case).
Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string.
Programming CWrite the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include:• Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node.• Find a node by integer value: This function takes in two parameters: the root...
Write a C++ method that traverses through a binary tree (only 2 children to a node)...
Write a C++ method that traverses through a binary tree (only 2 children to a node) using preorder traversal and finds the proper place to put in a new node. The method will take in an int and traverse through the tree to find the proper place but it must use PREORDER traversal to find a proper position. struct node{ int data; struct node* left, *right; Node(int Data){ this->data=data; left=right=NULL; } }; void preorderAdd(int newNum){ //Method to traverse using preorder...
Write a binary search tree and include the functions to find the number of nodes and...
Write a binary search tree and include the functions to find the number of nodes and the height of the tree. Test your code in main. Print the post-order, in-order and pre-order traversal. in c++ please.
Consider the following type of binary trees: data Tree a = Leaf a | Node (Tree...
Consider the following type of binary trees: data Tree a = Leaf a | Node (Tree a) (Tree a) A tree is balanced if the number of leaves in the left and right subtree of every node differ by at most one. Write a Haskell function balanced that returns whether a tree is balanced or not. balanced :: Tree a -> Bool
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT