Question

Extend the array based Binary Tree lab by adding these methods. (Remember 2i + 1 &...

Extend the array based Binary Tree lab by adding these methods.

(Remember 2i + 1 & 2i + 2)

Preorder(int)

-Recursive method that prints all the nodes in a VLR pattern.

Inorder(int)

-Recursive method that prints all the nodes in a LVR pattern.

Postorder(int)

-Recursive method that prints all the nodes in a LRV pattern.

C++, no libraries are allowed

0 0
Add a comment Improve this question Transcribed image text
Answer #1


#include
using namespace std;

void inorder(int a[], int root, int n )
{
if(root<n)
{
inorder(a,2*root+1, n);
cout<<a[root]<<" ";
inorder(a,2*root+2, n);
}
return ;
}
void preorder(int a[], int root, int n )
{
if(root<n)
{
  
cout<<a[root]<<" ";
preorder(a,2*root+1, n);
preorder(a,2*root+2, n);
}
return ;
}
void postorder(int a[], int root, int n )
{
if(root<n)
{
postorder(a,2*root+1, n);
postorder(a,2*root+2, n);
cout<<a[root]<<" ";
}
return ;
}
int main() {
int a[7]={1,2,3,4,5,6,7};
int n = 7;
inorder(a,0,n);cout<<endl;
preorder(a,0,n);cout<<endl;
postorder(a,0,n);cout<<endl;
  
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Extend the array based Binary Tree lab by adding these methods. (Remember 2i + 1 &...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Write a program in C to show how to traverse a tree or visit each...

    1. Write a program in C to show how to traverse a tree or visit each node in the tree exactly once? Use the following figure to implement the three methods, LVR (inorder), LRV (postorder), VLR (preorder). 17 1819 14 12 8 Figure 5.15: Binary tree with arithmetic expression

  • JAVA Given the sequence of in-order traversal for a general binary tree, stored in an array...

    JAVA Given the sequence of in-order traversal for a general binary tree, stored in an array inOrder[] = {9, 5, 1, 7, 2, 12, 8, 4, 3, 11 }. And given the sequence of post-order traversal for the same tree, stored in an array postOrder[] = {9, 1, 2, 12, 7, 5, 3, 11, 4, 8}. Please implement the following method: static BinaryTree buildTree(Object inOrder[], Object postOrder[]) -The method buildTree() returns a BinaryTree object in memory that is constructed on...

  • 1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the...

    1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. 2) Extend the Binary Search Tree ADT to include a public method singleParent-Count that returns the number of nodes in the tree that have only one child. 3) The Binary search tree ADT is extended to include a boolean method similarTrees that receives references to two binary trees and determines whether the shapes of the trees are...

  • java A University would like to implement its students registery as a binary search tree, calledStudentBST....

    java A University would like to implement its students registery as a binary search tree, calledStudentBST. Write an Student node class, called StudentNode, to hold the following information about an Student: - id (as a int) - gpa (as double) StudentNode should have constructors and methods (getters, setters, and toString()) to manage Write the StudentBST class, which is a binary search tree to hold objects of the class StudentNode. The key in each node is the id. : import.java.ArrayList; public...

  • C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N...

    C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N + 1 NULL pointers, half the space allocated in a binary search tree for pointer information is wasted. Suppose that if a node has a NULL left child, we make its left child pointer link to its inorder predecessor, and if a node has a NULL right child, we make its right child pointer link to its inorder successor. This is known as a...

  • in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...

    in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...

  • Create a binary tree class. -Data array field (private) -Count field Add method(data) -The add method...

    Create a binary tree class. -Data array field (private) -Count field Add method(data) -The add method takes a data point, add it to the count position in the array then increases the count. Print method(void) -Prints all the fields in the tree. C++, no libraries are allowed (only )

  • Please help this. Q. When is the right subtree of a binary tree processed before the...

    Please help this. Q. When is the right subtree of a binary tree processed before the left?                 When using an inorder traversal algorithm.                          When using a preorder traversal algorithm.                          When using a postorder traversal algorithm.                        Never. The left always goes before the right in the standard traversal algorithms. Q. In a postorder tree traversal, each node is processed ____ its children.                              instead of                            after                      before                  relative to the ordinal values of Q17. If you have an array of 4,000 sorted...

  • Code in C++ Instructions Write a program which... Prompt the user to enter the number of...

    Code in C++ Instructions Write a program which... Prompt the user to enter the number of values that they would like to insert into a binary search tree. Prompt the user to insert, one-at-a-time, a value into the binary search tree. Print the preorder, postorder, and inorder traversal on the tree. Binary Search Tree The methods preorder, postorder, inorder, preorderR, postorderR, and inorderR will all contains the necessary screen output statements to verify that the traversal is happening correctly (see...

  • Write a (void) method to traverse a binary tree in Breadth-First order, where you start from...

    Write a (void) method to traverse a binary tree in Breadth-First order, where you start from the root, print the node values of its children, (left and then right) (these are at level 1), then print all node values at level 2 from left to right, and then level 3 etc. For the example binary tree below, you should get the result A, B, E, C, F, D, G, H. Notice that this type of traversal is not the same...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT