简体   繁体   中英

How to represent binary and n-ary full tree using arrays?

I would like to represent a tree like this:

树1

As you can see internal nodes are empty and leaves contain strings.

What is the best way to do that using only arrays?

and if tree is n-ary like this?

树2

Try giving some dummy names to the nodes which doesn't have any values. Then traverse the tree either using pre/post order techniques and store the values in array.

For the first example: Using preorder

Using A as root

A, A, V10, V9, A, V8, A, A, V7, V6, A, V5, A, A, V4, V3, A, V2, V1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM