A
A
Axretit2022-01-09 18:38:48
Programming
Axretit, 2022-01-09 18:38:48

How to implement a tree based on a linked list?

It is necessary to implement a tree based on a linked linear list. As much as possible?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
Wataru, 2022-01-09
@Axretit

In general, a tree and a list are not isomorphic to each other. The tree has a hierarchy. A tree cannot be implemented with a list (single).
Perhaps you need to store the children of each vertex as a linked list (then you will have a bunch of lists). Also, a popular approach (actually doing the same thing) is to store at each node a reference/pointer to the first child and the next sibling. So all the lists will be mixed into one big structure. But here, however, unlike the linked list, there are still 2 types of links.

V
Vasily Demin, 2022-01-09
@includedlibrary

Each element of the list can have either one successor or zero. You probably need to implement on pointers.
If you really need it directly on lists, then you can number the elements of the list and store descendants in each.
Something like this:
list:
61db03b5f393d494297825.png

  1. [2, 3]
  2. [4, 5]
  3. []

R
res2001, 2022-01-09
@res2001

A tree based on a linked list is generally the first thing that comes to mind when you think about how to implement a tree. True, this is not an ordinary sequential linked list, but a hierarchical one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question