HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

how is data in a tree stored in memory?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
storedmemoryhowdatatree

Problem

I am not so familiar with trees.

How is a tree stored in memory? what is the data structure type used for storing it?

As a string?linked list? stack (!)?

Or is there some kind of data storage model for trees?

Solution

Usually it's stored as an adjacency list. Which is basically a linked list for every single node. So the linked list of a node u contains every node v such that (u,v) is a valid edge of the tree.

The adjacency list of this tree would be:

1 ->
 3 -> 1, 4
 4 ->
 5 -> 3, 8
 7 -> 
 8 -> 7, 10
10 ->


It can also be stored using an adjacency matrix. But that's used when the nodes of the tree are less (since it requires more memory). Adjacency list is better for sparse graphs.

Code Snippets

1 ->
 3 -> 1, 4
 4 ->
 5 -> 3, 8
 7 -> 
 8 -> 7, 10
10 ->

Context

StackExchange Computer Science Q#75416, answer score: 3

Revisions (0)

No revisions yet.