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

  • Home
  • SEARCH
  • 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 8881701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:23:51+00:00 2026-06-14T20:23:51+00:00

I have a vector based binary tree and need to apply a function to

  • 0

I have a vector based binary tree and need to apply a function to each value in the tree using various methods of traversal. The preorder traversal was very easy to implement with a recursive function but I have been having trouble doing the same with the inorder and postorder traversals. If anyone could help out that would be great!

Some extra information that I should have included:
I am using a vector of nodes, each node containing a boolean variable stating whether or not that node is filled and a templated data variable. Each node is stored at an index “i” while its left child is at the index “2i+1” and the right child at “2i+2”.

To apply a preorder traversal to the list, I first processed the data stored at index 0 and then called this recursive function

template <typename Item, typename Key>
template <typename Function>
void BST<Item,Key>::preTraverse(int n, Function f) {
    if(tree[n].occupied == false) return;
    else {
        f(tree[n].data);
        preTraverse(2*i+1,f);
        preTraverse(2*i+2,f);
    }
}

twice beginning with indices 1 & 2 as my “n” parameter.

  • 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-14T20:23:52+00:00Added an answer on June 14, 2026 at 8:23 pm

    Assuming your tree is a max-populated left-dominant representation, then any given point in your array at position i will have children at positions 2*i+1 and 2*i+2. The trivial walk:

    Node   Children
    =====  ===========
    ar[0]: ar[1], ar[2]
    ar[1]: ar[3], ar[4]
    ar[2]: ar[5], ar[6]
    ar[3]: ar[7], ar[8]
    ar[4]: ar[9], ar[10] etc...
    

    Given this definition, preorder, postorder, and in-order can all be done with simple index forwarding and some checks for your ‘occupied’ flag. The following templates assume type T is a structure type that has an ‘occupied’ member.

    template<typename T>
    void preorder(const T ar[], size_t i, size_t count, void (&visit)(const T&))
    {
        if (i>=count || !ar[i].occupied)
            return;
    
        visit(ar[i]);
        preorder(ar, 2*i+1, count, visit);
        preorder(ar, 2*(i+1), count, visit);
    }
    
    template<typename T>
    void inorder(const T ar[], size_t i, size_t count, void (&visit)(const T&))
    {
        if (i>=count || !ar[i].occupied)
            return;
    
        inorder(ar, 2*i+1, count, visit);
        visit(ar[i]);
        inorder(ar, 2*(i+1), count, visit);
    }
    
    template<typename T>
    void postorder(const T ar[], size_t i, size_t count, void (&visit)(const T&))
    {
        if (i>=count || !ar[i].occupied)
            return;
    
        postorder(ar, 2*i+1, count, visit);
        postorder(ar, 2*(i+1), count, visit);
        visit(ar[i]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector of unordered_map which is sorted based on the comparer function
I have a handle-based class that I need to create a vector of. An
I have a vector where I keep an incrementing data. Normally each element of
I have a small 3D vector class in C# 3.0 based on struct that
Say, I have a string and a vector of bool s. Based on the
I am attempting to create a custom vector based map using tiles, much like
I have two vector e and g . I want to know for each
The math I'm using to compute a 3D vector based on a mouse click
Problem I have timestamped data, which I need to search based on the timestamp
I have a numeric vector implementation in C; the whole implementation is based on

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.