Data Structures and Algorithms in Java
6th Edition
ISBN: 9781119278023
Author: Michael T. Goodrich; Roberto Tamassia; Michael H. Goldwasser
Publisher: Wiley Global Education US
expand_more
expand_more
format_list_bulleted
Question
Chapter 8, Problem 55C
Program Plan Intro
1. Below mentioned variables are used:
- T- tree with n elements
- root- root node of the tree.
- p- given element 1.
- q-given element 2.
2. Below Mentioned functions are used:
- findLCA()-recursive
algorithm to find the lowest common ancestor (lca) of the given two elements.
Program description:
This program is for finding the lowest common ancestor (LCA) of two elements in a given tree.LCA is an element of tree which has both elements p and q as its dedcendants and has a larger depth of all ancestors of p and q.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Given the following tree, implement DFS(Depth-first Search), IDDFS(iterative Deepening Breath First
Search), and BFS(Breadth-First Search) to write out the order in which nodes are visited for each. Each
algorithm should be written as a separate function taking in the parameters of the graph (T) and the
node being searched for. Please use python and comment your code so I can better understand. Your
code should be structured as follows:
T={'a':['b','c'],'b':['d','e'],'c':['E','g'],'d':['h','i'1,
'e':['j,'k'],'f':[],'g':['l'],'h':[],'i':['m',in'],'j':[],
'k': [],'1':[],'m':[],'n':[]}
def DFS function.
def IDDFS function.
def BFS function.
x=call to DES
y=call to IDDFS
z=call to BFS
print ("DFS:" + x's results)
print ("IDDFS:" + y's results)
print ("BFS:" + z's results)
B
mon
N
If n1, n, ., nk is a sequence of nodes in the tree such that n; is the parent of
n41 for 1
Let T be a binary search tree, and let z be a key. Give an efficient algorithm for finding
the smallest key y in T such that y > r. Note that r may or may not be in T. Explain why your
algorithm has the running time it does.
Chapter 8 Solutions
Data Structures and Algorithms in Java
Knowledge Booster
Similar questions
- Kruskal's algorithm is a minimum-spanning-tree algorithm which finds an edge of the least possible weight that connects any two trees in the forest. This algorithm is a Greedy Algorithm. The steps to find a MST using this algorithm are as follows: Sort all the edges in non-decreasing order of their weight. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it. Repeat step2 until there are (V-1) edges in the spanning tree. The graph must have 10 vertices and 20 weighted edges.arrow_forwardGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA: "The lowest common ancestor is defined between two nodes p and q as the lowest node in t that has both p and q as descendants (where we allow a node to be a descendant of itself)." For example, in the figure from question 1, the LCA between nodes 5 and 46 is 21. You may use the following typedef structure. The function returns the reference of the node that is considered the LCA. typedef struct node_s{ int data; struct node_s * leftchild; struct node _s * rightchild; }node_t;arrow_forwardTl and T2 are two very large binary trees, with Tl much bigger than T2. Create an algorithm to determine if T2 is a subtree of Tl.A tree T2 is a subtree of Tl if there exists a node n in Tl such that the subtree of n is identical to T2. That is, if you cut off the tree at node n, the two trees would be identical.arrow_forward
- The BST remove algorithm traverses the tree from the root to find the node to remove. When the node being removed has 2 children, the node's successor is found and a recursive call is made. One node is visited per level, and in the worst-case scenario, the tree is traversed twice from the root to a leaf. A BST with N nodes has at least log2N levels and at most N levels. Therefore, the runtime complexity of removal is best case O(logN) and worst case O(N). Two pointers are used to traverse the tree during removal. When the node being removed has 2 children, a third pointer and a copy of one node's data are also used, and one recursive call is made. Thus, the space complexity of removal is always O(1)."  I have to explain this clearly! and the advantages of the BST algorithimarrow_forwardGiven two binary trees with head reference as T and S having at most N nodes. The task is to check if S is present as subtree in T.A subtree of a tree T1 is a tree T2 consisting of a node in T1 and all of its descendants in T1 You don't need to read input or print anything. Your task is to complete the function isSubtree() that takes root node of S and T as parameters and returns 1 if S is a subtree of T else 0. Expected Time Complexity: O(N).Expected Auxiliary Space: O(N). Constraints:1 <= Number of nodes <= 1051 <= Value of nodes <= 104arrow_forwardGiven a binary tree, let an H-node be defined as a non-leaf node in the tree whose value is greater than or equal to its children nodes (1 or 2 children). Write a function, countHNodes(), that returns the number of H-nodes in a binary tree (pointed by p) using recursion.arrow_forward
- you need to write the solutions in python and provide a brief explanation of your codes and the efficiency analysis with comments. 3. Consider a loop tree which is an undirected wighted graph formed by taking a binary tree and adding an edge from exactly one of the leaves to another node in the tree as follows: Letnbe the number of vertices in a loop tree. How long does it take Prim's or Kruskal's algorithms to find the minimum spanning tree in terms ofn? Devise a more efficient algorithm that takes an nxn adjacency weighted matrix as input, and finds the minimum spanning tree of a loop tree. 三 input1 - Not Defteri Dosya Düzenle 1078000000 700650060 800006400 060000000 050000021 006000000 004000000 060020000 000010000 output1 - Not Defteri Dosya Düzenle Görünü p 14873265 三 input2 - Not Defteri Dosya Düzenle Görünüm0210000​2003440​1000000​0300000​0400056​0400500​0000arrow_forwardWe have established a family tree and each node in the family tree is a member. Now we want to find out how many family members are in the family. Please write down the algorithm.arrow_forwardSereja likes to hang around trees. A tree is an undirected graph on N vertices with N-1 edges and no cycles. Sereja has his own peculiar way of comparing two trees. To describe it, let's start with the way Sereja stores a tree. For every tree, Sereja has a value V– the root of the tree, and for every vertex i, he has an ordered list Q[i] with L[i] elements – Q[i][1], Q[i][2], ..., Q[iLI] which are children of the vertex i. Sereja assumes two trees to be equal if their roots are the same and for every i, the ordered list Q[i] is the same in both the trees that Sereja compares. So if Sereja has tree#1 given as [V=1, Q[1]=[2, 3], Q[2]=[], Q[3]=0] and tree#2 given as [V=1, Q[1]=[3, 2], Q[2]=[], Q[3]=[]], they will be considered different because Q[1] in the first tree is not equal to Q[1] in the second tree. For any vertex İ, Sereja calls number of vertices adjacent to it as E[i). Given an array C of N elements, Let f(C) be the number of different trees (in Sereja's representation) such…arrow_forward
- Consider the problem of in-order traversals of binary trees. We have seen one way to perform them: recursively, from the top of the tree. Another is to be able to identify, for each node, which node of the tree would follow it in an in-order traversal. Describe, using standard tree terminology, how you would reliably find the "next" after any arbitrary node in a binary tree. Indicate what you would need to know about the current node and how to navigate the tree to get to the next-in-order. You do not need to write code for this problem.arrow_forwardLet T be an arbitrary splay tree storing n elements A1, A2, . An, where A1 ≤ A2 ≤ . . . ≤ An. We perform n search operations in T, and the ith search operation looks for element Ai. That is, we search for items A1, A2, . . . , An one by one. What will T look like after all these n operations are performed? For example, what will the shape of the tree be like? Which node stores A1, which node stores A2, etc.? Prove the answer you gave for formally. Your proof should work no matter what the shape of T was like before these operations.arrow_forwardImplement add_d_leaves, a function that takes in a Tree instance t and mutates it so that at each depth d in the tree, d leaves with labels v are added to each node at that depth. For example, we want to add 1 leaf with v in it to each node at depth 1, 2 leaves to each node at depth 2, and so on. Recall that the depth of a node is the number of edges from that node to the root, so the depth of the root is 0. The leaves should be added to the end of the list of branches. def add_d_leaves (t, v): """Add d leaves >>> t1 = Tree (1, [Tree (3)]) >>>add_d_leaves (t1, 4) >>> t1 Tree (1, [Tree (3, >>> t2 = Tree (2, >>> t3 = t3 = Tree (3, >>>add_d_leaves (t3, 10) >>> print (t3) 3 1 0 3 2 11 4 10 10 5 10 10 6 10 10 10 10 10 containing v to each node at every depth d. 10 10 10 [Tree (4)])]) [Tree (5), Tree (6)]) [t1, Tree (0), t2]) def add_leaves (t, d): "*** YOUR CODE HERE ***" add_leaves (t, 0)arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education