Array-list implementation of tree problem

  • Thread starter s3a
  • Start date
  • Tags
    Tree
To make the next problem more interesting, it would be nice to find the minimum number of empty entries in an array that could store a tree that has N levels and maxTreeEntries entries.In summary, the conversation discusses drawing a binary tree based on given traversals and storing it in an array-list. The binary tree is then analyzed to find the contents of the array-list and the minimum number of empty entries needed to store the tree.
  • #1
s3a
818
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
  • #3
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
 
  • #4
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.
 

1. What is an array-list implementation of a tree?

An array-list implementation of a tree refers to a data structure in which a tree is represented as an array. Each node in the tree is assigned a unique index in the array, and the relationship between parent and child nodes is determined by the index values.

2. What are the advantages of using an array-list implementation of a tree?

One advantage of this implementation is that it allows for efficient storage and retrieval of data, as arrays have constant access time. It also simplifies tree traversal algorithms as they can be written using simple loops instead of recursive functions.

3. What are the limitations of an array-list implementation of a tree?

The main limitation is that it requires a fixed amount of memory, which can be wasteful if the tree is not full. Inserting or deleting nodes in the middle of the tree can also be inefficient, as it may require shifting all the elements in the array.

4. How do you insert a new node in an array-list implementation of a tree?

To insert a new node, the array needs to be resized to accommodate the new node. The parent node's index should be calculated, and the new node's index should be assigned as the last element in the array. The relationship between the parent and child nodes should then be updated.

5. How is the height of a tree calculated in an array-list implementation?

The height of a tree in an array-list implementation is calculated by determining the number of levels in the tree. This can be done by finding the index of the last element in the array and dividing it by the number of child nodes each parent node can have (usually 2). The result will be the height of the tree.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Replies
9
Views
1K
Back
Top