binary-tree

how to build a binary tree from preorder and inorder traversals

夙愿已清 提交于 2019-12-03 07:46:15
问题 Im doing an assignment on building a binary tree from the preorder and inorder traversals (a char in each Node) and im trying to wrap my brain around how to build the actual tree. Here is my thought process on how to accomplish this: store the first entry in the preorder as the root node search the inorder for that entry. take the chars to the left of the root node and save them as a char array. take the chars to the right of the root node and save them as a char array. make a new tree, with

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

浪子不回头ぞ 提交于 2019-12-03 07:44:33
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. Pseudo code: void ProcessTree(GenTreeNode currentNode, Stack<GenTreeNode> seen) { if(seen.Contains(currentNode)) return; // Or, do

Prove that binary trees with the same inorder and preorder traversals are identical?

落爺英雄遲暮 提交于 2019-12-03 07:30:44
问题 Does anybody know how to prove that if two binary trees have the same inorder and preorder traversals, then they are identical? (perhaps by showing that you can't have two different binary trees with identical inorder and preorder traversals) Alternatively, show a case that would disprove this, or show why can't it be done? (I'll admit, this is purely academic but it's not homework or anything. My instincts tell me that it's true, but I don't think I ever did any proofs on graphs.) 回答1: The

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

无人久伴 提交于 2019-12-03 06:02:13
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 complexity of BFS and DFS on a binary tree? The time complexities for BFS and DFS are just O(|E|) , or

Real world pre/post-order tree traversal examples

不想你离开。 提交于 2019-12-03 05:59:23
问题 I understand pre-order, in-order, and post-order tree traversal algorithms just fine. (Reference). I understand a few uses: in-order for traversing binary search trees in order, pre-order for cloning a tree. But I can't for the life of me come up with a real world task that I'd need post-order traversal to accomplish. Can you give me an example? And: can you give me any better uses for pre-order traversal? Edit: Can anyone give me an example other than expression trees and RPN? Is that really

Using arrow -> and dot . operators together in C

二次信任 提交于 2019-12-03 05:08:34
问题 I was under the impression that it was possible to access data from a sub-node of a linked list or similar structure by using the arrow and dot operators together like so: typedef struct a{ int num; struct a *left; struct a *right; }tree; tree *sample; ... if(sample->left.num > sample->right.num) //do something but when I try to implement this, using -> and . to access data from a sub node I get the error "request for member num in something not a structure or union". 回答1: Use -> for pointers

What is an “internal node” in a binary search tree?

三世轮回 提交于 2019-12-03 04:43:34
问题 I'm scouring the internet for a definition of the term "Internal Node." I cannot find a succinct definition. Every source I'm looking at uses the term without defining it, and the usage doesn't yield a proper definition of what an internal node actually is. Here are the two places I've been mainly looking: http://planetmath.org/encyclopedia/ExternalNode.html assumes that internal nodes are nodes that have two subtrees that aren't null, but doesn't say what nodes in the original tree are

How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space

a 夏天 提交于 2019-12-03 04:29:07
问题 How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space. 回答1: You don't give much to go on, but if the requirement is what I think it is, you have a binary tree already created and sitting in memory, but not sorted (the way you want it to be sorted, anyway). I'm assuming that the tree nodes look like struct tree_node { struct tree_node * left; struct tree_node * right; data_t data; }; I'm also assuming that you can read C While we could just sit around

What data structure using O(n) storage with O(log n) query time should I use for Range Minimum Queries?

让人想犯罪 __ 提交于 2019-12-03 04:16:26
问题 I'm stumped by the following homework question for an algorithms class: Suppose that we are given a sequence of n values x 1 , x 2 ... x n , and seek to quickly answer repeated queries of the form: given i and j, find the smallest value in x i ... x j Design a data structure that uses O(n) space and answers queries in O(log n) time. Firstly, I'm uncertain whether a sequence refers to a sorted set, or an unsorted set - but since it doesn't say otherwise I'll assume sequence means unsorted. So,

how to rebuild BST using {pre,in,post}order traversals results

不想你离开。 提交于 2019-12-03 03:54:43
We know the pre-order, in-order and post-order traversals. What algorithm will reconstruct the BST? 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 < len; i++) if(*(inOrder + i) == *preOrder) break; tree->left = createBT(preOrder + 1, inOrder, i); tree->right