Questions tagged [tree]

A tree is a widely-used data structure that emulates a hierarchical tree-like structure with a set of linked nodes.

A tree is a data structure where each node has a number of child nodes. By starting at the first node (called the root), one can use a decision algorithm to determine which of the children of the current node is the most appropriate for the next processing or traversal step.

Operations on trees include insert, delete, traverse and find. The algorithms for these methods are usually recursive, starting from the root node and recursively calling its left and right child.

Below is an example of binary-tree, in which each node has up to two child nodes:

binary tree

Because large portions of the remaining nodes are culled away with each decision, properly balanced trees typical yield logarithmic time lookups, inserts, and deletes when used for storage.


Useful links


Related tags

16659 questions
45
votes
2 answers

d3js Tree square

I would like to use d3.js to build a pedigree tree. All exemples that I found are showcasing organic trees. Are there any exemples out there with a pedigree like style ?
gpasse
  • 4,380
  • 7
  • 44
  • 75
44
votes
4 answers

Maximum json size for response to the browser

I am creating tree with some custom control prepared with JavaScript/jquery. For creating the tree we are supplying json object as the input to java-script to iterate through and create the tree. Since the volume of data may go up-to 25K nodes.…
ankur
  • 4,565
  • 14
  • 64
  • 100
44
votes
4 answers

Prettyprint to a file?

I'm using this gist's tree, and now I'm trying to figure out how to prettyprint to a file. Any tips?
James.Wyst
  • 859
  • 3
  • 8
  • 13
43
votes
4 answers

Is Pre-Order traversal on a binary tree same as Depth First Search?

It seems to me like Pre-order traversal and DFS are same as in both the cases we traverse till the leaf node in a depth wise fashion. Could anyone please correct me if I am wrong? Thanks in advance!
43
votes
4 answers

D3 Tree Layout Separation Between Nodes using NodeSize

Right now I am trying to separate my rectangle nodes because they overlap as shown in the picture below: I took a look and found out that D3 offers a nodeSize and separation method but for some reason it did not work. I found this blog post talking…
aug
  • 11,138
  • 9
  • 72
  • 93
42
votes
7 answers

CMD tree command limit number of levels

How do I limit the number of levels that the tree command goes through in Windows? I need to output the results to a text file for work, but because by default the tree command lists every single directory under the one you ran the command in, the…
iqover9000
  • 573
  • 1
  • 5
  • 7
42
votes
4 answers

Proof of correctness: Algorithm for diameter of a tree in graph theory

In order to find the diameter of a tree I can take any node from the tree, perform BFS to find a node which is farthest away from it and then perform BFS on that node. The greatest distance from the second BFS will yield the diameter. I am not sure…
AYR
  • 1,139
  • 3
  • 14
  • 24
40
votes
7 answers

How to easily remember Red-Black Tree insert and delete?

It is quite easy to fully understand standard Binary Search Tree and its operations. Because of that understanding, I even don't need to remember the implementations of those insert, delete, search operations. I am learning Red-Black Tree now and I…
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
40
votes
5 answers

Parsing an arithmetic expression and building a tree from it in Java

I needed some help with creating custom trees given an arithmetic expression. Say, for example, you input this arithmetic expression: (5+2)*7 The result tree should look like: * / \ + 7 / \ 5 2 I have some custom classes to represent…
ChocolateBear
  • 415
  • 1
  • 4
  • 7
39
votes
6 answers

Reverse A Binary Tree (Left to Right)

I was looking at interview questions and I recently came upon one that asked you how to reverse a general binary tree, like flip it from right to left. So for example if we had the binary tree 6 / \ 3 4 / \ / \ 7 3 8 …
user1234831
  • 409
  • 1
  • 4
  • 5
39
votes
3 answers

In GitHub URL's: what is the difference between a tree and a blob?

I keep noticing this whenever I'm trying to add links to github files. You can add a link using blob: https://github.com/facebook/pop/blob/master/Podfile And the same doc comes up if you change blob to…
gran_profaci
  • 8,087
  • 15
  • 66
  • 99
39
votes
8 answers

Tree implementation in Java (root, parents and children)

I need to create a tree structure similar as the attached image in Java. I've found some questions related to this one but I haven't found a convincing and well explained response. The application business consists in food super categories (main…
Carlos
  • 1,319
  • 2
  • 17
  • 20
38
votes
4 answers

What is the total number of nodes in a full k-ary tree, in terms of the number of leaves?

I am doing a unique form of Huffman encoding, and am constructing a k-ary (in this particular case, 3-ary) tree that is full (every node will have 0 or k children), and I know how many leaves it will have before I construct it. How do I calculate…
Andrew
  • 2,425
  • 2
  • 24
  • 35
38
votes
5 answers

How can you get the call tree with Python profilers?

I used to use a nice Apple profiler that is built into the System Monitor application. As long as your C++ code was compiled with debug information, you could sample your running application and it would print out an indented tree telling you what…
user
  • 7,123
  • 7
  • 48
  • 90
38
votes
5 answers

Why is there no mutable TreeMap in Scala?

Is it lack of time, some technical problem or is there a reason why it should not exist?
soc
  • 27,983
  • 20
  • 111
  • 215