binary-search-tree

Inserting an equal value element

别来无恙 提交于 2019-12-03 22:11:14
I am currently studying binary search trees, and I was wondering what do you do if you try to insert an element that is the same value as the root? Where does it go? The definition of BST is that it is a ordered set, thus duplicates are not allowed to be inserted. This is usually due to more complex structures being built atop the BST. Depending on the desired behavior, you may want to throw an exception, error or silently ignore when duplicates are inserted. However, depending on your comparison function you can store duplicates on the left or right subtree, but remember to keep your

deletion in a binary search tree

那年仲夏 提交于 2019-12-03 20:08:52
问题 I have been given two binary search trees. For example, A and B. Next, I was asked to delete the tree B from the tree A. By deletion, I mean delete all the nodes present in B from A. Note: B is not necessarily a subtree of A. eg: A: 50 / \ 10 75 / / \ 1 60 90 B: 10 / \ 1 75 Resulting tree should be: 50 \ 60 \ 90 Two approaches came to my mind: A1: node* deleteTree(node* A, node* B) ; Take the root of tree B. Delete this node from tree A(by normal BSt deletion method). Next divide the problem

Find number of permutations of a given sequence of integers which yield the same binary search tree

拈花ヽ惹草 提交于 2019-12-03 17:47:38
问题 Given an array of integers arr = [5, 6, 1] . When we construct a BST with this input in the same order, we will have "5" as root, "6" as the right child and "1" as left child. Now if our input is changed to [5,1,6], our BST structure will still be identical. So given an array of integers, how to find the number of different permutations of the input array that results in the identical BST as the BST formed on the original array order? 回答1: Your question is equivalent to the question of

Hash table - implementing with Binary Search Tree

五迷三道 提交于 2019-12-03 17:09:32
From Cracking the Coding Interview , page 71: Alternatively, we can implement hash table with a BST. We can then guarantee an O(log n) lookup time, since we can keep the tree balanced. Additionally we may use less space, since a large array no longer needs to be allocated in the very beginning. I know the basics of linked lists, hash tables and BSTs, but I am unable to understand these lines. What does it actually mean? Would this final data structure would be a Trie? The full text of that section states, with the last paragraph being the one you asked about: A hash table is a data structure

Find the parent node of a node in binary search tree

牧云@^-^@ 提交于 2019-12-03 17:05:34
So I want to find the parent node of a Node in a binary tree. Suppose that I input 30,15,17,45,69,80,7 in the tree through a text file. The tree should be 30 15 45 7 17 69 80 And here is my code : Node* BST::searchforparentnode(Node* pRoot, int value) { if(pRoot->pleft == NULL && pRoot->pright == NULL) return NULL; if(pRoot->pleft->value == value || pRoot->pright->value == value) return pRoot; if(pRoot->value > value) return searchforparentnode(pRoot->pleft,value); if(pRoot->value < value) return searchforparentnode(pRoot->pright,value); } In this case i'm not consider if the user input the

A sequence that forms the same AVL and splay trees?

旧巷老猫 提交于 2019-12-03 14:45:54
Is there such a sequence of numbers (1-7, all numbers used, only once each), that would form equal AVL and splay tree? Well, in the interests of science, I implemented both AVL and splay trees in Python based on their respective Wikipedia articles. Assuming I didn't make a mistake somewhere, my finding is that there are no permutations of {1, ..., 7} that produce the same AVL and splay tree . I conjecture the same is true for all sets of size k > 3. As to the fundamental reasons for this, I have no idea. If someone would like to vet my code, here it is: ##################### # Class

how to rebuild BST using {pre,in,post}order traversals results

删除回忆录丶 提交于 2019-12-03 14:05:03
问题 We know the pre-order, in-order and post-order traversals. What algorithm will reconstruct the BST? 回答1: Because it is BST, in-order can be sorted from pre-order or post-order <1>. Actually, either pre-order or post-order is needed only.... <1> if you know what the comparison function is From pre-order and in-order , to construct a binary tree BT createBT(int* preOrder, int* inOrder, int len) { int i; BT tree; if(len <= 0) return NULL; tree = new BTNode; t->data = *preOrder; for(i = 0; i <

BST with duplicates

ε祈祈猫儿з 提交于 2019-12-03 13:46:55
I know that, BST does not allow duplicates. For example, if I have a word "RABSAB". The Binary search tree for the above string is: R /\ A S \ B What if we wanted to include the duplicates in the tree. How the tree gonna change? I was asked this question in an interview. They asked me to draw: a binary tree an unbalanced Binary Search Tree a binary search tree without duplicates a binary search tree with duplicates Any Help is appreciated! PS: Help me by drawing the related trees Sazzadur Rahaman Rule to insert in a binary Search tree without duplicate is: Go left if element is less than root

Equality of two binary search trees constructed from unordered arrays

≡放荡痞女 提交于 2019-12-03 12:36:21
Given two unsorted arrays of size N each, we are to determine if the Binary Search Tree constructed from them will be equal or not. So, the elements of the array are picked and inserted into a basic (no balancing, no red-black, nothing) binary search tree. Given directly two arrays, can we determine whether they give rise to the same Binary Search Tree. There is an obvious O(N 2 ) worst case time complexity solution: Construct two trees, and compare their equality. Is there a O(N) or an O(N log N) solution to this? The idea of the problem that I am trying to extract is: The construction of the

Algorithm- Sum of distances between every two nodes of a Binary Search Tree in O(n)?

妖精的绣舞 提交于 2019-12-03 12:12:53
The question is to find out sum of distances between every two nodes of BinarySearchTree given that every parent-child pair is separated by unit distance. It is to be calculated after every insertion. ex: ->first node is inserted.. (root) total sum=0; ->left and right node are inserted (root) / \ (left) (right) total sum = distance(root,left)+distance(root,right)+distance(left,right); = 1 + 1 + 2 = 4 and so on..... Solutions I came up with: Brute-force. Steps: perform a DFS and track all the nodes : O(n) . Select every two nodes and calculate : O(nC2)_times_O(log(n))=O(n2log(n)) distance