c++ - Understanding Red-Black Tree Traversal of STL multiset in GDB -
i trying understand rb-tree traversal in following gdb script
define pset if $argc == 0 pset else set $tree = $arg0 set $i = 0 set $node = $tree._m_t._m_impl._m_header._m_left set $end = $tree._m_t._m_impl._m_header set $tree_size = $tree._m_t._m_impl._m_node_count if $argc == 1 printf "set " whatis $tree printf "use pset <variable_name> <element_type> see elements in set.\n" end if $argc == 2 while $i < $tree_size set $value = (void *)($node + 1) printf "elem[%u]: ", $i p *($arg1*)$value if $node._m_right != 0 set $node = $node._m_right while $node._m_left != 0 set $node = $node._m_left end else set $tmp_node = $node._m_parent while $node == $tmp_node._m_right set $node = $tmp_node set $tmp_node = $tmp_node._m_parent end if $node._m_right != $tmp_node set $node = $tmp_node end end set $i++ end end
i went through stl_tree.h understand internals of rb-tree could't it. these following doubts-
is iterative in-order traversal thats happening here?
how can
$end = $tree._m_t._m_impl._m_header
. thought_m_header
root node. if not root node?if inorder traversal why first going down right sub-tree of tree doing
if $node._m_right != 0 set $node = $node._m_right
. also, printing out element right @ beginning of while loop in pre-order. when run script in gdb, print elements in sorted order makes me speculate in-order.when comes traversal, isn't rb-tree same normal bst? cant rb-tree traversed left , right pointers? why parent pointer being used here?
i think figured out.
all above questions can answered 1 observation- "tree._m_t._m_impl._m_header
not root node. node acts cache rb-tree. tree._m_t._m_impl._m_header._m_left
points leftmost node of tree, tree._m_t._m_impl._m_header._m_right
points rightmost node , tree._m_t._m_impl._m_header._m_parent
points root node of tree.
now, can see how given code indeed doing in-order traversal. reason why normal bst traversal not used here gain speed. caching leftmost node of tree, things iterator.begin()
can implemented in o(1)
instead of o(h)
h
height of tree.
Comments
Post a Comment