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...

  • 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...

  • please do this lab in Java in given instructions by tomorrow morning. TkA CHRI - TREE...

    please do this lab in Java in given instructions by tomorrow morning. TkA CHRI - TREE UTH A HEAPOF PRESDNS CENETH CSC 236-Lab 6 (2 programs) trees 1. Given the two binary trees below: 14 18 16) Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree and create a program to test this method for these 2 trees. Show the original trees and...

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {...

    In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT {    private BinaryTreeNode root;    /**    * Creates an empty binary tree.    */    public LinkedBinaryTree() {        root = null;    }    /**    * Creates a binary tree from an existing root.    */    public LinkedBinaryTree(BinaryTreeNode root) {        this.root = root;    }    /**    * Creates a binary tree with the specified element...

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
ADVERTISEMENT