简体   繁体   English

一种使用O(1)在平衡的二进制搜索树中复制和存储所有条目的方法

[英]A way to copy and store all entries in a balanced binary search tree with O(1)

I am implementing a binary search tree in C++11. 我正在C ++ 11中实现二进制搜索树。 I want to add a feature that makes it possible to mark different versions of the data structure with constant time complexity. 我想添加一个功能,使它可以以恒定的时间复杂性标记不同版本的数据结构。

What I thought about was to add another property to the root node called "name", "key" or "mark" - thus being accessible with O(1). 我想到的是在根节点上添加另一个名为“名称”,“键”或“标记”的属性-因此可以通过O(1)访问。 But to save the tree version I would have to 但是要保存树版本,我必须

  1. create a copy of the root (I thought of creating a new instance of the binary search tree and simply assign the tree I want to copy to this new instance) 创建根的副本(我想到了要创建二进制搜索树的新实例,然后将要复制的树分配给该新实例)
  2. store the copied root in an array 将复制的根存储在数组中

It is sufficient if those stored roots have read-access only. 如果那些存储的根仅具有读访问权限就足够了。 But now my question is: Can I perform these 2 steps without adding time complexity? 但是现在我的问题是:我可以执行这两个步骤而不增加时间复杂度吗?

Below there is a little sketch illustrating the process. 下面有一个小图说明了该过程。

在此处输入图片说明

Thank you very much for your help. 非常感谢您的帮助。

FunkyPeanut FunkyPeanut

I think what you're after is a persistent BST . 我认为您追求的是持久的BST This allows "copying" in O(1) time (just copy a pointer), while performing all the other BST operations with the same big-O times as the vanilla BST. 这样就可以在O(1)时间内“复制”(只需复制一个指针),同时以与普通BST相同的big-O时间执行所有其他BST操作。 The main difference is that operations that used to take O(1) space now take up to O(lg n) space, at least when the old version of the tree has been stored somewhere. 主要区别在于,至少当树的旧版本已存储在某处时,以前占用O(1)空间的操作现在最多占用O(lg n)空间。

(You can implement persistent data structures in C++ quite easily with shared_ptr .) (您可以使用shared_ptr轻松地在C ++中实现持久性数据结构。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM