binary-search-tree

Does Cocoa Touch have a search tree data structure?

佐手、 提交于 2019-11-28 03:00:51
问题 I've been looking into this on Google and read the Collections entry in the SDK documentation, and turned up nothing. Is there a BST (any of its variants) implementation available out of the box with the iOS SDK? It seems odd that something so basic would be missing from a major development platform. Is their hash implementation just that magical? Or do the devs assume no one is going to do inserts/deletes on things that have an order? I can use NSSet for now, as I know most of us (myself

Is there any technical reason why std::lower_bound is not specialized for red-black tree iterators?

半腔热情 提交于 2019-11-27 23:27:06
I have always assumed that std::lower_bound() runs in logarithmic time if I pass a pair of red-black tree iterators ( set::iterator or map::iterator ) to it. I had to burn myself twice to notice that std::lower_bound() runs in O(n) time in this case, at least with the libstdc++ implementation. I understand that the standard doesn't have the concept of red-black tree iterators; std::lower_bound() will treat them as bidirectional iterators and advance them in linear time. I still don't see any reason why the implementation couldn't create an implementation specific iterator tag for red-black

How to calculate the depth of a binary search tree

半腔热情 提交于 2019-11-27 21:22:04
I would like to calculate the summation of the depths of each node of a Binary Search Tree. The individual depths of the elements are not already stored. Something like this: int countChildren(Node node) { if ( node == null ) return 0; return 1 + countChildren(node.getLeft()) + countChildren(node.getRight()); } And to get the sum of the depths of every child: int sumDepthOfAllChildren(Node node, int depth) { if ( node == null ) return 0; // starting to see a pattern? return depth + sumDepthOfAllChildren(node.getLeft(), depth + 1) + sumDepthOfAllChildren(node.getRight(), depth + 1); } Now for a

How to construct BST given post-order traversal

大憨熊 提交于 2019-11-27 19:30:33
I know there are ways to construct a tree from pre-order traversal (as an array). The more common question is to construct it, given the inorder and pre-order traversals. In this case, although the inorder traversal is redundant, it definitely makes things easier. Can anybody give me an idea how to do it for a post-order traversal? Both iterative and recursive solutions are required. I tried to do it iteratively using stack, but couldn't at all get the logic right, so got a horrible messy tree. Same went for recursion. Daniel Fischer If you have an array from a post-order traversal of a BST,

Implementing an iterator over a binary search tree

扶醉桌前 提交于 2019-11-27 17:43:12
I've been coding up a bunch of different binary search tree implementations recently (AVL, splay, treap) and am curious if there's a particularly "good" way to write an iterator to traverse these structures. The solution I've used right now is to have each node in the BST store pointers to the next and previous elements in the tree, which reduces iteration to a standard linked-list iteration. However, I'm not really satisfied with this answer. It increases the space usage of each node by two pointers (next and previous), and in some sense it's just cheating. I know of a way of building a

Balancing a BST

泄露秘密 提交于 2019-11-27 17:20:25
Reference: I was asked this question @MS SDE interview, 3rd round. And it's not a homework problem. I also gave it a thought and mentioning my approach below. Question: Modify a BST so that it becomes as balanced as possible. Needless to mention, you should do it as efficient as possible. Hint: Interviewer said that this is a logical question, if you think differently you will get the answer. No difficult coding involved. --> Having said that, I do not think he was expecting me to point to AVL/RB Trees. My Solution: I proposed that, I would do inorder traversal of tree, take middle element as

How to implement a binary search tree in Python?

非 Y 不嫁゛ 提交于 2019-11-27 11:02:41
This is what I've got so far but it is not working: class Node: rChild,lChild,data = None,None,None def __init__(self,key): self.rChild = None self.lChild = None self.data = key class Tree: root,size = None,0 def __init__(self): self.root = None self.size = 0 def insert(self,node,someNumber): if node is None: node = Node(someNumber) else: if node.data > someNumber: self.insert(node.rchild,someNumber) else: self.insert(node.rchild, someNumber) return def main(): t = Tree() t.root = Node(4) t.root.rchild = Node(5) print t.root.data #this works print t.root.rchild.data #this works too t = Tree()

How to merge two BST's efficiently?

半城伤御伤魂 提交于 2019-11-27 06:34:25
How to merge two binary search trees maintaining the property of BST? If we decide to take each element from a tree and insert it into the other, the complexity of this method would be O(n1 * log(n2)) , where n1 is the number of nodes of the tree (say T1 ), which we have splitted, and n2 is the number of nodes of the other tree (say T2 ). After this operation only one BST has n1 + n2 nodes. My question is: can we do any better than O(n1 * log(n2))? Naaff's answer with a little more details: Flattening a BST into a sorted list is O(N) It's just "in-order" iteration on the whole tree. Doing it

How to fix remove in RedBlackTree implementation?

爷,独闯天下 提交于 2019-11-27 05:32:41
Here is the implementation of RedBlackTree I am using (from Mark Allen Weiss, Data Structures public class RedBlackTree<AnyKey extends Comparable<? super AnyKey>, AnyValue extends Comparable<? super AnyValue>> implements MyTreeMap<AnyKey, AnyValue>{ private static final int BLACK = 1; private static final int RED = 0; // The psuedo(bogus) root, has a key value of negative infinity and a right link to the real root. private RedBlackNode<AnyKey, AnyValue> header; // Used in place of a null link. Will always be colored black. private RedBlackNode<AnyKey, AnyValue> nullNode; private RedBlackNode

How to calculate the depth of a binary search tree

杀马特。学长 韩版系。学妹 提交于 2019-11-27 04:31:30
问题 I would like to calculate the summation of the depths of each node of a Binary Search Tree. The individual depths of the elements are not already stored. 回答1: Something like this: int countChildren(Node node) { if ( node == null ) return 0; return 1 + countChildren(node.getLeft()) + countChildren(node.getRight()); } And to get the sum of the depths of every child: int sumDepthOfAllChildren(Node node, int depth) { if ( node == null ) return 0; // starting to see a pattern? return depth +