13 Replies - 402 Views - Last Post: 08 December 2017 - 09:43 AM Rate Topic: -----

#1 PROGRAMMINGisLIFE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 22-May 17

It must copy the binary search tree into array and print it

Posted 07 December 2017 - 02:28 PM

It does not print the all values
#include <iostream>
using namespace std;

struct node {
    int value;
    node *left, *right;
};
node *root;

class search_tree {


public:
    search_tree()
    {
        root=NULL;
    }
    void copy(node *from_tree, int to_heap[] ,int i);
    node *insert(node *m_node, int key);
    void print(int to_heap[], int size);

};

node *newNode(int item)
{
    node *temp=new node;
    temp->value=item;
    temp->left=NULL;
    temp->right=NULL;
    return temp;
}



node *search_tree::insert(node *m_node, int key)
{
    if(m_node==NULL)
    {
        return newNode(key);

    }
    if(key<m_node->value)
    {
        m_node->left=insert(m_node->left, key);
    }
    if(key>m_node->value)
    {
        m_node->right=insert(m_node->right, key);
    }
    return m_node;

}


void search_tree::copy(node *from_tree, int to_heap[], int i)
{

    if(from_tree==NULL)
    {
        return;
    }
    to_heap[i]=from_tree->value;
    i++;

    copy(from_tree->left, to_heap, i);
    copy(from_tree->right, to_heap, i);

}

void search_tree::print(int to_heap[], int size)
{
    for(int j=0; j<size; j++)
    {
        cout<<to_heap[j]<<endl;
    }
}
int main()
{
    search_tree tree;
    node *root=NULL;
    root=tree.insert(root, 50);
    tree.insert(root, 30);
    tree.insert(root, 20);
    tree.insert(root, 40);
    tree.insert(root, 70);
    tree.insert(root, 60);
    tree.insert(root, 50);
    int size=7;
    int to_heap[size]={};
    int i=0;
    tree.copy(root, to_heap, i);
    tree.print(to_heap, size);

}



Is This A Good Question/Topic? 0
  • +

Replies To: It must copy the binary search tree into array and print it

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13657
  • View blog
  • Posts: 54,519
  • Joined: 12-June 08

Re: It must copy the binary search tree into array and print it

Posted 07 December 2017 - 02:34 PM

Come on dude.. provide a bit more information. What is it doing? What should it be doing? What's the input? Are there error messages?
Was This Post Helpful? 0
  • +
  • -

#3 PROGRAMMINGisLIFE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 22-May 17

Re: It must copy the binary search tree into array and print it

Posted 07 December 2017 - 02:45 PM

Consider below a fragment of the
header file of a C++ class search_tree:
class search_tree {
private:
struct node {
int value;
record *left, *right;
};
node *root;
};
Write a private function void search_tree::copy(node* from_tree, int to_heap[]) that copies all int
values from a sub-tree rooted at node* from_tree into a heap represented by an array int to_heap[]. Assume
the array has sufficient capacity. If necessary, add required arguments.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13657
  • View blog
  • Posts: 54,519
  • Joined: 12-June 08

Re: It must copy the binary search tree into array and print it

Posted 07 December 2017 - 02:50 PM

Okely dokely.
Was This Post Helpful? 0
  • +
  • -

#5 PROGRAMMINGisLIFE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 22-May 17

Re: It must copy the binary search tree into array and print it

Posted 07 December 2017 - 02:56 PM

what is my okely dokely problem here?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6444
  • View blog
  • Posts: 26,064
  • Joined: 12-December 12

Re: It must copy the binary search tree into array and print it

Posted 07 December 2017 - 02:58 PM

How To Ask Questions The Smart Way
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon

Reputation: 5358
  • View blog
  • Posts: 16,731
  • Joined: 25-December 09

Re: It must copy the binary search tree into array and print it

Posted 07 December 2017 - 03:14 PM

Does that code even compile, it shouldn't?

Here is what my compiler says about your program:

||=== Build: Debug in testcpp (compiler: gcc 6.1.0) ===|
main.cpp||In constructor ‘search_tree::search_tree()’:|
main.cpp|error: ‘root’ was not declared in this scope|
main.cpp||In function ‘int main()’:|
main.cpp|error: ISO C++ forbids variable length array ‘to_heap’ [-Wvla]|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|




Why the global variables?


Jim
Was This Post Helpful? 0
  • +
  • -

#8 PROGRAMMINGisLIFE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 22-May 17

Re: It must copy the binary search tree into array and print it

Posted 07 December 2017 - 03:18 PM

it compiles
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon

Reputation: 5358
  • View blog
  • Posts: 16,731
  • Joined: 25-December 09

Re: It must copy the binary search tree into array and print it

Posted 07 December 2017 - 03:23 PM

Maybe with your broken compiler. but with any truly standard compatible compiler it will fail to compile, I suggest you fix the issues I pointed out and then repost your code and ask specific questions and that modified code.

Array sizes must be compile time constants in C++!


Jim
Was This Post Helpful? 0
  • +
  • -

#10 PROGRAMMINGisLIFE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 22-May 17

Re: It must copy the binary search tree into array and print it

Posted 07 December 2017 - 04:04 PM

broken compiler????? -_-
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5949
  • View blog
  • Posts: 20,393
  • Joined: 05-May 12

Re: It must copy the binary search tree into array and print it

Posted 07 December 2017 - 11:51 PM

Yes, a non-broken compiler will follow the C++ standards. The C++ standard does not support the use of VLAs. You are using VLAs in your code and you said it compiles successfully.
Was This Post Helpful? 0
  • +
  • -

#12 Thomas1965  Icon User is online

  • D.I.C Regular

Reputation: 71
  • View blog
  • Posts: 302
  • Joined: 09-September 16

Re: It must copy the binary search tree into array and print it

Posted 08 December 2017 - 09:23 AM

I think the term "broken compiler" is misleading - particular for non-native speakers of English.
It's more likely a compiler that has warnings turned off.
If you use GCC 7.2 with g++ -std=c++17 Main.cpp -oMain.Exe it will compile and run, but I never heard that GCC is broken.
Was This Post Helpful? 0
  • +
  • -

#13 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 1406
  • View blog
  • Posts: 4,454
  • Joined: 20-March 10

Re: It must copy the binary search tree into array and print it

Posted 08 December 2017 - 09:24 AM

But you as a user can break it, usually by modifying it with some add on...
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon

Reputation: 5358
  • View blog
  • Posts: 16,731
  • Joined: 25-December 09

Re: It must copy the binary search tree into array and print it

Posted 08 December 2017 - 09:43 AM

Quote

If you use GCC 7.2 with g++ -std=c++17 Main.cpp -oMain.Exe it will compile and run, but I never heard that GCC is broken.

IMO, if you tell the compiler to use a particular standard then it should enforce that standard. However it takes several other compile flags to truly enforce the requested standard. IMO, you should, when developing programs, always use the following compile switches as a minimum: g++ -Wall -Wextra -pedantic -pedantic-errors -std=c++14 main.cpp -o main.exe. The -pedantic flags are important because they are what forces gcc to stop using compiler hacks (such as VLA) and to really follow the chosen standard. The -std= flag only tells the compiler to use the features added by the particular chosen standard. I also use C++14 as the base because C++17 is not yet a thing and the level of actual support varies widely depending on the compiler maker (gcc/visual ++, etc) and the particular version of that particular compiler.


Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1