binary-tree

Tree iterator, can you optimize this any further?

本秂侑毒 提交于 2019-12-04 20:18:52
As a follow up to my original question about a small piece of this code I decided to ask a follow up to see if you can do better then what we came up with so far. The code below iterates over a binary tree (left/right = child/next ). I do believe there is room for one less conditional in here (the down boolean). The fastest answer wins! The cnt statement can be multiple statements so lets make sure this appears only once The child() and next() member functions are about 30x as slow as the hasChild() and hasNext() operations. Keep it iterative <-- dropped this requirement as the recursive

Removing duplicate subtrees from binary tree

可紊 提交于 2019-12-04 19:27:05
问题 I have to design an algorithm under the additional homework. This algorithm have to compress binary tree by transforming it into DAG by removing repetitive subtrees and redirecting all these connections to one left original subtree. For instance I've got a tree (I'm giving the nodes preorder): 1 2 1 3 2 1 3 The algorithm have to remove right connection (right subtree that means 2 1 3) of 1 (root) and redirect it to left connection (because these substrees are the same and left was first in

Generate All Possible Trees

社会主义新天地 提交于 2019-12-04 17:55:09
Given the following data type definition: data FormTree = Empty | Node FormTree FormTree deriving Show I want to write a function which generates an infinite list containing all possible trees sorted after length e.g. the amount of nodes. The following code almost does what I need but it only descends the tree on the right side by inserting additional nodes every time but I need it to alternate between both sides. allPossibleTrees :: [FormTree] allPossibleTrees = Empty : [Node x y | x <- recursive, y <- recursive] where recursive = allPossibleTrees Executing take 5 allPossibleTrees gives:

To print the boundary of Binary Tree

懵懂的女人 提交于 2019-12-04 17:51:44
问题 I have been asked in an interviews to print the boundary of the Binary Tree. For example. 1 / \ 2 3 / \ / \ 4 5 6 7 / \ \ 8 9 10 Answer will be : 1, 2, 4, 8, 9, 10, 7, 3 I have given the following answer. First Method : I have used a Bool variable to solve the above problem. void printLeftEdges(BinaryTree *p, bool print) { if (!p) return; if (print || (!p->left && !p->right)) cout << p->data << " "; printLeftEdges(p->left, print); printLeftEdges(p->right, false); } void printRightEdges

Sort BST in O(n) using constant memory

笑着哭i 提交于 2019-12-04 16:51:12
问题 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 回答1: 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 ...

Is Red-Black tree balanced

橙三吉。 提交于 2019-12-04 16:14:01
I am studying red-black trees and I am reading the Cormen's "Introduction to Algorithms" book. Now I am trying to create red-black tree with numbers 1-10 by using the pseudo-code described in the book - RB-INSERT-FIXUP(T, z). Here is the screenshot Everything was fine until I inserted number "6" into the tree. According to pseudo-code I get the following result As you can see all red-black tree requirements met, but I am confused because I know that red-black tree should be balanced on each step. I can manually perform "left-rotate" procedure with "2" and "4" and change the colours. In that

Reconstructing binary tree from inorder and preorder traversals

江枫思渺然 提交于 2019-12-04 14:36:28
I have written the following code for constructing a tree from its inorder and preorder traversals. It looks correct to me but the final tree it results in does not have the same inorder output as the one it was built from. Can anyone help me find the flaw in this function? public btree makeTree(int[] preorder, int[] inorder, int left,int right) { if(left > right) return null; if(preIndex >= preorder.length) return null; btree tree = new btree(preorder[preIndex]); preIndex++; int i=0; for(i=left; i<= right;i++) { if(inorder[i]==tree.value) break; } tree.left = makeTree(preorder, inorder,left,

Binary-Tree in Template

∥☆過路亽.° 提交于 2019-12-04 14:31:44
问题 so i want to make a code, that creates a binary tree, that holds data, for example ints like 1,6,2,10,8 and on pop i get the biggest number, and after that it gets deleted from the tree, and on push i can insert a new element. And this should be in a template so i can easy change the data type i want to hold in the tree. Now i got the tree so far, without template it is working fine thought, i can add items, and i can print them, but when i try to put it in a template, i get the following

More localized, efficient Lowest Common Ancestor algorithm given multiple binary trees?

若如初见. 提交于 2019-12-04 14:14:10
问题 I have multiple binary trees stored as an array. In each slot is either nil (or null; pick your language) or a fixed tuple storing two numbers: the indices of the two "children". No node will have only one child -- it's either none or two. Think of each slot as a binary node that only stores pointers to its children, and no inherent value. Take this system of binary trees: 0 1 / \ / \ 2 3 4 5 / \ / \ 6 7 8 9 / \ 10 11 The associated array would be: 0 1 2 3 4 5 6 7 8 9 10 11 [ [2,3] , [4,5] ,

Detect cycles in a genealogy graph during a Depth-first search

纵然是瞬间 提交于 2019-12-04 12:17:34
问题 I'm loading horse genealogical data recursively. For some wrong sets of data my recursion never stops... and that is because there are cycles in the data. How can I detect those cycles to stop recurring? I thought of while recurring maintain a hashTable with all "visited" horses. But that will find some false positives, because a horse CAN be twice in a tree. What cannot happen is that a horse appear as a father or grandfather or great grandfather of ITSELF. 回答1: Pseudo code: void ProcessTree