binary-search-tree

How to merge two BST's efficiently?

故事扮演 提交于 2019-11-27 03:59:56
问题 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))? 回答1: Naaff's answer with a little more

Difference between binary tree and binary search tree

孤街浪徒 提交于 2019-11-27 02:28:57
Can anyone please explain the difference between binary tree and binary search tree with an example ? Mehrdad Binary tree: Tree where each node has up to two leaves 1 / \ 2 3 Binary search tree: Used for searching . A binary tree where the left child contains only nodes with values less than the parent node, and where the right child only contains nodes with values greater than or equal to the parent. 2 / \ 1 3 Jayzcode Binary Tree is a specialized form of tree with two child (left child and right Child). It is simply representation of data in Tree structure Binary Search Tree (BST) is a

Java generics issue: Class “not within bounds of type-variable” error.

痞子三分冷 提交于 2019-11-27 02:15:16
I'm working on a project for class that involves generics. public interface Keyable <T> {public String getKey();} public interface DataElement extends Comparable<Keyable<DataElement>>, Keyable<DataElement>, Serializable {...} public class Course implements DataElement {...} public interface SearchTree<K extends Comparable<Keyable<K>> & Keyable<K>> extends Serializable {...} public class MySearchTree implements SearchTree<Course> { ... private class Node { public Course data; public Node left; public Node right; ... } } When trying to use the Course class within the declaration of MySearchTree,

PHP daylight saving time detection

∥☆過路亽.° 提交于 2019-11-27 01:19:46
I need to send an email to users based wherever in the world at 9:00 am local time. The server is in the UK. What I can do is set up a time difference between each user and the server's time, which would then perfectly work if DST didn't exist. Here's an example to illustrate it: John works in New York, -5 hours from the server (UK) time Richard works in London, UK, so 0 hour difference with the server. When the server goes from GMT to GMT +1 (BST) at 2:00am on a certain Sunday, this means that John now has a -6H time difference now. This scenario I can still handle by updating all the users

How to construct BST given post-order traversal

我的未来我决定 提交于 2019-11-26 22:48:43
问题 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

Implementing an iterator over a binary search tree

こ雲淡風輕ζ 提交于 2019-11-26 19:08:52
问题 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

Balancing a BST

混江龙づ霸主 提交于 2019-11-26 18:58:14
问题 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

How to implement a binary search tree in Python?

孤者浪人 提交于 2019-11-26 15:24:42
问题 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

Heap vs Binary Search Tree (BST)

一笑奈何 提交于 2019-11-26 14:49:03
What is the difference between a heap and BST? When to use a heap and when to use a BST? If you want to get the elements in a sorted fashion, is BST better over heap? Ciro Santilli 新疆改造中心996ICU六四事件 Summary Type BST (*) Heap Insert average log(n) 1 Insert worst log(n) log(n) or n (***) Find any worst log(n) n Find max worst 1 (**) 1 Create worst n log(n) n Delete worst log(n) log(n) All average times on this table are the same as their worst times except for Insert. * : everywhere in this answer, BST == Balanced BST, since unbalanced sucks asymptotically ** : using a trivial modification

Store the largest 5000 numbers from a stream of numbers

爱⌒轻易说出口 提交于 2019-11-26 13:22:25
Given the following problem: "Store the largest 5000 numbers from a stream of numbers" The solution which springs to mind is a binary search tree maintaining a count of the number of nodes in the tree and a reference to the smallest node once the count reaches 5000. When the count reaches 5000, each new number to add can be compared to the smallest item in the tree. If greater, the new number can be added then the smallest removed and the new smallest calculated (which should be very simple already having the previous smallest). My concern with this solution is that the binary tree is