Array-list implementation of tree problem

  • Thread starter Thread starter s3a
  • Start date Start date
  • Tags Tags
    Tree
Click For Summary

Discussion Overview

The discussion revolves around a homework problem involving the construction of a binary tree from given traversals (inorder and postorder) and the subsequent representation of that tree as an array-list. Participants explore the implications of treating the tree as a complete binary tree and the resulting array structure.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a binary tree based on the provided traversals and seeks help with its array-list representation.
  • Another participant questions whether the array-list representation would contain many empty cells due to the tree not being complete, suggesting that a complete tree would fill indices from 0 to 12 without gaps.
  • A third participant points out that the binary tree has 6 levels and discusses the maximum number of entries in a complete binary tree of that height, indicating that there would be many empty entries in the array-list representation.
  • There is a mention of the indexing differences in programming languages, noting that in C-based languages, array indices start at 0, which may affect the indexing of the tree elements.

Areas of Agreement / Disagreement

Participants express uncertainty regarding the correct representation of the binary tree as an array-list, with some agreeing that many entries would be empty while others clarify the indexing method. No consensus is reached on the exact contents of the array-list.

Contextual Notes

Participants note that the tree is not complete, which affects the array-list representation. There are unresolved questions about the indexing method and the implications of treating the tree as complete.

s3a
Messages
828
Reaction score
8

Homework Statement


a) Draw a single binary tree that gave the following traversals:
Inorder: T N C K V A S M W Q B R L
Postorder: T C N V S A W M B Q L R K

b) Assume that the binary tree from the above part (a) of this question is stored in an array-list as a complete binary tree as discussed in class. Specify the contents of such an array-list for this tree.

Homework Equations


If the first element's index starts at pos=1, the index of the left child of a certain element is 2*pos, and the index of the right child of a certain element is 2*pos+1.

The Attempt at a Solution


Here is the tree sought by part a.:
...K
.../\
..N...R
../\.../\
T..C...Q..L
.../\
...M..B
.../\
...A..W
.../\
..V..S

I am trying to do part b.

If I (incorrectly) assume that the above tree is a complete tree (like it seems that the question wants me to do), I get stuck when implementing the tree into an array(list), as can be seen below. (Sorry about the formatting.):

Indices: 0 1 2 3 4 5 6 7 8 9 10 11 12
Elements in the same order as the indices above: K N R T C Q L ? ? ? ? M B

What am I doing wrong?

Any input would be GREATLY appreciated!
 
Physics news on Phys.org
Mark44 said:
Does this wiki article help? https://en.wikipedia.org/wiki/Tree_traversal
They discuss depth-first searches - pre-order, in-order, and post-order, as well as breadth-first searches.
I do understand part a.

As for part b, I'm not 100% sure. Is it the case that the array-list would have a lot of cells with no values filled in as shown in my array-list representation below (unlike a tree that were complete, whose array-list representation would have indices 0 to 12, where each cell would contain an element)?:
Index | Element Contained
0 | K
1 | N
2 | R
3 | T
4 | C
5 | Q
6 | L
7 | empty
8 | empty
9 | empty
10 | empty
11 | M
12 | B
13 | empty
14 | empty
15 | empty
16 | empty
17 | empty
18 | empty
19 | empty
20 | empty
21 | empty
22 | empty
23 | A
24 | W
25 | empty
26 | empty
27 | empty
28 | empty
29 | empty
30 | empty
31 | empty
32 | empty
33 | empty
34 | empty
35 | empty
36 | empty
37 | empty
38 | empty
39 | empty
40 | empty
41 | empty
42 | empty
43 | empty
44 | empty
45 | empty
46 | empty
47 | V
48 | S
 
Your binary tree has 6 levels, with 13 entries, so a complete binary tree with this many levels would have at most 63 entries (1 at the root at level 0, 2 at the level 1, 4 at level 2, and so on down to 32 at level 5). 1 + 2 + 4 + 8 + 16 + 32 = 63.

Based on what you wrote in Relevant Equations, K should be at index 1 in the array. K's left child N should be at index 2*1 = 2, and the right child R should be at index 2*1 + 1 = 3. Treating N as the root of a subtree, its left child T should be stored at index 2*2 = 4, and the right child C stored at index 2*(3) + 1 = 7. Continue the indexing with the subtree headed by R until you all of the tree entries are indexed.

If you do this in C or other C-based languages, array indexes start at 0, so all the numbers I wrote are too large by 1, and can be adjusted accordingly. Inasmuch as your tree isn't complete, there will be a 63 - 13 = 50 entries in the array that are empty.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
3
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
9K