building binary tree out of a given traversal [closed] - algorithm

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I was solving my homework for the Data Structures and Algorithms course that I came across this question :
"Given two methods of traversing which can be pre-order and post-order , pre-order and in-order , post-order and in-order , how many binary trees can we extract ?"
Now I know that you certainly cannot find the binary tree from only one traversal order , but which of those two traversals would give you only one binary tree ? and how ? and the ones that do not exemplify one binary tree , how many binary trees do they exemplify and how can we count that number ?

I believe this source is useful. A pre and a post order traversal of a tree is not enough to uniquely reconstruct it without further restrictions. However an algorithm is shown how to reconstruct a tree from its post order and inorder travesal and as the last case is somewhat symmetric I believe this is algorithmic prove that a tree can be reconstructed uniquely from its inorder and any of the other traversals.

Related

Tree data structure [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Needs details or clarity Add details and clarify the problem by editing this post.
Improve this question
A b tree is a generalized binary tree . How ?
A binary tree is a tree in which each node has at most 2 children. A b-tree of order m is a tree in which
Each node has at most m children.
Each internal node (not the root or a leaf) has at least ⌈m/2⌉ children. (⌈x⌉ means the ceiling of x, the least integer not less than x.)
Each non-leaf node (parent and all internal nodes) has at least 2 children.
All leaves appear on the same level.
(B tree nodes also have keys, but this is not directly part of the tree structure and does not concern us in this question.)
So some b-trees are binary trees. Every b-tree of order 2 is a binary tree. Some b-trees of higher order are binary trees if they happen not to have any nodes with more than 2 children.
b-trees of orders 5 and greater could be binary trees only if they are just a parent and two children, which are leaves. If a tree of order 5 or greater had any internal nodes, that node would be required to have at least ⌈5/2⌉ = 3 children, so it could not be a binary tree. b-trees of orders 3 and 4 could have internal nodes and still be binary trees.
The concepts of binary tree and b-tree overlap, but neither is a subset of the other in the sense that all requirements of one would satisfy the other. For the most part in programming, you are not going to mix uses of routines for binary trees and other routines for b-trees based on just how the current tree happens to be filled and arranged; on a particular set of data being managed, you would be working entirely with binary tree routines or entirely with b-tree routines.

Best way to implement shortest path algorithm with directed graph [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Lets assume i have a weighted undirected graph with edges and wanted to find the shortest path as well as all possible paths that i could follow from the startpoint to the endpoint with distances, what would be the best way to implement this? Breadth depth search and k paths algorithm seem to offer reasonable solutions, although im not sure which is best
Sorry, can't post this as comments...
If you need all possible paths, you can't do really better than "tree" traversal (BFS or DFS for instance). Note that you'll need to consider each node as many times as it can be reached from the start (the "tree" is much bigger than the original graph - even infinite if you have cycles in your graph, but let's assume you don't).
To get the smallest path, you could look for it in your list in the end; or preferably, you could use a Dijkstra-like order for your tree traversal, so the shortest path will be the first to come up.

Maximum number of red nodes in red black tree [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
In a red-black tree with n nodes, what is the maximum number of red nodes (assuming the root is black)?
Is it O(n)?
If the tree has n nodes and the root is black, there are n - 1 nodes left and n - 1 = O(n), so you're right.
If you want to count/bound number of red nodes in the tree more accurately, you have to know the topology of that tree.
For instance, if the tree is a complete binary tree, according to the definition of the red-black-tree, it can have no red nodes at all.

Can we have a red-black tree without any red nodes? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I wonder whether a red black tree should have at least one red node. Also, given a BST, if we can convert it into an RBT, is there a unique way to turn this tree into a red-black tree?
A quick glance at the properties of a red-black tree shows that there is no requirement for any node to be red. The only way red nodes come about is through property 5:
Every simple path from a given node to any of its descendant leaves contains the same number of black nodes.
This property is also satisfied by any perfect binary tree, so every perfect binary search tree with only black nodes is also a red-black tree. (I'm not sure if the textbook red-black tree algorithms ever produce these, though.)
Also, given a BST, if we can convert it into an RBT, is there a unique way to turn this tree into a red-black tree?
There is no single unique RBT for an arbitrary BST; there are always multiple equivalent RBTs, except for very shallow trees.

Time complexity of depth-first graph algorithm [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
I am starting to learn time complexity, and I looked in the examples for the time complexity for some simple sort.
I wanted to know how do we calculate average time complexity for a depth-first search in a graph with |V|=n and |E|=m,let the start node be 'u' and end node be 'v'.
The time complexity for DFS is O(n + m). We get this complexity considering the fact that we are visiting each node only once and in the case of a tree (no cycles) we are crossing all the edges once.
For example, if the start node is u, and the end node is v, we are thinking at the worst-case scenario when v will be the last visited node.
So we are starting to visit each the first neighbor's of u connected component, then the second neighbor's connected component, and so on until the last neighbor's connected component, where we find v. We have visited each node only once, and didn't crossed the same edge more than once.
it will be O(n+m) if the graph is given in the form of adjacency list but
if the graph is in the form of adjacency matrix then the complexity is O(n*n),
as we have to traverse through the whole row until we find an edge.

Resources