Question

In: Computer Science

Exercise 1: (Divide and Conquer: Binary tree traversal) Write pseudocode for one of the classic traversal...

Exercise 1: (Divide and Conquer: Binary tree traversal)

Write pseudocode for one of the classic traversal algorithms (preorder, inorder, and postorder) for binary trees. Assuming that your algorithm is recursive, find the number of recursive calls made.

Solutions

Expert Solution

Preorder Pseudocode

Algorithm

Algorithm Preorder(T)
//Implements the preorder traversal of a binary tree
//Input: Binary tree T (with labeled vertices)
//Output: Node labels listed in preorder
if T = ∅
print label of T’s root
Preorder(TL) //TL is the root’s left subtree
Preorder(TR) //TR is the root’s right subtree

Inorder Pseudo code

Algorithm Inorder(tree)
//Implements the preorder traversal of a binary tree
//Input: Binary tree T (with labeled vertices)
   1. Traverse the left subtree, i.e., call Inorder(left-subtree)
   2. Visit the root.
   3. Traverse the right subtree, i.e., call Inorder(right-subtree)
Algorithm Postorder(tree)
//Implements the preorder traversal of a binary tree
//Input: Binary tree T (with labeled vertices)
   1. Traverse the left subtree, i.e., call Postorder(left-subtree)
   2. Traverse the right subtree, i.e., call Postorder(right-subtree)
   3. Visit the root.

Comment Down if any Query

Pleause Thumbs up if you satisfy with Answer


Related Solutions

1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element...
1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element in an array of n numbers. 5. Find the order of growth for solutions of the following recurrences. a . T(n)=4T(n/2)+n,T(1)=1 b. T(n)=4T(n/2)+n2,T(1)=1 c. T(n)=4T(n/2)+n3,T(1)=1
A: Write a divide-and-conquer program to solve the following problem:
in Java A: Write a divide-and-conquer program to solve the following problem:     1. Let A[1..n] and B[1..n] be two arrays of distinct integers, each sorted in an increasing order.      2. Find the nth smallest of the 2n combined elements. Your program must run in O(log n) time. For example: n = 4If A[1..n] = {2, 5, 8, 9} and B[1..n] = {1, 4, 6, 7}The nth (i.e. 4th) smallest integer is 5.If A[1..n] = {2, 5, 8, 13}...
Make the following assumptions: Assume a binary search tree holds integer numbers Write a pseudocode for...
Make the following assumptions: Assume a binary search tree holds integer numbers Write a pseudocode for a binary tree search algorithm that searches in the tree (starting at root) for a node which meets the following requirements, and prints a message when found: (a) has a value that is one third as large as either its left or right child node. Think carefully about what steps are needed to do the search, and review the insert and search methods for...
Create an array-based implementation of a binary tree. (WRITE IN JAVA) DON'T FORGET TO INCLUDE PSEUDOCODE...
Create an array-based implementation of a binary tree. (WRITE IN JAVA) DON'T FORGET TO INCLUDE PSEUDOCODE AND UML DIAGRAM
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds...
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds the largest and second largest value from a vector of ints. void LargestTwo (vector l, int left, int right, int & largest, int & secondLargest) • Please write comment for your functions, similar to the one in the pseudocode, to include both pre- and post-conditions. • Comment on the logic of your code, e.g., what is true after the two recursive calls? • Answer...
Create a Binary Search Tree for the following data and do In-order, Preorder and Post-order traversal...
Create a Binary Search Tree for the following data and do In-order, Preorder and Post-order traversal of the tree. 50, 60, 25, 40, 30, 70, 35, 10, 55, 65, 5 Write an algorithm to delete a node in Singly Linked List                            [12 Write an algorithm of Binary Search                                                              [10] Write a program in ‘C’ to generate Fibonacci series using recursion            [8]
(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...
Write a method for binary tree in Python that can determine whether a binary tree is...
Write a method for binary tree in Python that can determine whether a binary tree is a binary search tree or not. The input should be a binary tree. The output should be true or false. True= binary tree meets the criteria to be a binary search tree. False= does not meet the criteria to be a binary search tree.
1. programmers often refer to a _____ search as a "divide and conquer" procedure a. bubble...
1. programmers often refer to a _____ search as a "divide and conquer" procedure a. bubble b. binary c. division d. split 2. you can add an item at any point in a _______ container and the array size expands automatically to accomodate the new item a. arraylLst b. Array c. ResizableArray d. array 3.int[][] myVals = {{2,4,6,8},{20,40,60,80}; Using the above two dimensional array, what is the value of myVals[1][2]? a. 4 b.60 c. 6 d.40
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT