Vector based binary tree traversal

萝らか妹 提交于 2019-12-24 03:40:28

问题


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:


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]);
}



回答2:


preorder:

do something with the value
f(go to the left)
f(go to the right)

inorder:

f(go to the left)
do something with the value
f(go to the right)

postorder:

f(go to the left)
f(go to the right)
do something with the value


来源:https://stackoverflow.com/questions/13557469/vector-based-binary-tree-traversal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!