EBK DATA STRUCTURES AND ALGORITHMS IN C
4th Edition
ISBN: 9781285415017
Author: DROZDEK
Publisher: YUZU
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
2. Write a method that takes any two nodes u and v in a tree T, and quickly determines if the node u in the
tree is a descendant or ancestor of node v
}
20. In a binary search tree, the immediate predecessor of a given node is the largest node in its left subtree.
For example, the immediate predecessor of node 25 in the following tree is 23 while it is 15 for node 16.
Nodes 21 and 39 do not have an immediate predecessor because none of them has a left child.
25
/ 1
Write a non-recursive method immediate Predecessor, which takes a
BSTNode parameter node (a reference to a node in a binary search tree)
and returns a reference to its immediate predecessor in the tree. If node is
null or it does not have an immediate predecessor, the method returns
null.
public class BSTNode
4
{
private int m_info;
private BSTNode m_left;
private BSTNode m_right;
public int getInfo() {return m_info; }
public void setInfo (int value) {m_info
= info; }
public BSTNode getLeft () {return m_left; }
public void setLeft (BSTNode left) (m_left = left; }
public BSTNode getRight () {return m_right;}
public void setRight (BSTNode right) {m_right
= right;}
}
public…
7
4
2
4
2
Tree #1
1
5
Tree #2
6
3
6
8
8
9
5
7
9
1
For parts a through g, implement the given method and demonstrate the method working with the
two trees provided earlier.
All code implemented in this assignment should be in a class called Homework 6. You may use the
data structures and algorithm code from the lecture notes.
Hint: Consider using recursion. To do so implement a private helper method that takes a Node and
then recursively calls itself to traverse the tree. The public method would call the private method
passing the tree's root as the Node.
Chapter 7 Solutions
EBK DATA STRUCTURES AND ALGORITHMS IN C
Knowledge Booster
Similar questions
- Write a recursive method isOrdered() that takes a Node and twokeys min and max as arguments and returns true if all the keys in the tree are betweenmin and max; min and max are indeed the smallest and largest keys in the tree, respectively; and the BST ordering property holds for all keys in the tree; false otherwise.arrow_forwardWrite a recursive function that increments by one the value for every node in the binary tree pointed at by root, then returns the modified tree. Assume that nodes store integer values. Here are methods that you can use on the BinNode objects: interface BinNode { public int value(); public void setValue(int v); public BinNode left(); public BinNode right(); public boolean isLeaf(); } public BinNode BTinc(BinNode root){ }arrow_forwardCreate a recursive method isOrdered() that takes a Node as an argument and two keys min and max as arguments and returns true if all the keys in the tree are between min and max; min and max are indeed the smallest and largest keys in the tree, respectively; and the BST ordering property holds for all keys in the tree; false otherwise.arrow_forward
- Create a recursive method called isOrdered() that takes a Node and the two keys min and max as arguments. It should return true if all of the tree's keys are between min and max, min and max are, in fact, its smallest and largest keys, respectively, and the BST ordering property holds for all of the tree's keys, and false if it does not.arrow_forwardAll the methods as well as the main method/tester statements must be written in one class. DO NOT write a different class for each method. RECURSIVELY calculate the level of a Node in a tree. Print elements of all the Nodes of a tree using Pre-order Traversal. NOTE: [USE PYTHON LANGUAGE] Solve both questions if you want a thumbs-up.arrow_forwardAnswer the given question with a proper explanation and step-by-step solution. Write a recursive method(Code ) static void mirrorTree(node root) This will take a tree as input and then change the tree such that it becomes a mirror of itself. We have the following methods implemented(you can use them and assume we coded somewhere else): getLeft(), getRight(), setLeft(), setRight(), getValue(). All the values in a node are integers. [Hint: Use them, and think how you would write when the tree only has 2 children ] Example:arrow_forward
- A D F H J K L M N P Q R a. What is the post-order traversal of the tree? b. Write down all leaves. c. What is the height of node B? d. Write down all siblings of node J. e. Write down all descendants of node D.arrow_forwardHow to answer the following question screenshot shows our binary search tree For the binary search trees, the public method, size, calls the private recursive method, recSize, and passes it a reference to the root of the tree, returning to the caller whatever is returned to it by recSize. Critique the following implementation of recSize assuming its parameter variable is node: if (node == null) return 0; else if ((node.getLeft() == null) && (node.getRight() == null)) return 1; else if ((node.getLeft() == null)) return (1 + recSize(node.getRight())); else return (1 + recSize(node.getLeft())); A. The code will throw a "null pointer exception" under certain circumstances. B. The code will return an incorrect result under certain circumstances. C. The code works correctly but could be simpler. D. The code works correctly. If an application uses the remove method on an Iterator object returned by our binary search tree: A. a TreeUnderflowException is…arrow_forwardAll the methods as well as the main method/tester statements must be written in one class. DO NOT write a different class for each method. NOTE: USE PYTHON AD SOLVE BOTH QUESTIONS IF YOU WANT A THUMBS_UP. Print elements of all the Nodes of a tree using In-order Traversal. Print elements of all the Nodes of a tree using Post-order Traversal.arrow_forward
- : You are implementing a binary search tree class from scratch, which, in additionto insert, find, and delete, has a method getRandomNode() which returns a random nodefrom the tree. All nodes should be equally likely to be chosen. Design and implement an algorithmfor getRandomNode, and explain how you would implement the rest of the methods.arrow_forwardWrite a method that takes any two nodes u and v in a tree T whose root node is s, and quickly determines if the node u in the tree is a descendant or ancestor of node v.arrow_forwardThe program BinaryTreeTest.java prints the nodes by inorder traversal. Please add two methods printPreOrder(Node node) and printPostOrder(Node node) in the program and print the numbers by the two orders. Then please add findMax(Node node)and findMin(Node node) to find the maximal and minimal numbers respectively for the tree. Hint: to implement findMax(Node node), a recursive method is needed. If the node is empty. return 0. If the node's right sub-tree is null, return the node's value. Otherwise call findMax() recursively using the right child as the parameter. findMin(Node node) is similar, but you will focus on left child.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