I'm trying to create a program to work with binary search trees and my instructor has this function set up with this declaration, which must stay the same.

```
void BSTType<T>::traverse(std::ostream& out, TravType trav) const{
```

this is currently how i have it implemented, but what is the std::ostream& out used for and how do i use this is my main program? I don't understand why we need this as a parameter and not the current tree I am working with. please help soon. this is due at 8pm and my instructor won't help. if more info is needed just let me know in comments or something.

```
template <class T>
void BSTType<T>::traverse(std::ostream& out, TravType trav) const{
if(trav==IN)
inorder(root);
else if(trav=PRE)
preorder(root);
else
postorder(root);
}
```

this is the idea i have for calling this function from my main program

```
tree.traverse(???, IN);
```

this should output the tree using inorder traversal by calling the inorder(root) function. I don't understand what goes where the question marks are

here are my functions that traverse can call, depending on what travType the user enters

```
void BSTType<T>::inorder(BTNodeType<T>* node) const{
if (node!= NULL){
inorder(node->left);
std::cout << node->item << " ";
inorder(node->right);
}
}
template <class T>
void BSTType<T>::postorder(BTNodeType<T>* node) const{
if (node!= NULL){
postorder(node->left);
postorder(node->right);
std::cout << node->item << " ";
}
}
template <class T>
void BSTType<T>::preorder(BTNodeType<T>* node) const{
if(node!=NULL){
std::cout << node->item<< " ";
preorder(node-> left);
preorder(node-> right);
}
}
```

**edit:** for some reason this function isn't copying my tree correctly, anyone know why?

```
template <class T>
void BSTType<T>::copy(BTNodeType<T>*& node1, BTNodeType<T>* node2){
if(node2==NULL)
node1=NULL;
else{
node1=new BTNodeType<T>;
node1->item=node2->item;
copy(node1->left, node2->left);
copy(node1->right, node2->right);
}
}
```

```
std::ostream& out
```

is the output stream, used to append the different node information when visited, in order to be able to print the whole tree in the order it was visited.

The algorithm should be something like this: `out << node.getValue()`

. This would append the current value to the output string. According to each algorithm, the order of the calls will change, but they will all result in a different order of this 3 actions: append the node's information, then call left or right child.

`inorder(root);`

should be replaced by `inorder(out, root);`

Inside inorder it should have: `out << root.getValue()`

to append the value

Add: `using namespace std;`

so you can call `tree.traverse(cout, IN);`

to use the standard output

`tree.traverse(???, IN);`

I don't understand what goes where the question marks are.

You could just put:

??? = `std::cout`

If you want to print at the standard output (i.e., it needs an output stream).

Also it's recommended to pass the output stream as input argument to the member functions you posted, that is your member functions will become:

```
void BSTType<T>::inorder(BTNodeType<T>* node, std::ostream& out) const{
if (node!= 0){
inorder(node->left);
out << node->item << " ";
inorder(node->right);
}
}
template <class T>
void BSTType<T>::postorder(BTNodeType<T>* node, std::ostream& out) const{
if (node!= 0){
postorder(node->left);
postorder(node->right);
out << node->item << " ";
}
}
template <class T>
void BSTType<T>::preorder(BTNodeType<T>* node, std::ostream& out) const{
if(node!=0){
out << node->item<< " ";
preorder(node-> left);
preorder(node-> right);
}
}
```

Then `traverse`

will become:

```
template <class T>
void BSTType<T>::traverse(std::ostream& out, TravType trav) const{
if(trav==IN)
inorder(root, out);
else if(trav=PRE)
preorder(root, out);
else
postorder(root, out);
}
```

Similar Questions

What is the difference between binary search and binary search tree? Are they the same? reading the internet it seams the second is only for trees(up to 2 children nodes) and binary search doesn't fol

Is there a method to build a balanced binary search tree? Example: 1 2 3 4 5 6 7 8 9 5 / \ 3 etc / \ 2 4 / 1 I'm thinking there is a method to do this, without using the more complex self-balancing t

Can anyone please explain the difference between binary tree and binary search tree with an example?

I need help in understanding this interview question: Q: find an algorithm to find the ‘next’ node (e.g., in-order successor) of a given node in a binary search tree where each node has a link to its

I want to perform level-order traversal of a binary tree. Hence, for a given tree, say: 3 / \ 2 1 / \ \ 4 6 10 the output would be: 3 2 1 4 6 10 I understand that I could use some sort of queue, bu

