binary-tree

zipWith for trees in Haskell

扶醉桌前 提交于 2019-12-19 04:14:36
问题 I am learning Haskell using The Haskell School of Expression: Learning Functional Programming through Multimedia and I am unsure how to go about solving this exercise. Using the definition of trees given by data Tree a = Node (Tree a) (Tree a) | Leaf a Define tree versions of the list functions zip and zipWith . There will be cases at the leaves or where trees are of different shapes where you’ll have to make design decisions. Try to make your decisions as elegant as possible. For zip I have

Can we use binary search tree to simulate heap operation?

二次信任 提交于 2019-12-18 21:54:43
问题 I was wondering if we can use a binary search tree to simulate heap operations (insert, find minimum, delete minimum), i.e., use a BST for doing the same job? Are there any kind of benefits for doing so? 回答1: Sure we can. but with a balanced BST. The minimum is the leftest element. The maximum is the rightest element. finding those elements is O(logn) each, and can be cached on each insert/delete, after the data structure was modified [note there is room for optimizations here, but this naive

Can we use binary search tree to simulate heap operation?

痴心易碎 提交于 2019-12-18 21:54:42
问题 I was wondering if we can use a binary search tree to simulate heap operations (insert, find minimum, delete minimum), i.e., use a BST for doing the same job? Are there any kind of benefits for doing so? 回答1: Sure we can. but with a balanced BST. The minimum is the leftest element. The maximum is the rightest element. finding those elements is O(logn) each, and can be cached on each insert/delete, after the data structure was modified [note there is room for optimizations here, but this naive

Strategy to find duplicate entries in a binary search tree

孤街醉人 提交于 2019-12-18 15:54:42
问题 I have a BST which has duplicate entries. I am trying to find duplicate entries. Now obviously I can write a dumb algorithm which traverses the whole tree, which is easy. However, I want to write a more efficient one. Here's what I've done/thought so far: Assume the following tree. 10 / \ 5 15 /\ / \ 2 8 10 16 \ \ 8 12 If I want to find all 8's, I will first find the 8 on the left subtree of the 10. To find a duplicate, if it has no right child, is it going to be the left-most node on the

Max-Heapify A Binary Tree

瘦欲@ 提交于 2019-12-18 13:06:30
问题 This is one of the interview questions I recently came across. Given the root address of a complete or almost complete binary tree, we have to write a function to convert the tree to a max-heap. There are no arrays involved here. The tree is already constructed. For e.g., 1 / \ 2 5 / \ / \ 3 4 6 7 can have any of the possible max heaps as the output-- 7 / \ 3 6 / \ / \ 2 1 4 5 or 7 / \ 4 6 / \ / \ 2 3 1 5 etc... I wrote a solution but using a combination of pre and post order traversals but

Looking for fast algorithm to find distance between two nodes in binary tree

﹥>﹥吖頭↗ 提交于 2019-12-18 12:54:28
问题 How do I find the distance between two nodes in a binary tree? Equivalently, what algorithms are there for finding the most recent common ancestor (lowest common ancestor) of two nodes? 回答1: calculate the list of ancestors for each node find the common prefix the last element from the common prefix is the lowest common ancestor remove the common prefix from both ancestor lists the distance is the sum of lengths of the remaining lists +1 回答2: Finding the common ancestor is almost certainly the

Finding max depth of binary tree without recursion

浪子不回头ぞ 提交于 2019-12-18 10:55:26
问题 Recursive mechanism to find max depth of depth of binary tree is very straightforward, but how can we do it efficiently without recursion as I have large tree where I would rather avoid this recursion. //Recursive mechanism which I want to replace with non-recursive private static int maxDepth(Node node) { if (node == null) return 0; return 1 + Math.max(maxDepth(node.left), maxDepth(node.right)); } PS: I am looking for answers in Java. 回答1: This variant uses two stacks, one for additional

Diameter of Binary Tree - Better Design

半腔热情 提交于 2019-12-18 10:30:10
问题 I have written a code for finding diameter of Binary Tree. Need suggestions for the following: Can I do this without using static variable at class level? Is the algorithm fine/any suggestions? public class DiameterOfTree { public static int diameter = 0; public static int getDiameter(BinaryTreeNode root) { if (root != null) { int leftCount = getDiameter(root.getLeft()); int rightCount = getDiameter(root.getRight()); if (leftCount + rightCount > diameter) { diameter = leftCount + rightCount;

Is there a built-in Binary Search Tree in .NET 4.0?

假装没事ソ 提交于 2019-12-18 10:25:09
问题 Is there a built-in binary search tree in .NET 4.0, or do I need to build this abstract data type from scratch? Edit This is about the binary search tree specifically, and not abstract data type "trees" in general. 回答1: I think the SortedSet<T> class in System.Collections.Generic is what you're looking for. From this CodeProject article: It is implemented using a self-balancing red-black tree that gives a performance complexity of O(log n) for insert, delete, and lookup. It is used to keep

How does inorder+preorder construct unique binary tree?

谁说我不能喝 提交于 2019-12-18 10:24:37
问题 Recently, my questions were marked duplicate, like this , even if they weren't. So, let me start with following and then I'll explain my question. Why this question is not a duplicate? I'm not asking how to create a binary tree when inorder & preorder traversal are given. I'm asking for the proof, that inorder+preorder traversal define a unique binary tree. Now, to original question . I went to an interview and interviewer asked me this question. I was stuck and couldn't proceed. :| Question: