binary-tree

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 <

IntervalTree DeleteNode Java Implementation

非 Y 不嫁゛ 提交于 2019-12-03 14:00:05
问题 I need an IntervalTree or RangeTree implementation in Java, and am having trouble finding one with working deletion support. There's a built-in one at sun.jvm.hotspot.utilities.IntervalTree, but the deleteNode method in the RBTree superclass states: /** * FIXME: this does not work properly yet for augmented red-black * trees since it doesn't update nodes. Need to figure out exactly * from which points we need to propagate updates upwards. */ Trying to delete nodes from a tree ends up throwing

Algorithm to Render a Horizontal Binary-ish Tree in Text/ASCII form

泪湿孤枕 提交于 2019-12-03 13:59:33
It's a pretty normal binary tree, except for the fact that one of the nodes may be empty. I'd like to find a way to output it in a horizontal way (that is, the root node is on the left and expands to the right). I've had some experience expanding trees vertically (root node at the top, expanding downwards), but I'm not sure where to start, in this case. Preferably, it would follow these couple of rules: If a node has only one child, it can be skipped as redundant (an "end node", with no children, is always displayed) All nodes of the same depth must be aligned vertically; all nodes must be to

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

Why use heap instead of binary tree when implementing priority queue?

柔情痞子 提交于 2019-12-03 13:35:57
It seems to me that the only advantage of heap over binary tree is to find the smallest item in the heap in complexity of O(1) instead of O(log(2)n) in binary tree. When implementing priority queue you need to delete the smallest item each from the data structre. deleting the smallest item from a tree and both heap done in complexity of O(log(2)n). Althogh deleting item from a tree may be more complex. Deleting item with no childrens acctually very simple. My question is why use heap instead of binary tree(which is simpler in this case) when implementing priority queue? Worst case complexity

Sort BST in O(n) using constant memory

…衆ロ難τιáo~ 提交于 2019-12-03 11:50:54
This is not a homework. Just an interesting task :) Given a complete binary search three represensted by array. Sort the array in O(n) using constant memory. Example: Tree: 8 / \ 4 12 /\ / \ 2 6 10 14 /\ /\ /\ /\ 1 3 5 7 9 11 13 15 Array: 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 It is possible, people calling it homework probably haven't tried solving it yet. We use the following as a sub-routine: Given an array a1 a2 ... an b1 b2 .. bn, convert in O(n) time and O(1) space to b1 a1 b2 a2 ... bn an A solution for that can be

Looking for a java library that has implemented Binary Tree [closed]

让人想犯罪 __ 提交于 2019-12-03 11:49:00
Is there a java library that has Binary Tree that I can use? I am not looking forward to test and implement my own. The Java standard API only contains libraries that are universally useful and non-trivial to implement. A basic tree is trivial to implement: class BinaryTree { BinaryTree left; BinaryTree right; Object value; } Non-trivial trees are not universally useful: either they are needed as a part of the application data model, which is better modeled using domain specific classes (component has-a list of sub-components), or they are used as a part of a specific algorithm. Algorithms

What is the degree of a tree? (As in, a tree ADT)

浪子不回头ぞ 提交于 2019-12-03 11:46:35
I understand that the degree of a node is the number of children it has. However, how do we define the degree of a tree? Basically The degree of the tree is the total number of it's children i-e the total number nodes that originate from it.The leaf of the tree doesnot have any child so its degree is zero. The degree of a node is the number of partitions in the subtree which has that node as the root. Nodes with degree=0 are called leaves. In general a graph has a minimum degree and a maximum degree, that is just the minimum respectivly the maximum degree of all nodes in the graph. If a graph

Nicely printing/showing a binary tree in Haskell

限于喜欢 提交于 2019-12-03 11:31:17
问题 I have a tree data type: data Tree a b = Branch b (Tree a b) (Tree a b) | Leaf a ...and I need to make it an instance of Show , without using deriving . I have found that nicely displaying a little branch with two leaves is easy: instance (Show a, Show b) => Show (Tree a b) where show (Leaf x) = show x show (Branch val l r) = " " ++ show val ++ "\n" ++ show l ++ " " ++ show r But how can I extend a nice structure to a tree of arbitrary size? It seems like determining the spacing would require

How is Backtracking done using recusrion in Binary Tree

懵懂的女人 提交于 2019-12-03 08:41:14
I'm trying to insert a Binary Node. My code is convoluted and there's no hope of rescuing it so I plan to rewrite it (basically I didn't account for backtracking and didn't think about the algorithm all that closely). I'm trying to insert a Binary Node using in order traversal, but I don't understand how I'm supposed to backtrack. D / \ B E / \ / \ A C F How would I search through the left subtree of root D and then go back and search through the right one? It might be a stupid question, but I'm stumped. The best I can come up with is something like this: if (!root.hasLeftChild) { root = root