I am trying to quickly implement a Binary search tree in Java. What is the best class to use that has methods for in-order traversal? (I have heard of TreeMap class. But it looks like the class does n

Show that every n-node binary search tree is not equally likely (assuming items are inserted in random order), and that balanced trees are more probable than straight-line trees. How is it prove mathe

I am working on BST and right now trying tree traversal.My inorder traversal output is coming correct by pre order and post order output is not coming correct. My code is public class binarytree { sta

I am not getting the same result by executing the recursive and non recursive preorder traversal on a binary search Tree recursive method public static void preorder(TreeNode root) { if (root == null)

How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space.

Good evening all, I've been tasked with designing a function in Python that will build a Binary Search tree. When I walk through the function myself, it seems to make perfect sense and it SHOULD work

I am trying to print the size of my binary search tree. However, what my code is doing now is printing what level each node is at. I feel as though I am putting my count += 1 in the wrong place or per

I'm having issues implementing my breadth first traversal for my binary tree. I keep getting a class cast exception at these two lines: if(node.left != null) queue.offer(node.left); if(node.right !=

First off, this is homework, so putting that out there. I'm supposed to implement a binary search tree with specific methods: void insert(String), boolean remove(String), and boolean find (String). I

I am reading through the binary tree delete node algorithm used in the book Data Structures and Algorithms: Annotated Reference with Examples on page 34 , case 4(Delete node which has both right and l

Hey everyone! I am studying Data Structures in java and I am having difficulty with using generics in Binary Search Trees. For our assignment we are to implement a Binary Search Tree using nodes that

I want to write the algorithm of Balanced Binary Search Tree with backtracking would you please guild me about it? I dont know how should I implement it. I dont want any code I need just explanation.

Ok, I am trying to get a binary search tree to balance, and I know why it's not working, but I don't know how to fix it. This is what I have for my balancing methods. public void balance(){ if(isEmpt

when removing a node with two children, and if instructed to use the standard binary search tree node removal algorithm, should we replace it with the smallest node of the right subtree or the largest

I'm working on a programming assignment and writing a bunch of functions to implement a binary search tree, and some functions are given. I thought I understood recursion, but I keep getting hung up o

Is there a way you can have a Binary Search Tree with Object nodes, that store multiple values, and which have the ability to treat one of these values as the main variable that will be used for com

As our term project, we're implementing a binary search tree. The thought behind it is as follows: Assume a bst with 3 nodes: 10 / \ / \ 8 14 Its address representation is as follows (value, left no

If I am attempting to minimize the height of a Binary Search Tree, are these the correct steps?: 1) produce a sorted array from the tree 2) reconstruct the tree by adding the sorted elements into the

I have already put in the following methods for a binary search tree: import java.util.Collections; import java.util.NoSuchElementException; import java.util.ArrayList; public class MyTree { private c

I am having some problems printing an inOrder traversal of my binary tree. Even after inserting many items into the tree it's only printing 3 items. public class BinaryTree { private TreeNode root; pr

I'm trying to find a way that I could take a binary tree class and traverse through its nodes, performing X amount of inline actions on each node without having to rewrite the same traversal code over

Consider a binary search tree, where all keys are unique. Nodes haven't parent pointers. We have up to n/2 marked nodes. I can delete all of them at O(n^2) time ( using postorder traversal and when en

I'm having trouble interpreting a certain question about inserting elements to a binary search tree. I'm familiar with preorder, postorder, and inorder traversals, but I'm unfamiliar with the followin

I am getting confused on my binary search algorithm. I have to insert the following integers into an empty binary tree in the given order using put(): 4,1,3,5,2. Im not 100 percent sure if I went abou

For inorder binary search tree traversal there is an iterative algorithm that uses no auxiliary memory (stack, parent pointers, visited flags) known as Morris Traversal. Is there a similar algorithm f

In preparation for the data structures midterm, the professor gave us last year's test, one question which deals with re-arranging an example tree into a complete binary search tree. I've tried severa

How do I implement a sorted array into a binary search tree and find the Max depth. I can do each step separately but I am having trouble implementing them together. How would you implement an algorit

I'm implementing an AVL Tree (a self-balancing Binary Search Tree) in PHP and have things working pretty normally. I have in-order, pre-order, and level-order iterators working, but I can't figure out

Can anybody explain the answer for binary search, A binary search tree (BST) is built by inserting tree following values in the given order: 4,25,15,12,20,70,40. The Post Order Traversal will be A. 12

I built a function that needs to add a node to a binary search tree that is sorted by its ID (moviecode = id) but it doesn't work right. can you help me figure out whats wrong with the code? Node *bui

I'm trying to fill in a binary search tree with a text file, but i'm having alot of trouble implementing my insert function. Am i reading the input correctly or is it my code? Code for reading file: i

In the below code, I'am creating a binary tree using insert function and trying to display the inserted elements using inorder function which follows the logic of In-order traversal.When I run it, num

Inorder traversal of a Binary Search Tree yields nodes in increasing order. But what advantages do pre order and post order traversals have on any binary tree? EDIT:What I mean by advantages is : any

I am trying to implement an inorder traversal which in every stage I'll get the current node. For example: ?- getnodesinorder(tree(1,nil,nil),X). X = 1 ; false. ?- getnodesinorder(tree(5,tree(4,tree(1

Given a sequence of numbers, I want to insert the numbers into a balanced binary tree such that when I do a inorder traversal on the tree, it gives me the sequence back. How can I construct the insert

I am working on a template for a binary search tree and I am getting the error below. I know there are similar post but I can't figure this thing out. Templates are definitely a weak subject for me so

I have a binary search tree consists of nodes like : struct ProductNode { Product data; ProductNode* left; ProductNode* right; }; and i have a delete function that takes ProductNode pointer parameter

given a set of integers find out how many unique binary search trees can be constructed out of it??? according to me the answer depends on the size of the integer set.. if the size of the set integer

I am studying Breadth First Search , and wanted to ask that Is the Tree constructed by Breadth First Search ( the BFS tree, wherein we store each node's predecessor) a Binary Tree?

Welcome! I have a recursive public static method named less that takes a tree node (an original binary tree, not really a search tree) and a int parameter that returns if all the values in the tree ar

I am currently learning java by writing my programs from C++ to Java. I am trying to print the data using recursive binary search tree, but its not printing Here is my code: public class PersonRec { i

For a school project I am trying to make a binary search tree at the same time we are supposed to learn how to use 'friendship' in classes. The errors I get while compiling are: [I put comments in cod

What's the best way to create a balanced binary search tree from a sorted singly linked list?

I am reviewing for my final and one of the practice problem asks to implement a function that puts a value into a binary search tree in Python. Here is the Tree implementation I am using. class Tree(o

This is Exercise 15.5-4 of Introduction to Algorithms, 3rd edition, which is about Knuth's improvement to the DP approach to Optimal Binary Search Tree. The DP algorithm of Optimal Binary Search Tree