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
38
votes
10 answers

Generic tree implementation in Java

Is anyone aware of a generic tree (nodes may have multiple children) implementation for Java? It should come from a well trusted source and must be fully tested. It just doesn't seem right implementing it myself. Almost reminds me of my university…
Ivan Koblik
  • 4,285
  • 1
  • 30
  • 33
37
votes
5 answers

Height of a binary tree

Consider the following code: public int heightOfBinaryTree(Node node) { if (node == null) { return 0; } else { return 1 + Math.max(heightOfBinaryTree(node.left), …
Programmer
  • 6,565
  • 25
  • 78
  • 125
37
votes
6 answers

Plot trees for a Random Forest in Python with Scikit-Learn

I want to plot a decision tree of a random forest. So, i create the following code: clf = RandomForestClassifier(n_estimators=100) import pydotplus import six from sklearn import tree dotfile = six.StringIO() i_tree = 0 for tree_in_forest in…
Zoya
  • 1,195
  • 2
  • 12
  • 14
36
votes
3 answers

Hierarchical/tree database for directories path in filesystem

I want to store the directories (present on the disk) into a database, maintaining their hierarchical/tree structure. Here's a figure: (ROOT) / \ Dir2 Dir3 …
JOHN
  • 587
  • 1
  • 5
  • 9
36
votes
9 answers

What's the difference between ordering and sorting?

No, it's not a question from a class, I'm studying trees and heaps ( partially sorted binary tree ) by myself and I was wondering how to correctly define this 2 properties/actions in relation to a generic data structure.
user2485710
  • 9,451
  • 13
  • 58
  • 102
35
votes
5 answers

PHP tree structure for categories and sub categories without looping a query

I'm trying to create a list of categories with any number of sub categories, where sub categories can also has their own sub categories. I have selected all categories from the Mysql db, the cats are in a standard associate array list, each category…
John
  • 351
  • 1
  • 4
  • 3
35
votes
6 answers

Height of a tree with only one node

According to Wikipedia, The height of a tree is the length of the path from the root to the deepest node in the tree. A (rooted) tree with only one node (the root) has a height of zero (or one). I dont get it - is it zero or one (or both)?
Snowman
  • 31,411
  • 46
  • 180
  • 303
35
votes
2 answers

How to convert to D3's JSON format?

While following numerous D3 examples, data usually gets formatted in the format given in flare.json: { "name": "flare", "children": [ { "name": "analytics", "children": [ { "name": "cluster", "children": [ {"name":…
Legend
  • 113,822
  • 119
  • 272
  • 400
34
votes
3 answers

Tail recursive function to find depth of a tree in Ocaml

I have a type tree defined as follows type 'a tree = Leaf of 'a | Node of 'a * 'a tree * 'a tree ;; I have a function to find the depth of the tree as follows let rec depth = function | Leaf x -> 0 | Node(_,left,right) -> 1 + (max (depth…
ppaul74
  • 751
  • 1
  • 13
  • 21
34
votes
2 answers

How to represent a tree like structure in a db

I'm starting a project and I'm in the designing phase: I.e., I haven't decided yet on which db framework I'm going to use. I'm going to have code that creates a "forest" like structure. That is, many trees, where each tree is a standard: nodes and…
Guy
  • 14,178
  • 27
  • 67
  • 88
34
votes
3 answers

Fast, templated, C++ Octree implementation

I've been searching high and low (mostly on google) for a fast, efficient, templated (ie. with STL-like properties) octree implementation, without success. I want to use this in the context of a 3D scene graph. Does such a thing exist, or do people…
Robinson
  • 9,666
  • 16
  • 71
  • 115
34
votes
15 answers

Help me understand Inorder Traversal without using recursion

I am able to understand preorder traversal without using recursion, but I'm having a hard time with inorder traversal. I just don't seem to get it, perhaps, because I haven't understood the inner working of recursion. This is what I've tried so…
Srikanth
  • 11,780
  • 23
  • 72
  • 92
34
votes
1 answer

Representing an Abstract Syntax Tree in C

I'm implementing a compiler for a simple toy language in C. I have a working scanner and parser, and a reasonable background on the conceptual function/construction of an AST. My question is related to the specific way to represent an AST in C. …
34
votes
3 answers

How do I show marriages in a d3.js based 'family-tree'?

I'm a HTML/CSS developer, researching javascript solutions for building a 'family-tree' which needs to show marriages (from outside the family, of course) in a meaningful way. Essentially I'm looking at basing it upon a dendrogram, based on d3.js,…
byzantine cucumber
  • 531
  • 1
  • 5
  • 12
34
votes
5 answers

How to display XML in a HTML page as a collapsible and expandable tree using Javascript?

How to display a XML document in a HTML page as a collapsible and expandable tree? I'd like to display a XML document inside a HTML page as a nicely pretty printed tree structure. I'd like to be able to expand and collapse tree branches. For example…
Juha Syrjälä
  • 33,425
  • 31
  • 131
  • 183