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

15264 questions
1397
votes
7 answers

What are the options for storing hierarchical data in a relational database?

Good Overviews Generally speaking, you're making a decision between fast read times (for example, nested set) or fast write times (adjacency list). Usually, you end up with a combination of the options below that best fit your needs. The following…
orangepips
  • 9,601
  • 6
  • 29
  • 56
540
votes
14 answers

What is the most efficient/elegant way to parse a flat table into a tree?

Assume you have a flat table that stores an ordered tree hierarchy: Id Name ParentId Order 1 'Node 1' 0 10 2 'Node 1.1' 1 10 3 'Node 2' 0 20 4 'Node 1.1.1' 2 10 5 …
Tomalak
  • 306,836
  • 62
  • 485
  • 598
515
votes
24 answers

How to implement a tree data-structure in Java?

Is there any standard Java library class to represent a tree in Java? Specifically I need to represent the following: The sub-tree at any node can have an arbitrary number of children Each node (after the root) and it's children will have string…
ikl
  • 5,169
  • 3
  • 13
  • 4
489
votes
6 answers

Unable to show a Git tree in terminal

Killswitchcollective.com's old article, 30 June 2009, has the following inputs and outputs git co master git merge [your_branch] git push upstream A-B-C-D-E A-B-C-D-E-F-G \ ----> \ your branch …
Léo Léopold Hertz 준영
  • 119,377
  • 159
  • 417
  • 655
397
votes
14 answers

Why does the C++ STL not provide any "tree" containers?

Why does the C++ STL not provide any "tree" containers, and what's the best thing to use instead? I want to store a hierarchy of objects as a tree, rather than use a tree as a performance enhancement...
Roddy
  • 63,052
  • 38
  • 156
  • 264
340
votes
12 answers

Difference between binary tree and binary search tree

Can anyone please explain the difference between binary tree and binary search tree with an example?
Neel
  • 8,893
  • 14
  • 46
  • 70
313
votes
9 answers

What is the difference between tree depth and height?

This is a simple question from algorithms theory. The difference between them is that in one case you count number of nodes and in other number of edges on the shortest path between root and concrete node. Which is which?
Gabriel Ščerbák
  • 16,864
  • 8
  • 33
  • 50
254
votes
16 answers

How can I implement a tree in Python?

I am trying to construct a General tree. Are there any built-in data structures in Python to implement it?
vishnu
  • 3,149
  • 3
  • 17
  • 6
239
votes
9 answers

Google Chrome display JSON AJAX response as tree and not as a plain text

I cannot find an answer to this one: My AJAX calls return JSON data. In Google Chrome Developer Tools > Resources > XHR when I click on the resource on the left and then on the Content tab I see the JSON string as a string and not as a tree as…
GRboss
  • 5,501
  • 5
  • 19
  • 20
217
votes
3 answers

What are the differences between segment trees, interval trees, binary indexed trees and range trees?

What are differences between segment trees, interval trees, binary indexed trees and range trees in terms of: Key idea/definition Applications Performance/order in higher dimensions/space consumption Please do not just give definitions.
Aditya
  • 5,101
  • 4
  • 27
  • 50
184
votes
18 answers

Non-recursive depth first search algorithm

I am looking for a non-recursive depth first search algorithm for a non-binary tree. Any help is very much appreciated.
mousey
  • 10,655
  • 15
  • 47
  • 54
173
votes
18 answers

How to efficiently build a tree from a flat structure?

I have a bunch of objects in a flat structure. These objects have an ID and a ParentID property so they can be arranged in trees. They are in no particular order. Each ParentID property does not necessarily matches with an ID in the structure.…
Costo
  • 5,590
  • 7
  • 30
  • 35
163
votes
31 answers

Build tree array from flat array in javascript

I have a complex json file that I have to handle with javascript to make it hierarchical, in order to later build a tree. Every entry of the json has : id : a unique id, parentId : the id of the parent node (which is 0 if the node is a root of the…
Franck
  • 1,859
  • 3
  • 13
  • 22
156
votes
5 answers

Database Structure for Tree Data Structure

What would be the best way to implement a customizable tree data structure (meaning, a tree structure with an unknown number of level) in a database? I've done this once before using a table with a foreign key to itself. What other implementations…
CodeMonkey1313
  • 14,477
  • 17
  • 71
  • 109
154
votes
11 answers

What's the difference between the data structure Tree and Graph?

Academically speaking, what's the essential difference between the data structure Tree and Graph? And how about the tree based search and Graph based search?
user918304
  • 1,747
  • 3
  • 13
  • 9
1
2 3
99 100