This is my program, but I don't know how to show the binary search tree results in array (one dimensional). I use random as inputs. How to show binary search tree results in arrays? Help me please.

```
#include<iostream>
#include<algorithm>
using namespace std;
struct BstNode{
int data;
BstNode* left;
BstNode* right;
};
BstNode* GetNewNode(int data){
BstNode* newNode = new BstNode();
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
BstNode* Insert(BstNode* root, int data){
if(root == NULL){
root = GetNewNode(data);
}
else if(data <= root->data){
root->left = Insert(root->left,data);
}
else{
root->right = Insert(root->right,data);
}
return root;
}
int main(){
int x, i, n, data;
BstNode* root = NULL;
cout<<"The number of data : ";cin>>n;
for (i=1;i<=n;i++) {
data=(rand()%100+1);
cout<<data<<" ";
Insert(root,data);
}
}
```

I guess OP is looking to fill an array with the elements of BST as observed in in-order traversal. If so, we can try something like the following.

```
void
fillArrayInorder( BstNode const * pCurr, int * pArr, int & arrIdx ) {
if( pCurr ) {
fillArrayInorder( pCurr->left, pArr, arrIdx );
pArr[ arrIdx++ ] = pCurr->data;
fillArrayInorder( pCurr->right, pArr, arrIdx );
}
}
```

Call it as

```
int arr[ MAX_ELEMS ];
int arrIdx = 0;
fillArrayInorder( root, & arr, arrIdx );
```

Note: Instead of the raw array, it might be better to use a `vector`

and its `push_back()`

method, so that the length and index updating complexity is not necessary in the application code.

Similar Questions

I've given the result of in-order traversal of a binary tree (not binary search tree) as: E, D, B, A, G, F, H, C Now I've to find out the result of the post-order traversal of the same tree for which

I want to make a generic BST, that can be made up of any data type, but i'm not sure how I could add things to the tree, if my BST is generic. All of my needed code is below. I want my BST made up of

Please explain the difference between a binary search tree and m-way tree?

I'm currently trying to implement an immutable BST in clojure. This is my make-tree function: (defn make-tree [v] {:v v :l nil :r nil}) and insertion: (defn insert [tree v] (if (nil? tree) (make-tree

Introduction I'm building an HTML5 web application that creates a visual representation of a binary search tree from a given list of numbers. Currently, I have an algorithm which calculates the visual

I read on here of an exercise in interviews known as validating a binary search tree. How exactly does this work? What would one be looking for in validating a binary search tree? I have written a bas

I have written a functioning binary search tree in java apart from one key function, the search. I am using the logic that i will check if the root is null, then if the term i want to search for is eq

