Calculate BST internal path length only from pre-sunset or postoperative bypass

Hello StackOverflow Community!

I am trying to figure out how to calculate the length of the internal BST path, taking into account only the preliminary or post-order (it should not matter much) without building a tree; that is, I want to use only one of the workarounds mentioned above. This may be the simple answer for most of you, but as you might think, I'm brand new to the trees.

Well, any thought is appreciated and thanks.

+3
source share
3 answers

http://geeksforgeeks.org/?p=6633, . , , ( ). , , , node ( ), , . , , node, . , ( , ):

int internal_path_length(key_iter& cur_node, key_iter end, int level, key max_key) {
  if (cur_node == end) return 0;
  key cur_key = *cur_node;
  if (cur_key > max_key) return 0;
  ++cur_node;
  int len1 = internal_path_length(cur_node, end, level + 1, cur_key);
  int len2 = internal_path_length(cur_node, end, level + 1, max_key);
  return len1 + len2 + level;
}

:

key_iter i = preorder.begin();
internal_path_length(i, preorder.end(), 0, mk);

mk , .

0

BST ( ).

Pre [R, R, R) Post [ R, R, R]

.

findIPLPreOrder(poArray,startIndex,endIndex, height) {
     if(startIndex==endIndex){
          retrn height;
     }
     m=findIndexOfEndofLeftSubTree(poArray,start,end);
     return findIPLPreOrder(poArray,start+1,m,height + 1) + findIPLPreOrder(poArray,m+1,end,height + 1);     
}

findIndexOfEndofLeftSubTree(poArray,start,end){
  R=poArray[start]
  for(i=start+1;i<=end;i++){
     if(R < poArray[i]){
         return i-1;
       }
  }
}
0

, .

   A         A
  / \        |
 B   C       B
             |
             C

(ABC), (2 3).

-1

Source: https://habr.com/ru/post/1794050/


All Articles