Find the median in two AVL BSTs in O(log(n))

心不动则不痛 提交于 2020-04-16 04:54:44

问题


So im attempting the classic problem of finding the median in two AVL BST's in O(log(n)) time. Given two AVLs, with a combined size of n (integers are distributed randomly i.e. one may have n-1 elements with the other having 1). I am currently trying to come up with an algorithm to solve this. Each node, besides holding its integer value and pointers to its parent and child nodes, holds the size of the subtree for the subtree rooted at it. I've approached it by initially finding the number of elements n (we assume this to always be odd) via the subtree sizes of each AVL tree's root. Let the subtrees be A and B, where AL is the left subtree of A and AR is the right etc. I've begun by operating on the AVL tree of a larger size and then comparing AL and AR's sizes. With this approach im trying to rule out the existance of the median in one of the subtrees but im currently confused on how to do this. Any ideas? Thanks

来源:https://stackoverflow.com/questions/60927275/find-the-median-in-two-avl-bsts-in-ologn

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