Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postorder traversal in stl map

Tags:

c++

gcc

map

stl

I am using a stl map on gcc comper which uses a tree to store key,value pairs. The iterator advances in an inorder fashion so inorder traversal is really easy. However one of my output requirements is a postorder traversal. I have been specifically aksed to use map. Is there any way to get it done?

like image 542
Abhijeet Verma Avatar asked Oct 11 '12 10:10

Abhijeet Verma


1 Answers

There is no standard way to access the "actual tree structure" of an instance of std::map.

Furthermore, the standard doesn't know (or care) exactly how the elements of a map are arranged in any internal tree that the map might use. Red-Black trees and AVL trees are both valid implementations of std::map, and you'd get a different postorder traversal according to which is in fact used. In practice I expect its always R-B or very similar, but the implementation freedom informs the interface defined by the standard.

In short, std::map is not a tree, it's an abstract data structure that can be (and is) implemented using a tree.

It might be possible to hack a particular implementation, but probably best not to. If you want the tree structure of your data to be part of the defined state of your program, you could perhaps write your own tree.

like image 173
Steve Jessop Avatar answered Sep 30 '22 03:09

Steve Jessop