binary-tree

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

安稳与你 提交于 2019-12-02 22:20:11
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.) Nick Dandoulakis The basic idea is how to reconstruct a binary tree by the given inorder and preorder traversals.

how to build a binary tree from preorder and inorder traversals

末鹿安然 提交于 2019-12-02 21:16:09
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 the root as the parent and its 2 children being the left and right char arrays. keep going recursively

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

烂漫一生 提交于 2019-12-02 20:53: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 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 the left-most and right-most elements of these BSSTs. It destroys its inputs (avoidable with persistent data

Using arrow -> and dot . operators together in C

偶尔善良 提交于 2019-12-02 19:30:40
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". Use -> for pointers; use . for objects. In your specific case you want if (sample->left->num > sample->right->num) because all

Skip Lists — ever used them?

北城余情 提交于 2019-12-02 19:10:13
I'm wondering whether anyone here has ever used a skip list . It looks to have roughly the same advantages as a balanced binary tree but is simpler to implement. If you have, did you write your own, or use a pre-written library (and if so, what was its name)? Todd Years ago I implemented my own for a probabilistic algorithms class. I'm not aware of any library implementations, but it's been a long time. It is pretty simple to implement. As I recall they had some really nice properties for large data sets and avoided some of the problems of rebalancing. I think the implementation is also

balancing an AVL tree (C++)

穿精又带淫゛_ 提交于 2019-12-02 19:09:05
I'm having the hardest time trying to figure out how to balance an AVL tree for my class. I've got it inserting with this: Node* Tree::insert(int d) { cout << "base insert\t" << d << endl; if (head == NULL) return (head = new Node(d)); else return insert(head, d); } Node* Tree::insert(Node*& current, int d) { cout << "insert\t" << d << endl; if (current == NULL) current = new Node(d); else if (d < current->data) { insert(current->lchild, d); if (height(current->lchild) - height(current->rchild)) { if (d < current->lchild->getData()) rotateLeftOnce(current); else rotateLeftTwice(current); } }

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

假如想象 提交于 2019-12-02 19:02:13
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 internal vs. external. http://www.math.bas.bg/~nkirov/2008/NETB201/slides/ch06/ch06-2.html seems to

Real world pre/post-order tree traversal examples

天大地大妈咪最大 提交于 2019-12-02 18:29:49
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 all post-order is good for? Topological sorting is a post-order traversal of trees (or directed

binary search vs binary search tree

≯℡__Kan透↙ 提交于 2019-12-02 18:04:23
What is the benefit of a binary search tree over a sorted array with binary search? Just with mathematical analysis I do not see a difference, so I assume there must be a difference in the low-level implementation overhead. Analysis of average case run time is shown below. Sorted array with binary search search: O(log(n)) insertion: O(log(n)) (we run binary search to find where to insert the element) deletion: O(log(n)) (we run binary search to find the element to delete) Binary search tree search: O(log(n)) insertion: O(log(n)) deletion: O(log(n)) Binary search trees have a worst case of O(n)

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

南楼画角 提交于 2019-12-02 17:42:50
How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space. 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 wondering why this tree was ever created without having been created in sorted order that doesn't do us any