binary-tree

Finding the largest subtree in a BST

夙愿已清 提交于 2019-12-08 23:04:31
问题 Given a binary tree, I want to find out the largest subtree which is a BST in it. Naive approach: I have a naive approach in mind where I visit every node of the tree and pass this node to a isBST function. I will also keep track of the number of nodes in a sub-tree if it is a BST. Is there a better approach than this ? 回答1: I have posted the full solution and explanation in my blog: http://www.leetcode.com/2010/11/largest-binary-search-tree-bst-in.html The idea is to do a depth-first

Getting a random number from a binary tree in O(log n) time

≯℡__Kan透↙ 提交于 2019-12-08 19:42:48
问题 Is it possible to get a uniformly distributed random value (calling the function means it's equally likely to get any value in the tree) from a balanced binary search tree in O(log n) time? My initial idea was to generate a random number 0, 1, or 2. If 0, take the left path from the current node, if 1, take the right path, otherwise the value of the node is the random value. If you hit a leaf node, take the value of that node. I don't think this would be randomly distributed though. This is

Binary Trees, arrays vs linked

爷,独闯天下 提交于 2019-12-08 17:26:43
问题 In general, binary tree based abstractions can be implemented either using actual linked node objects, where each node has pointers to it's two children, or an array, where the children of node in index k are 2k and 2k+1. Other than the small extra memory overhead of nodes, the complexity in general seems to be identical. Are there any concrete advantages of one over the other? Anecdotally, I've seen that binary heaps tend to use the array implementation, while binary search trees tend to use

Find whether a tree is a binary search tree in Haskell

扶醉桌前 提交于 2019-12-08 16:19:36
问题 type BSTree a = BinaryTree a data BinaryTree a = Null | Node (BinaryTree a) a (BinaryTree a) deriving Show flattenTree :: BinaryTree a -> [a] flattenTree tree = case tree of Null -> [] Node left val right -> (flattenTree left) ++ [val] ++ (flattenTree right) isBSTree :: (Ord a) => BinaryTree a -> Bool isBSTree btree = case btree of Null -> False tree -> (flattenTree tree) == sort (flattenTree tree) What I want to do is to write a function to determine whether the given tree is a binary search

Why isn't std::set just called std::binary_tree? [closed]

拜拜、爱过 提交于 2019-12-08 14:23:55
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 8 months ago . std::set in C++ is not a real set in terms of data structures. std::unordered_set is a real set, but std::set is a binary search tree , more specifically a red-black tree. Why, then, is it called std::set? Is there some specific functionality that sets a std::set apart from a binary tree? Thanks.

Preorder printing Binary Tree with indentations

眉间皱痕 提交于 2019-12-08 12:41:46
问题 How would one go about pre-order printing a binary tree with an indentation (3 spaces) for each subsequent level. At this point, I'm recursively printing out the tree by using a helper method, but I'm not sure how to go about coding the indentation. This is what I have so far: public void print() { printPreorder(root); System.out.println(); } private void printPreorder(BinaryTreenode<E> node) { System.out.println(node.getData() + " "); if (node.getLeft() != null) { printPreorder(node.getRight

Removing leaves from Binary Tree not being represented properly

空扰寡人 提交于 2019-12-08 12:17:28
问题 I have been working on creating a Binary Tree from scratch, not using built-in libraries. I am working on a function called "pruneLeaves". The job is to remove all leaves of the tree; nodes with no children. When I step through the function with breakpoints, it appears to be removing the leaves and even prints out that it is indeed removing the proper nodes. However, when I display the tree in my main function afterwards, the nodes are still there! I have tried for hours to figure this out,

Java: binary tree recursion methods

旧街凉风 提交于 2019-12-08 11:13:26
问题 I'm quite new to java and one of our assignments requires me to create a binary tree containing nodes with int values. My professor wants us to use one class containing the main method. I applied two recursive methods, one to insert a node and one to display existing nodes. Whenever I run my code however, the console only displays the most recent node that I entered. Is there something wrong with the methods I used? This is what I have so far: import java.util.Scanner; public class node {

Traversing arbitrarily large binary tree inorder

假装没事ソ 提交于 2019-12-08 11:08:32
问题 I'm stuck at finding a solution. C#, .NET 4.0, VS2010 I can easily write a recursive one, but can't for the life of me figure out something that won't overflow the stack if the tree is arbitrarily large. This is a binary tree question, and i am trying to write a public IEnumerable<T> Values() method. Here is the full code in case you are interested: http://pastebin.com/xr2f3y7g Obviously, the version currently in there doesn't work. I probably should mention that I am a newbie in C#,

Rotating a binary search tree

有些话、适合烂在心里 提交于 2019-12-08 10:52:07
问题 I am implementing a binary search tree. I have created a method to rotate the tree if I find that the tree is out of balance. Somehow this method is not working correctly and my tree is emptied. Only the last two values I added to make the tree unbalanced are still left in the tree. Can you tell me what is wrong with my algorithm? I have not posted the entire class because it is very long and do not want to confuse anyone. public boolean balanceBST(){ if (root == null){ return false; } int