binary-search-tree

Create a balanced binary search tree from a stream of integers

坚强是说给别人听的谎言 提交于 2019-12-20 13:30:19
问题 I have just finished a job interview and I was struggling with this question, which seems to me as a very hard question for giving on a 15 minutes interview. The question was: Write a function, which given a stream of integers (unordered), builds a balanced search tree. Now, you can't wait for the input to end (it's a stream), so you need to balance the tree on the fly. My first answer was to use a Red-Black tree, which of course does the job, but i have to assume they didn't expect me to

Why implement a Hashtable with a Binary Search Tree?

耗尽温柔 提交于 2019-12-20 12:25:05
问题 When implementing a Hashtable using an array, we inherit the constant time indexing of the array. What are the reasons for implementing a Hashtable with a Binary Search Tree since it offers search with O(logn)? Why not just use a Binary Search Tree directly? 回答1: If the elements don't have a total order (i.e. the "greater than" and "less than" is not be defined for all pairs or it is not consistent between elements), you can't compare all pairs, thus you can't use a BST directly, but nothing

converting a binary search tree to doubly linked list

ⅰ亾dé卋堺 提交于 2019-12-20 10:55:56
问题 This question was asked in a recent coding interview. Q : Given a binary tree, write a program to convert it to a doubly linked list. The nodes in the doubly linked list are arranged in a sequence formed by a zig-zag level order traversal My approach i could always do the zig-zag level order traversal of the tree and store it in an array an then make a double linked list. but the question demands for a in-place solution. can anyone help in explaining the recursive approach should be used? 回答1

Is it always possible to turn one BST into another using tree rotations?

北战南征 提交于 2019-12-20 10:25:43
问题 Given a set of values, it's possible for there to be many different possible binary search trees that can be formed from those values. For example, for the values 1, 2, and 3, there are five BSTs we can make from those values: 1 1 2 3 3 \ \ / \ / / 2 3 1 3 1 2 \ / \ / 3 2 2 1 Many data structures that are based on balanced binary search trees use tree rotations as a primitive for reshaping a BST without breaking the required binary search tree invariants. Tree rotations can be used to pull a

2 binary trees are equal or not [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-20 10:10:03
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Determine if two binary trees are equal Got an interview yesterday, a question got me, here it is: Description There are 2 binary trees , check if they are equal. They are equal if and only if tree1->child == tree2->child , and one tree's left and right children can be swapped with each other . For example: 5 6 / \ / \ they are equal. 1 2 2 1 5 6 / \ / \ they are equal. 1 2 2 1 / \ / / 3 4 4 3 Any ideas are

Difference between binary search and binary search tree?

六眼飞鱼酱① 提交于 2019-12-20 08:22:08
问题 What is the difference between binary search and binary search tree? Are they the same? Reading the internet it seems the second is only for trees (up to 2 children nodes) and binary search doesn't follow this rule. I didn't quite get it. 回答1: Binary Search Trees A node in a binary tree is a data structure that has an element, and a reference to two other binary trees, typically called the left and right subtrees. I.e., a node presents an interface like this: Node: element (an element of some

Difference between binary search and binary search tree?

会有一股神秘感。 提交于 2019-12-20 08:22:06
问题 What is the difference between binary search and binary search tree? Are they the same? Reading the internet it seems the second is only for trees (up to 2 children nodes) and binary search doesn't follow this rule. I didn't quite get it. 回答1: Binary Search Trees A node in a binary tree is a data structure that has an element, and a reference to two other binary trees, typically called the left and right subtrees. I.e., a node presents an interface like this: Node: element (an element of some

How to find height of BST iteratively?

穿精又带淫゛_ 提交于 2019-12-20 07:09:18
问题 public void HeightIterative() { int counter = 0; int counter2 = 0; TreeNode current=root; if(current != null) { while(current.LeftNode!=null) { counter++; current = current.LeftNode; } while(current.RightNode!=null) { counter2++; current = current.RightNode; } } int res = 1+Math.Max(counter, counter2); Console.WriteLine("The Height Of Tree Is: "+res); } I wrote iterative method, to calculate height of tree. but in some cases its not working properly. As in case: 10 1 2 3 4 5 18 17 16 15 14 13

A complete Binary Search Tree with level order insert in Java

大憨熊 提交于 2019-12-20 06:20:47
问题 We got an assignment where we need to code: A Binary Search Tree The Tree has to be complete , not perfect (which means all nodes which are not on the lowest level or second lowest level should have 2 children, while the nodes on the lowest level should be as far left as possible) We need to insert to the tree in level order So if I have an Array with elements {0, 1, 2, 3, 4, 5, 6, 7} the root should be 4 , with 2, 1, 3, 0 on the left side, and 6, 5, 7 on the right side. The level order

Count number of smaller values while inserting into binary search tree (BST)

佐手、 提交于 2019-12-20 02:16:48
问题 I'm currently implementing an algorithm in which I need to know how many numbers, from the ones that have already been read, are smaller than the one that's currently being processed. A way to do that is through merge sort, but I'm more interested in a BST approach. This procedure is meant to compute in O(log n) the number of nodes with value less than the key node's value : countLessThan(int x, node T) if T = null return if T.value >= x countLessThan(x, T.left) // T.left contains only