Equality of two binary search trees constructed from unordered arrays

后端 未结 4 1974
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-15 02:24

Given two unsorted arrays of size N each, we are to determine if the Binary Search Tree constructed from them will be equal or not.

So, the elements of

4条回答
  •  不知归路
    2021-02-15 03:12

    "Construct two trees and compare" does not have to be O(N^2). You can use an auxilliary data structure that lets you find the position of the new node in O(log N) instead of O(N), so that the construction of the BST is O(N log N) even if the BST being constructed is not balanced.

    With each empty position (i.e. a free child slot in a node) pos in a BST, there is an associated interval (a_pos,b_pos) (one of the values might be +/- infinity), such that new node for value v will be created at pos if and only if the value is in the interval.

    You can store the intervals in a balanced interval tree, so that the position for each new arriving value can be found in O(log N). The update of the interval tree is also O(log N), as you only replace one interval with two.

    (Actually, the intervals never overlap, so the auxilliary structure can be a plain old (balanced) BST instead of an interval tree.)

    Example:

    Take the following non-balanced BST, constructed for an array prefix [1,10,2,9,3, ...]

      1
     /  \
    a  10
       / \
      2   f
     / \
    b   9
       / \
      3   e
     / \
    c   d
    

    The letters a-f denote the possible places where a new node can be placed (nil leaves). With each of the letter, there's an associated interval, as follows:

    [-inf,1] -> a
    [1,2] -> b
    [2,3] -> c
    [3,9] -> d
    [9,10] -> e
    [10, +inf] -> f
    

    A new node for a value v will be added into the BST at the place determined by the interval that v belongs to. Zero will end up at a, 5 at d and so on. The key idea is to store this information outside of the tree.

    If you can efficiently represent the above table (with links to the actual tree nodes), adding new node to the tree will take O(access to table) + O(1). The O(1) represents adding the node into the non-balanced BST, given that you already know where you place it. Adding 5 will not require comparing with 1,10,2,9 and 3 but instead will be looked up in the table and and placed directly at d.

    Once you place the new node, you obviously also have to update the table. The data structure to represent the table could be an interval tree ( http://en.wikipedia.org/wiki/Interval_tree ).

提交回复
热议问题