binary-tree

What would be the time complexity of counting the number of all structurally different binary trees?

十年热恋 提交于 2019-12-22 08:08:31
问题 Using the method presented here: http://cslibrary.stanford.edu/110/BinaryTrees.html#java 12. countTrees() Solution (Java) /** For the key values 1...numKeys, how many structurally unique binary search trees are possible that store those keys? Strategy: consider that each value could be the root. Recursively find the size of the left and right subtrees. */ public static int countTrees(int numKeys) { if (numKeys <=1) { return(1); } else { // there will be one value at the root, with whatever

Convert Preorder listing of a binary tree to postorder and vice versa

心不动则不痛 提交于 2019-12-22 07:58:59
问题 How can I find the preorder listing of a tree if only the postorder listing is given and vice versa. Also, in the tree, every non-leaf node has two children (i.e. Each node has either two or zero children.) EDIT: Another given assumption is that the label of each node is unique and has a field that will identify it as an internal node or a leaf. I think that ought to get rid of the ambiguity of a single preorder or postorder being able to uniquely identify a tree. 回答1: Without assuming that

How can I calculate the level of a node in a perfect binary tree from its depth-first order index?

这一生的挚爱 提交于 2019-12-22 06:02:39
问题 I have a perfect binary tree, i.e. each node in the tree is either a leaf node, or has two children, and all leaf nodes are on the same level. Each node has an index in depth-first order. (E.g. in a tree with 3 levels the root node has index 0, the first child has 1, the first child of the first child has 2, the second child of the first child has 3, the second child has 4, the first child of the second child has 5, the second child of the second child has index 6. 0 / \ 1 4 / \ / \ 2 3 5 6 )

Print a tree in sorted order using heap properties (Cormen)

只谈情不闲聊 提交于 2019-12-22 04:58:16
问题 I am refreshing on algorithm theory (from Cormen). There is an exercise in the chapter for binary tries that asks: Can the min-heap property be used to print out the keys of an n-node tree in sorted order in O(n) time? Show how, or explain why not. I thought yes it is possible. In the min heap the element in a node is smaller than both its children. So the root of the heap is always the smaller element of all the n elements and the left child of the root is the smaller than all the elements

Is there an existing solution for these particular multithreaded data structure requirements?

本秂侑毒 提交于 2019-12-22 03:59:07
问题 I've had the need for a multi-threaded data structure that supports these claims: Allows multiple concurrent readers and writers Is sorted Is easy to reason about Fulfilling multiple readers and one writer is a lot easier, but I really would wan't to allow multiple writers. I've been doing research into this area, and I'm aware of ConcurrentSkipList (by Lea based on work by Fraser and Harris) as it's implemented in Java SE 6. I've also implemented my own version of a concurrent Skip List

draw binary tree with php

非 Y 不嫁゛ 提交于 2019-12-21 18:05:22
问题 I'm looking for a good library / API to draw a binary tree using PHP. I've tried using Image_GraphViz, but it doesn't seem to work. I've also looked at phpsyntaxtree, but its not documented. Alternatively, I'm also looking for a jQuery plugin that does this. (just not this one because it has no documentation). Thanks 回答1: My suggestion is to go with js, because vector graphs are nicer and you can add interactivity anytime. Try raphael, a good (and perfectly documented) js graph library

Rewrite a C code in Java to construct full binary tree

对着背影说爱祢 提交于 2019-12-21 10:49:39
问题 I want to write a function to construct a full binary tree from a given preorder and postorder array. I found that link http://www.geeksforgeeks.org/full-and-complete-binary-tree-from-given-preorder-and-postorder-traversals/ which proposes the following C code : struct node* constructTreeUtil (int pre[], int post[], int* preIndex, int l, int h, int size) { // Base case if (*preIndex >= size || l > h) return NULL; // The first node in preorder traversal is root. So take the node at // preIndex

How to finding first common ancestor of a node in a binary tree?

匆匆过客 提交于 2019-12-21 10:16:17
问题 Following is my algorithm to find first common ancestor. But I don’t know how to calculate it time complexity, can anyone help? public Tree commonAncestor(Tree root, Tree p, Tree q) { if (covers(root.left, p) && covers(root.left, q)) return commonAncestor(root.left, p, q); if (covers(root.right, p) && covers(root.right, q)) return commonAncestor(root.right, p, q); return root; } private boolean covers(Tree root, Tree p) { /* is p a child of root? */ if (root == null) return false; if (root ==

How to construct a binary tree from just the level order traversal string

拜拜、爱过 提交于 2019-12-21 05:18:20
问题 Consider a binary tree with the following properties: An internal node (non-leaf node) has a value 1 if it has two children. A leaf node has a value 0 since it has no children. A level order traversal on the tree would generate a string of 1s and 0s (by printing the weird value at each node as they are visited). Now given this string construct the binary tree and perform a post order traversal on the tree. The post order string should be the output of the program. For example: Input String is

Binary search tree traversal that compares two pointers for equality

一曲冷凌霜 提交于 2019-12-21 05:07:19
问题 I'm reading the Cormen algorithms book (binary search tree chapter) and it says that there are two ways to traverse the tree without recursion: using stack and a more complicated but elegant solution that uses no stack but assumes that two pointers can be tested for equality I've implemented the first option (using stack), but don't know how to implement the latter. This is not a homework, just reading to educate myself. Any clues as to how to implement the second one in C#? 回答1: Sure thing.