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
4
votes
1 answer

How to filter nested JSON and preserve branch hierarchy for matches?

This one is tricky... There are a number of questions and answers on how to traverse trees, but I could not adapt any of the proposed solutions to my special case. My problem is pretty close to Python: How can I filter a n-nested dict of dicts by…
boadescriptor
  • 735
  • 2
  • 9
  • 29
4
votes
2 answers

Height of binary search tree without building it

I have a few million consecutive numbers e.g. {3, 1, 2, ...}. In the same order as they are, numbers are inserted into a binary search tree. I need to found what is the height of the tree. Is there any method to determine a height of the tree,…
Lukas1212
  • 41
  • 1
  • 1
4
votes
2 answers

Deeply nested subqueries for traversing trees in MySQL

I have a table in my database where I store a tree structure using the hybrid Nested Set (MPTT) model (the one which has lft and rght values) and the Adjacency List model (storing parent_id on each node). my_table (id, parent_id, lft, rght,…
nickf
  • 537,072
  • 198
  • 649
  • 721
4
votes
2 answers

wxPython TreeCtrl without showing root while still showing arrows

I am making a python tree visualizer using wxPython. It would be used like so: show_tree([ 'A node with no children', ('A node with children', 'A child node', ('A child node with children', 'Another child')) ]) It worked fine but it shows a root…
None
  • 3,875
  • 7
  • 43
  • 67
4
votes
2 answers

Pushing item into a multi-dimensional tree like structure in JavaScript

I have an array of objects which goes on something like this var obj = [{ id: 23, name: 'Test1', children: [{ id: 24, name: 'Test2, children: [..] },{ id: 25, name: 'Test2, …
uniquerockrz
  • 231
  • 3
  • 9
4
votes
1 answer

How do I limit tree nodes from being dragged from their current node in flex 3?

So I have a flex tree component with a xmllistcollection as it's data provider. I would like to be able to rearrange the leaves and branches in the tree by drag and drop. I want to limit the drop area to the current level of the item being dragged.…
smartdirt
  • 1,027
  • 3
  • 14
  • 27
4
votes
1 answer

Flatten a tree (nested list) up to a certain depth

I have a recursive algebraic data type to implement Lisp-like nested lists: data T a = N a | L [T a] deriving (Show, Eq) L [N 1, L [N 2, L [N 3, L [N 4], N 5], N 6], N 7] is equivalent to Lisp's (1 (2 (3 (4) 5) 6) 7). Now I want to partially…
Mirzhan Irkegulov
  • 17,660
  • 12
  • 105
  • 166
4
votes
1 answer

Covariant Trees in Scala

Looking at scalaz.Tree[A], it is invariant in A. I'm looking for a multi-way tree which I can dump values of a hierarchy in E.g. if I have an ADT of trait MyThing case object Thing1 extends MyThing case object Thing2 extends MyThing And I want a…
NightWolf
  • 7,694
  • 9
  • 74
  • 121
4
votes
2 answers

Tree searching algorithm: how to determine quickly if A has a sure-to-win strategy

The original question goes like this: There are 99 stones, A and B are playing a game that, each one take some stones in turn, and each turn one can only take 1, 2, 4, or 6 stones, the one take the last stone wins. If A is the first one to take…
athos
  • 6,120
  • 5
  • 51
  • 95
4
votes
2 answers

How to generate a

class tree: def __init__(self, name='a', childs=[]): self.name = name self.childs = childs output:
  • Introduction
    • Sub Intro
  • Module 1
user210221
  • 41
  • 2
4
votes
2 answers

Why is the root of an RB tree black?

If, after insertion of a new element, the root of an RB tree becomes red, its color is changed to black. Why is that? It appears to me that red roots would work just as fine. Is this color change simply done so that subsequent operations can be done…
4
votes
1 answer

(R) Plot dendrograms BRT models from gbm.step

(previously posted here, to the wrong sub, with not enough info, which was closed, I edited, the edits seem to have been deleted, & the post consigned to purgatory, so apologies for re-posting, I don't know whether the previous post can/should be…
dez93_2000
  • 1,730
  • 2
  • 23
  • 34
4
votes
5 answers

Move node in Nested Sets tree

I am working on an adjacency list with mySQL and can not (atleast by myself) do the thinking needed to make a decent enough query to be able to move a set of nodes (together with eventual children nodes) around. The table has following columns: id …
Industrial
  • 41,400
  • 69
  • 194
  • 289
4
votes
4 answers

Which applications use R-Trees?

Besides GIS applications, which other applications or libraries use R-trees and its variants?
filinep
  • 529
  • 2
  • 11
  • 17
4
votes
1 answer

Drawing colored trees with NetworkX

NOTE -- This is a complete rewrite of the previous question, which I think was too complex. It presents a much simpler version which, if I can solve, will result in progress. I have adapted my code from this SO answer: Is there a way to guarantee…
Wapiti
  • 1,851
  • 2
  • 20
  • 40