Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8206043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:31:04+00:00 2026-06-07T08:31:04+00:00

Consider I already have a function called int* binToArrayInOrder(TreeRoot* tr) which creates a sorted

  • 0

Consider I already have a function called int* binToArrayInOrder(TreeRoot* tr) which creates a sorted array of tree values (because it’s in order).

Is there anyway to construct back the tree from the in order given array, without other information such as the same tree representation in pre order array?

How can I write the array to a text file in C, please show me code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T08:31:09+00:00Added an answer on June 7, 2026 at 8:31 am

    [1] You can reinsert the array elements into a binary tree again. Depending on the balancing algorithm, the tree may not look exactly like it did when you extracted them into the array, though.

    [2] How about this?

    void print_array (int *array, size_t sz, FILE *f) {
        if (!sz) return;
        fprintf(f, "%d\n", *array);
        print_array(array+1, sz-1, f);
    }
    

    From your comments, your actual question is how to save a binary tree to disk, and then restore it. This is a data structure serialization problem. For this problem, an in-order walk is probably not what you want. Instead, the serialization should reflect how the data structure is laid out. So, you need a record that describes a binary node:

    struct binary_node_file_data {
        char data_[MAX_BINARY_NODE_DATA_SIZE];
        int parent_;
    };
    

    Now, you can perform a pre-order traversal of your binary tree to populate your nodes.

    struct binary_node_fila_data *bfd = malloc(sizeof(*bfd)*nodeCount);
    int count = 0;
    populate_binary_node_file(tree, bfd, &count, -1);
    
    void populate_binary_node_file(binary_tree_t *tree,
                                   struct binary_node_file_data *bfd,
                                   int *count,
                                   int parent) {
        if (tree) {
            int me = *count;
            *count += 1;
            export_binary_node_data(tree, &bfd[me], parent);
            populate_binary_node_file(tree->left_subtree, bfd, count, me);
            populate_binary_node_file(tree->right_subtree, bfd, count, me);
        }
    }
    

    Here, I expect -1 to be treated like a NULL pointer. Then, dump the bfd to a file. I’ll leave restoring the tree as an exercise. Reflecting on the problem a little more, it doesn’t really matter if the traversal is pre-order or in-order (or post-order). The restoration step just needs to be able to allow all the children to find the parent so that they can populate the left and right pointers properly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following scenario. We have a C++ function with a static local variable:
Consider this is a ListView that shows files and folders, I have already wrote
I have a button that already has an onclick-event and an assigned function. I
Consider the following C#: // C# .net switch(x) { case 1: for(int i =
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
I've got some basic questions about C++. Consider the following code in which I
Consider I have the following graph: A -> B B -> C C ->
Please use my previous question as reference. I have marked an answer already. Question
What I have is basically a problem which is easily solved with multiple tables,
I've got several elements on a HTML page which have the same class -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.