binary-search-tree

Deletion procedure for a Binary Search Tree

江枫思渺然 提交于 2019-12-04 09:46:38
问题 Consider the deletion procedure on a BST, when the node to delete has two children. Let's say i always replace it with the node holding the minimum key in its right subtree. The question is: is this procedure commutative? That is, deleting x and then y has the same result than deleting first y and then x? I think the answer is no, but i can't find a counterexample, nor figure out any valid reasoning. EDIT: Maybe i've got to be clearer. Consider the transplant(node x, node y) procedure: it

Second max in BST

心已入冬 提交于 2019-12-04 08:59:06
问题 This is an interview question. Find the second max in BST. The max element is the rightmost leaf in the BST. The second max is either its parent or its left child. So the solution is to traverse the BST to find the rightmost leaf and check its parent and left child. Does it make sense? 回答1: Recall that you can list the nodes of a BST in reverse order by doing a modified inorder traversal where you explore the right subtree first. This leads to a simple algorithm: Node rightmost =

maximum length of a descending path in a tree which always goes left|right

送分小仙女□ 提交于 2019-12-04 08:52:41
I'm prepearing for tech interview, so basically learning algorithms from very beginning :) we are given a BST. I need to find the max length of a desc path in it, which always goes left or right In other words, an example tree's descending path is 2, ie 15-10-6 5 / \ 2 15 / 10 / \ 6 14 I'm very new to algorithmic problems.what are my steps to solving this? My idea was to use DFS and a counter to store the longest path. but I can't figure out how to employ recursion to do the job, whereas recursion seems more natural for this data structure. any suggestions/directions? The wording is a little

Find median in O(1) in binary tree

痴心易碎 提交于 2019-12-04 08:17:44
Suppose I have a balanced BST (binary search tree). Each tree node contains a special field count , which counts all descendants of that node + the node itself. They call this data structure order statistics binary tree . This data structure supports two operations of O(logN): rank(x) -- number of elements that are less than x findByRank(k) -- find the node with rank == k Now I would like to add a new operation median() to find the median. Can I assume this operation is O(1) if the tree is balanced? If the tree is complete (i.e. all levels completely filled), yes you can. Unless the tree is

For a given binary tree find maximum binary search sub-tree

瘦欲@ 提交于 2019-12-04 08:07:40
问题 For a given binary tree, find the largest subtree which is also binary search tree? Example: Input: 10 / \ 50 150 / \ / \ 25 75 200 20 / \ / \ / \ / \ 15 35 65 30 120 135 155 250 Output: 50 / \ 25 75 / \ / 15 35 65 回答1: This answer previously contained an O(n log n) algorithm based on link/cut trees. Here is a simpler O(n) solution. The core is a procedure that accepts a node, the unique maximum BSST rooted at its left child, the unique maximum BSST rooted at its right child, and pointers to

how to determine a balanced or perfectly balanced Binary search tree ( just from the picture )

大兔子大兔子 提交于 2019-12-04 07:43:39
I am not sure how to determine if a tree is balanced, perfectly balanced, or neither if I have it as a picture not a code For example if I have this tree How can I check if it's balanced, perfectly balanced, or unbalanced? and can someone give me an example of a perfectly balanced tree? [o] / \ [b] [p] \ / \ [d] [m] [r] Clearly I can tell that the tree is unbalanced if it was something like this: [b] \ [d] \ [r] \ [c] However, if it was something very similar to the one above I don't know how to get it This is a perfectly balanced and balanced tree: [k] / \ [A] [p] / \ [N] [R] Can someone

Recursive Binary Search Tree Insert

北慕城南 提交于 2019-12-04 06:26:43
So this is my first java program, but I've done c++ for a few years. I wrote what I think should work, but in fact it does not. So I had a stipulation of having to write a method for this call: tree.insertNode(value); where value is an int. I wanted to write it recursively, for obvious reasons, so I had to do a work around: public void insertNode(int key) { Node temp = new Node(key); if(root == null) root = temp; else insertNode(temp); } public void insertNode(Node temp) { if(root == null) root = temp; else if(temp.getKey() <= root.getKey()) insertNode(root.getLeft()); else insertNode(root

Height of binary search tree in constant time

不打扰是莪最后的温柔 提交于 2019-12-04 05:28:34
问题 I need to fine the height of a binary search tree with a time of O(1) the only way i could think to do this is to put a check in the add and remove methods incrementing a global counter is there any other way? 回答1: O(1) time suggests that you should already have the height when it is requested. The best way is it to keep/update the correct value whenever a new node is added/deleted . You are doing it in a right way , however it increases the complexity on addition and deletion. You can do it

Is a node in a tree considered its own ancestor?

偶尔善良 提交于 2019-12-04 03:21:03
I'm wondering what the consensus is on the definition of "ancestor" in a computer science context. I only ask because in Introduction to Algorithms , Second Edition, p. 259 there is a description of the algorithm Tree-Successor(x) that seems odd. In finding the successor of node x , [...] if the right subtree of node x is empty and x has a successor y , then y is the lowest ancestor of x whose left child is also an ancestor of x . In a binary search tree with a root having key 2 and children 1 and 3 , the successor of 1 is its parent 2 . In this case, x is the left child of x 's successor, y .

Balanced Binary Search Tree

被刻印的时光 ゝ 提交于 2019-12-04 02:36:28
问题 I need to build a balanced binary search tree. So far my program inserts the numbers from 1 to 26, but my program does not build it into a balanced binary search tree. If anyone could look at my code and help me out it would be much appreciated. public class TreeNode { TreeNode leftTreeNode, rightTreeNode;// the nodes int data; //int size; public TreeNode(){//Constructer leftTreeNode = null; rightTreeNode = null; } public TreeNode(int newData){//Constructer with new Data coming in for