binary-tree

Is the runtime of BFS and DFS on a binary tree O(N)?

♀尐吖头ヾ 提交于 2019-12-04 10:27:16
问题 I realize that runtime of BFS and DFS on a generic graph is O(n+m), where n is number of nodes and m is number of edges, and this is because for each node its adjacency list must be considered. However, what is the runtime of BFS and DFS when it is executed on a binary tree? I believe it should be O(n) because the possible number of edges that can go out of a node is constant (i.e., 2). Please confirm if this is the correct understanding. If not, then please explain what is the correct time

Saving a Binary tree to a file [closed]

倖福魔咒の 提交于 2019-12-04 10:17:44
Closed. This question is off-topic. It is not currently accepting answers. Learn more . Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I have a non-balanced (not binary-search) binary tree Need to incode (and later decode) it to txt file. How can I do it in efficient way? I found this link which talks about similar (same) problem,but it is obvious for me Zegar Please look at this on LeetCode . I like this solution because it's relatively efficient and produces light output files. Assuming that you've got a tree like this: _30_ / \

Trying to implement path record for Haskell binary tree search

ⅰ亾dé卋堺 提交于 2019-12-04 10:04:48
I've been playing around with binary trees in Haskell, and I am attempting to implement a dfs variant that returns the path (composed of left and rights) from the root node to the node containing the value searched for. I think it would be best to return a Maybe Directions type. Here is what has been implemented so far. data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Show, Eq) data Direction = L | R deriving (Show) type Directions = [Direction] inTree :: (Eq a) => a -> Tree a -> [Direction] inTree val (Node x l r) | val == x = [] | l /= Empty = L:(inTree val l) | r /= Empty = R:

making binary search tree

谁都会走 提交于 2019-12-04 10:03:38
How do I make a BST when I have an array list of 100 elements like {3,2,6,7,...,99} ? I believe TreeSet is an implementation of a binary search tree. Since integers have a natural ordering you could simply loop through your array of integers and add them all to a TreeSet<Integer> . Note also, that there is a method Arrays.binarySearch that does a binary search in a sorted array. int[] someInts = {3,2,6,7, /*...,*/ 99}; // use a TreeSet TreeSet<Integer> ints = new TreeSet<Integer>(); for (int i : someInts) ints.add(i); System.out.println(ints.contains(2)); // true System.out.println(ints

Deletion procedure for a Binary Search Tree

江枫思渺然 提交于 2019-12-04 09:46:38
问题 Consider the deletion procedure on a BST, when the node to delete has two children. Let's say i always replace it with the node holding the minimum key in its right subtree. The question is: is this procedure commutative? That is, deleting x and then y has the same result than deleting first y and then x? I think the answer is no, but i can't find a counterexample, nor figure out any valid reasoning. EDIT: Maybe i've got to be clearer. Consider the transplant(node x, node y) procedure: it

Code with explanation for binary tree rotation (left OR right)

蹲街弑〆低调 提交于 2019-12-04 08:43:40
I have been trying to wrap my brain around how to write code for rotation of binary tree. I looked at http://en.wikipedia.org/wiki/Tree_rotation and enfuzzled.com I have been staring at this for 2 hours and have looked at it multiple times earlier. I still see problems in the wikipedia article and cannot understand the other one completely e.g. Both these lines mentioned in the wikipedia article cannot be true at once Let P be Q's left child. Set P to be the new root. Can anyone please help?Thanks According to your comments to the question you are looking for the guidance for the rotation

For a given binary tree find maximum binary search sub-tree

瘦欲@ 提交于 2019-12-04 08:07:40
问题 For a given binary tree, find the largest subtree which is also binary search tree? Example: Input: 10 / \ 50 150 / \ / \ 25 75 200 20 / \ / \ / \ / \ 15 35 65 30 120 135 155 250 Output: 50 / \ 25 75 / \ / 15 35 65 回答1: This answer previously contained an O(n log n) algorithm based on link/cut trees. Here is a simpler O(n) solution. The core is a procedure that accepts a node, the unique maximum BSST rooted at its left child, the unique maximum BSST rooted at its right child, and pointers to

how to determine a balanced or perfectly balanced Binary search tree ( just from the picture )

大兔子大兔子 提交于 2019-12-04 07:43:39
I am not sure how to determine if a tree is balanced, perfectly balanced, or neither if I have it as a picture not a code For example if I have this tree How can I check if it's balanced, perfectly balanced, or unbalanced? and can someone give me an example of a perfectly balanced tree? [o] / \ [b] [p] \ / \ [d] [m] [r] Clearly I can tell that the tree is unbalanced if it was something like this: [b] \ [d] \ [r] \ [c] However, if it was something very similar to the one above I don't know how to get it This is a perfectly balanced and balanced tree: [k] / \ [A] [p] / \ [N] [R] Can someone

OpenCL - copy Tree to device memory

断了今生、忘了曾经 提交于 2019-12-04 06:10:25
问题 I'm implemented a Binary-Search-Tree in C code. Each of my tree nodes looks like this: typedef struct treeNode { int key; struct treeNode *right; struct treeNode *left; } treeNode_t; The construction of the Tree made by the Host. The query of the tree made by the device. Now, let's assumed that I'm already finished building my Tree in host memory. I'm want to copy the root of my tree to the memory of my device. Copying the root of the tree it self isn't enough. Because the right \ left child

Rewrite a C code in Java to construct full binary tree

情到浓时终转凉″ 提交于 2019-12-04 05:10:37
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 from preorder and make it root, and increment preIndex struct node* root = newNode ( pre[*preIndex] );