Well i build a basic Binary Search Tree using a class called Node for simplicity i will include the core method that is used to insert Nodes public function addNode($node) { if ($this->left == null

So I've looked around a lot for some information on how to take elements from a binary search tree and write them to a file, and then later read them back into the tree as a type of load function. I

I'm currently writing a program that will delete a node from a binary search tree (among other things, but the deletion is my issue) and I'm having a problem with the final step of deletion, specifica

I am having a huge issue with my recursive function in my binary search tree. My project is due in a few hours and I cannot for the life of me get ahold of my instructor. My function seems to only be

Hello the problem I am having with this question is making a function that uses only the binary tree head pointer and the desired level(height) of the tree, something like: int countLevel(tree_type tr

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'm having a little bit of trouble understanding how to do a binary search with strings. What I have as input is a sentence in the command line prompt which is argv[1], (argv[0] being the name of the

I have a problem with this Binary Search Tree. After that i cut a leave, if I try to show the tree VisualStudio answer me this message An unhandled exception of type 'System.AccessViolationException'

I made a binary search tree in Java but I'm having troubles whit the deleting nodes part. I managed to erase the node when it has only 1 son, and I have the idea to make the deletion when it has 2 son

I have this code for reconstructing a binary search tree from a flattened list of pre-order`traversal elements. I see this code working but unable to understand how. Here is the code: public static No

I have 2 classes, NonEmptyTree and EmptyTree that implement the Tree interface. toString() method in NonEmptyTree class should return a string: key=>value key=>value key>value etc... I do not

I'm trying to traverse on a binary tree to find someone's ID by using his/her ID number. When I debug this function it works well, but on the other hand, when I directly run, it terminates itself.. Ca

I define a binary tree as a search table. The tree is a linked list binary tree, the node is like typedef struct node{ int key; char *buf; ... }node_t; typedef table_t node_t *; so I have functions l

I'm currently writing a binary search tree to hold a list of songs that I want to be arranged by name. I do the usual recursion, using strcmp to compare the title of the current node of the tree (song

This is probably a simple task for expert coders, but is it possible to recursively traverse a binary unordered tree to find a node? I can do this for a binary search tree, but I'm struggling with how

When implementing a Hashtable using an array, we inherit the constant time indexing of the array. What are the reasons for implementing a Hashtable with a Binary Search Tree since it offers search wit

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

Does anyone know how to calculate the complexity of a nested binary search tree? I have implemented a nested binary search tree to a depth of 3 BSTs. EDIT: I apologize for the confusion, I had meant

I am working on a binary search tree in C++ at the moment and I have reached the stage where I have to write the remove/delete function(using recursive approach, x = change(x)). I have two options: t

for my coursework(binary search tree and hashtables) I would like to make a java program that scans a text file and orders words based on the most frequent words. Something like most popular tags. Exa

Just wanted to know how efficient is the below algorithm to find the lowest common ancestor of two nodes in a binary search tree. Node getLowestCommonAncestor (Node root, Node a, Node b) { Find the in

I'm trying to delete from a binary search tree and keep getting this error in debugger and am not sure what to do to correct it. Is this code correct? Program received signal EXC_BAD_ACCESS, Could no

This question already has an answer here: How do you validate a binary search tree? 18 answers Given a simple binary tree, how can we prove that tree is a binary search tree? When we traverse a

Copying directory from source tree to binary tree. For example: How to copy www to bin folder. work ├─bin └─src ├─doing │ └─www ├─include └─lib Thanks.

I am trying to create a program which which takes a list of words as an input, and sorts them into a binary tree, in order to allow them to be found, e.g. like a dictionary. This is what I have done s

I am trying to connect the siblings of the binary search tree. You can find the logic in the Connect() method. My question is, is there any better way to do it? Am I overdoing by using two queues to i

I'm currently working on a school project. The assignment is to write the implementation of a templated binary search tree given the corresponding header file. My issue is that since it's templated,

I am trying to define a simple binary search tree. It is stored in lists like so: [Key, Left Tree, Right Tree]. I'm having issue with my logic. This is what I have bstadd(K, [], [K,[],[]]). bstadd(K,

I believe that my operator overloading is not working for one of my two datatypes. MY program works with an int datatype but not with my defined class students. students is a tree of pointers so this

Binary search tree algorithms usually uses recursion and i m having hard time with recursion. This is a code which converts the tree into its mirror image . void mirror_image(struct tree* node1) { if

I've been trying to write a recursive string method for a binary search tree that returns a multiple line representation of a tree with preorder path info. Each node should be prefaced by a series of

When removing a node with two children in a binary search tree, how many times can the recursive removal of the child node be called?

This question already has an answer here: java Arrays.binarySearch fails to find target 3 answers search in a binary tree 2 answers I am trying to search an array for a value and decided to

I know we can preprocess and turn any binary search tree into a perfect binary tree using DSW algorithm or red black balanced trees. How do these two methods differ time-complexity wise? Can you provi

For a given binary tree, find the largest subtree which is also binary search tree? Example: Input: 10 / \ 50 150 / \ / \ 25 75 200 20 / \ / \ / \ / \ 15 35 65 30 120 135 155 250 Output: 50 / \ 25

These days I come across several Google search results that contain sites with links that exactly match my search words. How is it possible for the sites to dynamically change their content or rather

So this is my first java program, but I've done c++ for a few years. I wrote what I think should work, but in fact it does not. So I had a stipulation of having to write a method for this call: tree.i

As a programming exercise I need to rewrite some existing methods and classes that make up a binary tree. (The method signatures and constructors have to stay the same). Somehow I don't really seem to

I'm having a pretty confusing problem in building a binary tree. Apparently this should be an easy task but somehow I may mess up with the pointers in it. Here's the simplified code (of course it's n

I'm trying to write a size() method for a polymorphic binary search tree. I.e. A BST that has classes EmptyTree and NonEmptyTree that both implement a tree interface. EmptyTree is being used to repr

I am getting an stack overflow when the program tries to add a number. It seems to be accessing a nonexistent binary search tree. Here is the error code: Unhandled exception at 0x01084DD9 in CIS 350 q

I have to implement a sparse matrix (a matrix that has predominantly zeroes, so you only record values different than 0), but I have to implement it using a binary search tree. EDIT: So now I'm thinki

I was wondering how to save a Binary Tree that I have previously created. Does anyone know how to do it? Thank you so much. PD: Here there is a link about how to implement a binary tree, I am using th