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
33
votes
11 answers

How to Serialize Binary Tree

I went to an interview today where I was asked to serialize a binary tree. I implemented an array-based approach where the children of node i (numbering in level-order traversal) were at the 2*i index for the left child and 2*i + 1 for the right…
worker1138
  • 2,071
  • 5
  • 29
  • 36
33
votes
1 answer

Python Element Tree Writing to New File

Hi so I've been struggling with this and can't quite figure out why I'm getting errors. Trying to export just some basic XML into a new file, keeps giving me a TypeError. Below is a small sample of the code from xml.etree.ElementTree import…
Kyle Zimmerman
  • 443
  • 1
  • 4
  • 5
33
votes
3 answers

Represent directory tree as JSON

Is there any easy way to generate such a JSON? I found os.walk() and os.listdir(), so I may do recursive descending into directories and build a python object, well, but it sounds like reinventing a wheel, maybe someone knows working code for such a…
Andrey Moiseev
  • 3,914
  • 7
  • 48
  • 64
33
votes
8 answers

Obtain forest out of tree with even number of nodes

I'm stuck on a code challenge, and I want a hint. PROBLEM: You are given a tree data structure (without cycles) and are asked to remove as many "edges" (connections) as possible, creating smaller trees with even numbers of nodes. This problem is…
g4ur4v
  • 3,210
  • 5
  • 32
  • 57
32
votes
3 answers

Display complete dependency tree with Leiningen

I understand that lein deps :tree displays a dependency tree of all the project dependencies (implicit and explicit). However, "each dependency is only shown once within a tree." I'd really like to see a tree where this wasn't the case, and that if…
metasoarous
  • 2,854
  • 1
  • 23
  • 24
31
votes
1 answer

Finding all parents in mysql table with single query (Recursive Query)

I have this schema Sample Data | ID | TITLE | CONTROLLER | METHOD | PARENT_ID | |----|-------------------|------------|-------------------|-----------| | 1 | Dashboard | admin | dashboard | 0…
Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103
31
votes
5 answers

Output of tree in command prompt

I was hoping to be able to use the tree /F /A > "desktop"\file.txt command to output only text files. Currently as is, it outputs every file extension. Does anyone know of an easy way to do this?
trippedoutfish
  • 357
  • 1
  • 4
  • 8
30
votes
3 answers

Build a simple, high performance Tree Data Structure in c#

I need to create a product catalog, in tree type. every tree node presents by a ID(string), the functions on the tree data only 2: getChild(string ID), give a ID, get children (no need include childrens' children), if ID is null, get all root nodes…
Eric Yin
  • 8,737
  • 19
  • 77
  • 118
30
votes
4 answers

Fenwick tree vs Segment tree

I needed to compute sums within a range on an array, so I came across Segment Tree and Fenwick Tree and I noticed that both of these trees query and update with the same asymptotic running time. I did a bit more research, and these 2 data structures…
Will Kanga
  • 652
  • 1
  • 6
  • 12
30
votes
1 answer

Use angular-tree-component in "dotnet new angular" project?

When trying to integrate angular-tree-component into a project created with dotnet new angular, I consistently run into an exception: void(0) is not a function. Either the tree never appears, or it appears briefly, but then the error happens and…
William Jockusch
  • 26,513
  • 49
  • 182
  • 323
30
votes
7 answers

'Head First' Style Data Structures & Algorithms Book?

I loved the Head First series book on object oriented design. It was a very gentle and funny introduction to the subject. I am currently taking a data structures class and find the text we are using (Kruse/Ryba Data Structures and Program Design…
samh
30
votes
7 answers

How is the memory of the array of segment tree 2 * 2 ^(ceil(log(n))) - 1?

The link: http://www.geeksforgeeks.org/segment-tree-set-1-sum-of-given-range/. This is the quoted text: We start with a segment arr[0 . . . n-1]. And every time we divide the current segment into two halves(if it has not yet become a segment of…
dauntless
  • 413
  • 1
  • 4
  • 6
30
votes
5 answers

How can I plot a tree (and squirrels) in R?

Here is my tree: tree = data.frame(branchID = c(1,11,12,111,112,1121,1122), length = c(32, 21, 19, 5, 12, 6, 2)) > tree branchID length 1 1 32 2 11 21 3 12 19 4 111 5 5 112 12 6 1121 6 7 …
Remi.b
  • 17,389
  • 28
  • 87
  • 168
29
votes
1 answer

Monad instance for binary tree

I built binary tree with: data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Eq, Ord, Read, Show) How can i make Monad type class instance for this tree? And can i make it on not? i try: instance Monad Tree where …
0xAX
  • 20,957
  • 26
  • 117
  • 206
29
votes
19 answers

In Order Successor in Binary Search Tree

Given a node in a BST, how does one find the next higher key?
shreyasva
  • 13,126
  • 25
  • 78
  • 101