Hot answers tagged

32 votes
Accepted

What is the difference between radix trees and Patricia tries?

I found this post very helpful. To see the difference between Patricia tries and radix trees, it is important to understand: The notion of radix, since Patricia tries are radix trees with radix ...
user avatar
28 votes
Accepted

Is there a difference between perfect, full and complete tree?

Yes, there is a difference between the three terms and the difference can be explained as: Full Binary Tree: A Binary Tree is full if every node has 0 or 2 children. Following are examples of a full ...
user avatar
  • 406
28 votes
Accepted

Can the pre-order traversal of two different trees be the same even though they are different?

Tree Examples (image): ...
user avatar
18 votes
Accepted

What are the applications of Rose trees?

You seem to have an overly "data structures and algorithms" mindset. Not every tree is some kind of search tree. Data structures are often designed to correspond to or capture aspects of a domain ...
user avatar
14 votes

BIT: What is the intuition behind a binary indexed tree and how was it thought about?

I think that the original paper by Fenwick is much clearer. The answer above by @templatetypedef requires some "very cool observations" about the indexing of a perfect binary tree, which are ...
user avatar
  • 339
14 votes
Accepted

When are binary trees better than hashtables in real world applications?

Hash tables can only tell you if an element is present or not. Here are somethings you can do with a binary tree that you can't do wiht a hash table. sorted traversal of the tree find the next ...
user avatar
  • 522
13 votes

Algorithm to find diameter of a tree using BFS/DFS. Why does it work?

The intuition behind is very easy to understand. Suppose I have to find longest path that exists between any two nodes in the given tree. After drawing some diagrams we can observe that the longest ...
user avatar
13 votes

What is the earliest use of "trees" in computer science?

Wikipedia says that the first use of tree in mathematics was by Cayley in 1857. Since the use in computer science is taken directly from mathematics, it seems more fundamental to ask when they ...
user avatar
13 votes
Accepted

Do Kruskal's and Prim's algorithms yield the same minimum spanning tree?

Found this which states that if all the conditions I mentioned above are met, a graph necessarily has a unique MST. Therefore, in terms of my question, Kruskal's and Prim's algorithms necessarily ...
user avatar
11 votes

When are binary trees better than hashtables in real world applications?

One application domain where binary trees are better, or more easily adjustable than certain alternatives, are persistent data structures (which are often used in (purely) functional programming). A ...
user avatar
11 votes
Accepted

What does pre-, post- and in-order walk mean for a n-ary tree?

No, it's not limited to binary trees. Yes, pre-order and post-order can be used for $n$-ary trees. You simply replace the steps "Traverse the left subtree.... Traverse the right subtree...." in the ...
user avatar
  • 143k
11 votes

What is the earliest use of "trees" in computer science?

According to Donald Knuth's TAOCP, Vol. 1, pg. 459 the following papers might be considered as one of the first appearances of trees in CS. H. G. Kahrimanian, Analytical Differentiation by a Digital ...
user avatar
  • 11.9k
10 votes

Longest path in an undirected tree with only one traversal

This can be solved in a better way. Also, we can reduce the time complexity to O(n) with a slight modification in the data structure and using an iterative approach. For a detailed analysis and ...
user avatar
  • 200
10 votes
Accepted

Binary rooted tree isomorphism

There is a classical linear time algorithm for rooted tree isomorphism due to Aho, Hopcroft and Ullman. The algorithm actually uses a simple isomorphism invariant. See for example lecture notes of ...
user avatar
10 votes
Accepted

What algorithm should I use to find a minimal tree that include certain nodes within a graph?

This is the famous Steiner tree problem in graphs, which is known as NP-hard.
user avatar
  • 7,275
10 votes
Accepted

Count total number of k length paths in a tree

This can be solved in $\mathcal{O}(n \log n)$ by using the smaller-to-larger merging technique. Root the tree at an arbitrary vertex. We will calculate for every subtree an array where the $d$th ...
user avatar
9 votes
Accepted

What is the difference between a R-tree and a BVH?

Note that we want to be able to retrieve, for any query range, the points that are inside, or sometimes the points that are closest to that query range. That's why a bounding-volume hierarchy is ...
user avatar
  • 1,914
9 votes
Accepted

Why is the running time for BFS $O(b^{d+1})$?

This represents a difference between the kinds of problems the CS algorithms community usually uses BFS to solve, vs the kinds of problems the CS artificial intelligence community usually uses BFS to ...
user avatar
  • 143k
9 votes

Why does the formula 2n + 1 find the child node in a binary heap?

I would like to propose my revisited version of Hiroki's answer. Currently it's been sitting in peer-review (https://cs.stackexchange.com/review/suggested-edits/66932) for a while, so it's not being ...
user avatar
  • 91
9 votes

Time Complexity to find height of a BST

Your algorithm runs in linear time on all inputs. The algorithm visits each node of the tree exactly once, and does $O(1)$ work per node. Therefore it runs in time $\Theta(n)$, where $n$ is the number ...
user avatar
9 votes

Can the pre-order traversal of two different trees be the same even though they are different?

Counting argument The number of unlabeled binary trees of $n$ nodes is the $n^\text{th}$ Catalan number $C_n=(2n)!/(n!(n+1)!).$ For example there are 5 binary trees of 3 nodes, ...
user avatar
  • 366
8 votes
Accepted

How can one search in O(log n) time in a red-black tree?

The search operation is the same for all binary search trees - recurse into the left or right branch depending on whether the element is smaller or larger than the current root. Red-black trees are ...
user avatar
  • 2,209
8 votes
Accepted

Is the height of the tree the number of edges or number of nodes?

As Yuval says, there's no standard definition. This is not because computer scientists are indecisive but because it's sometimes more convenient to use one definition and sometimes more convenient to ...
user avatar
8 votes
Accepted

What is the chance that this code terminates?

This is an example of a branching process. The behavior of a branching process depends on the expected number of children, which in your case is $1.25 > 1$. When this number is at most 1, the ...
user avatar
8 votes
Accepted

Time complexity of Depth First Search

The book is counting the number of times each line is executed throughout the entire execution of a call of DFS, rather than the number of times it is executed in each call of the subroutine DFS-VISIT....
user avatar
8 votes

Why is the running time for BFS $O(b^{d+1})$?

The bounds $O(|V|+|E|)$ and $O(b^d)$ are talking about different things. The former is appropriate when you know what $V$ and $E$ are in advance, and they're both finite. The latter is ...
user avatar
8 votes

Can the pre-order traversal of two different trees be the same even though they are different?

Lets assume you consider trees of $n$ nodes. Now take any binary tree with $n$ nodes and name the nodes according to their pre-order numbering. Then clearly the pre-order sequence of the tree will be $...
user avatar
  • 27.7k
7 votes

Algorithm to find diameter of a tree using BFS/DFS. Why does it work?

Update 3 and corrected answer There's an error in the linked solution set (see update 2 below), but it can be easily corrected with @Yuval Filmus's suggestion in the question's comment, which further ...
user avatar
  • 818
7 votes

Difference between spanning tree and a tree?

Given a graph $G$, a spanning tree is a subgraph of $G$ that (i) is a tree, and (ii) has all the vertices in $G$. There can be many spanning trees for $G$. If you put weights on the edges, one of ...
user avatar
  • 20.4k
7 votes

What is the earliest use of "trees" in computer science?

Isaiah: ""And there shall come forth a rod out of the stem of Jesse, and a Branch shall grow out of his roots" The tree as a data model for genealogical information is very ancient indeed.
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible