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
131
votes
6 answers

Tree view of a directory/folder in Windows?

In Linux/KDE, I can see a directory as a tree. How can I do it in Windows 7? Consider I do NOT mean "Windows Explorer". This just shows the directories, I also want the files.
Pietro
  • 12,086
  • 26
  • 100
  • 193
130
votes
4 answers

What is the difference between trie and radix trie data structures?

Are the trie and radix trie data structures the same thing? If they aren't the same, then what is the meaning of radix trie (AKA Patricia trie)?
Aryak Sengupta
  • 1,727
  • 2
  • 18
  • 23
122
votes
7 answers

Definition of a Balanced Tree

I am just wondering if someone might be able to clarify the definition of a balanced tree for me. I have that "a tree is balanced if each sub-tree is balanced and the height of the two sub-trees differ by at most one. I apologize if this is a dumb…
Mark Soric
  • 1,381
  • 2
  • 9
  • 8
113
votes
11 answers

Why DFS and not BFS for finding cycle in graphs

Predominantly DFS is used to find a cycle in graphs and not BFS. Any reasons? Both can find if a node has already been visited while traversing the tree/graph.
113
votes
15 answers

How to flatten tree via LINQ?

So I have simple tree: class MyNode { public MyNode Parent; public IEnumerable Elements; int group = 1; } I have a IEnumerable. I want to get a list of all MyNode (including inner node objects (Elements)) as one flat list Where…
myWallJSON
  • 9,110
  • 22
  • 78
  • 149
109
votes
11 answers

Convert a series of parent-child relationships into a hierarchical tree?

I have a bunch of name-parentname pairs, that I'd like to turn into as few heirarchical tree structures as possible. So for example, these could be the pairings: Child : Parent H : G F : G G : D E : D A : E B : C C : E …
Eric
  • 95,302
  • 53
  • 242
  • 374
101
votes
7 answers

Is there a module for balanced binary tree in Python's standard library?

Is there a module for an AVL tree or a red–black tree or some other type of a balanced binary tree in the standard library of Python?
aeter
  • 11,960
  • 6
  • 27
  • 29
95
votes
6 answers

How to render a tree in Twig

I would like to render a tree with an undetermined depth (children of children of children, etc.). I need to loop through the array recursively; how can I do this in Twig?
T-RonX
  • 1,053
  • 1
  • 9
  • 7
91
votes
3 answers

Using self.xxxx as a default parameter - Python

I'm trying to simplify one of my homework problems and make the code a little better. What I'm working with is a binary search tree. Right now I have a function in my Tree() class that finds all the elements and puts them into a list. tree =…
chrisheinze
  • 1,129
  • 2
  • 9
  • 14
91
votes
10 answers

ASCII Library for Creating "Pretty" Directory Trees?

Is there some *nix tool or perl/php library that will let you easily create directory tree visualizations that look like the following? www |-- private | |-- app | | |-- php | | | |-- classes | | | +-- scripts | | …
Alana Storm
  • 164,128
  • 91
  • 395
  • 599
90
votes
5 answers

Worst case in Max-Heapify - How do you get 2n/3?

In CLRS, third Edition, on page 155, it is given that in MAX-HEAPIFY, The children’s subtrees each have size at most 2n/3—the worst case occurs when the bottom level of the tree is exactly half full. I understand why it is worst when the bottom…
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
90
votes
5 answers

Calculate minimal operations to make two tree structures identical

This is more of a CS question, but an interesting one : Let's say we have 2 tree structures with more or less the same nodes reorganized. How would you find any in some sense minimal sequence of operations MOVE(A, B) - moves node A under node B…
Tomas Vana
  • 18,317
  • 9
  • 53
  • 64
90
votes
5 answers

looping through an object (tree) recursively

Is there a way (in jQuery or JavaScript) to loop through each object and it's children and grandchildren and so on? If so... can I also read their name? Example: foo :{ bar:'', child:{ grand:{ greatgrand: { //and so on } …
Val
  • 17,336
  • 23
  • 95
  • 144
88
votes
4 answers

When to choose RB tree, B-Tree or AVL tree?

As a programmer when should I consider using a RB tree, B- tree or an AVL tree? What are the key points that needs to be considered before deciding on the choice? Can someone please explain with a scenario for each tree structure why it is chosen…
Palladin
  • 983
  • 1
  • 7
  • 10
87
votes
11 answers

With ' N ' no of nodes, how many different Binary and Binary Search Trees possible?

For Binary trees: There's no need to consider tree node values, I am only interested in different tree topologies with 'N' nodes. For Binary Search Tree: We have to consider tree node values.
siva
  • 1,693
  • 2
  • 21
  • 29