Concept explainers
(Printing Trees) Write a recursive function outputTree to display a binary tree on the screen. The function should output the tree row-by-row with the top of the tree at the left of the screen and the bottom of the tree toward the right of the screen. Each row is output vertically. For example, the binary tree illustrated in Fig12.22. is output as follows:
Note that the rightmost leaf node appears at the top of the output in the rightmost column, and the root node appears at the left of the output. Each column of output starts five spaces to the right of the previous column. Function outputTree should receive as arguments a pointer to the root node of the tree and an integer totalSpaces representing the number of spaces preceding the value to be output (this variable should start at zero so that the root node is output at the left of the screen). The function uses a modified inorder traversal to output the tree. The
While the pointer to the current node is not NULL.
Recursively call outputTree with the current node’s right subtree and totalSpaces + 5.
Use a for statement to count from 1to totalSpaces Output the value in the current node.
Recursively call outputTree with the current nodes left subtree and totalSpaces + 5.
Want to see the full answer?
Check out a sample textbook solutionChapter 12 Solutions
C How to Program (8th Edition)
Additional Engineering Textbook Solutions
Java: An Introduction to Problem Solving and Programming (7th Edition)
Programming in C
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
Introduction to Programming Using Visual Basic (10th Edition)
Problem Solving with C++ (10th Edition)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
- Information to solve question: Use "a" for loop to calculate the sum of squares of values in this list: 1, 7, 8, 6, 11. (The answer should be 271) Create a recursive function to do the same calculation a in the previous question. (The function input will be the list. Each recursion, you are going to send a sub-list with one less item from the list) ----------------------------------------------------------------------------------- (1) Create a function to perform bubble sorting in python? The algorithm should stop when there are no swaps in the last iteration.arrow_forward17: recursion.cpp) Write two recursive integer functions. The first function should calculate factorials using the definition 0! = 1 and n! = (n-1)! x n. In addition, if n is too large it should be able to detect integer overflow before it happens. The next function should calculate the greatest common factor (gcf) of two numbers. If the second number is zero, then the gcf is the other number. Otherwise, the gcf of a and b is the same as the gcf of b and a mod b.arrow_forward(Kruskal’s algorithm) The text introduced Prim’s algorithm for finding a minimum spanning tree. Kruskal’s algorithm is another well-known algorithm for finding a minimum spanning tree. The algorithm repeatedly finds a minimumweight edge and adds it to the tree if it does not cause a cycle. The process ends when all vertices are in the tree. Design and implement an algorithm for finding an MST using Kruskal’s algorithm.This is a java code questionarrow_forward
- C Proggraming Found in the Shuffle (Strings) An ordinary deck of playing cards contains 52 cards, each of which has a suit and a value. Most card games require that a deck be shuffled at the beginning of a play. However, sometimes the deck isn't shuffled well enough, and there are arrangements of cards in the deck which may cause a bias in the play. Two such arrangements are a same-suit sequence and an ascending sequence. A same-suit sequence is simply a sequence of consecutive cards in the deck with the same suit. An ascending sequence is a sequence of consecutive cards in the deck that follow one another in increasing value, with Ace following King and preceding two. Thus, 2S, 5S, KS, 3S AS is a same-suit sequence of length five, and 9C, 10D, JC, QS, KH, AC, 2D is an ascending sequence of length seven, and 2H 3H 4H 5H 6H is both a same-suit and ascending sequence of length five. The Problem Given a deck of cards, determine the length of the longest same-suit sequence and the…arrow_forwardProblem-28 Recursively remove all adjacent duplicates: Given a string of characters, recursively remove adjacent duplicate characters from string. The output string should not have any adjacent duplicates.arrow_forwardGroup B Q1) A vector is given by x = [-3.5-5 6.2 11 0 8.1-903-132.5]. Using conditional statements and loops, write a program that creates two vectors from x-one (call it P) that contains the positive elements of x, and a second (call it N) that contains the negative elements of x. In both P and N, the elements are in the same order as in x.arrow_forward
- (a) List at least 8 distinct strings that are in S. You must use each recursive rule at least twice. (b) List at least 2 strings made up of only as and bs that are not in S.arrow_forward#python codecoud you provide comments and little explanation it would really help me to understandthankyouarrow_forwardCodeW For fun X C Solved https://codeworkou... 臺亂 CodeWorkout X272: Recursion Programming Exercise: Is Reverse For function isReverse, write the two missing base case conditions. Given two strings, this function returns true if the two strings are identical, but are in reverse order. Otherwise it returns false. For example, if the inputs are "tac" and "cat", then the function should return true. Examples: isReverse("tac", "cat") -> true Your Answer: 1 public boolean isReverse(String s1, String s2) { 2. if > 3. 4. else if > return true; return false; 5. 6. else { String s1first = String s2last return s1first.equals (s2last) && 51. substring(0, 1); s2, substring(s2.length() 1); 7. 8. 6. isReverse(s1.substring(1), s2.substring(0, s2.length() 1)); { 12} 1:11AM 50°F Clear 12/4/2021arrow_forward
- Problem 2 -- Recursive Palindrome (Grey + Scarlet) Write a recursive method, isPalindrome, which takes a String as a parameter, and returns true if the String is a palindrome. For the purposes of this method, you may assume Strings with a length of o or 1 are palindromes.arrow_forwardQuestion Info: The calculation "a" was used for loop to calculate the sum of squares of values in this list: 1, 7, 8, 6, 11 (The answer was 271) Question To Answer: (1) Create recursive function to do the same calculation a in the above. (The function input will be the list. Each recursion, you are going to send a sub-list with one less item from the list) I need help doing this in python programming language.arrow_forward1.) Design a Binary Search Tree ADT by using following functions. Include the function definitions & run the program (Kindly include header files as well). Paste the output as a screenshot and write the code in your answer sheet. insert(int val)find(int x)************************************************************************************ class btNode { public: int info; btNode *lLink; btNode *rLink; btNode(int e, btNode *l = NULL, btNode *r = NULL) { info = e; lLink = l; rLink = r; } btNode() { lLink = NULL; rLink = NULL; } }; class binarySTADT { private: btNode *root; int count = 0; public: binarySTADT() { root = NULL; } void insert(int val); // function to insert a given value in the tree.…arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning