Posts Forest Playground
Post
Cancel

Forest Playground

Introduction

View Instructions.

Alternatively, see comments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import java.lang.Math;
/**
 * @author  Don Allen
 */
class ForestPlayGround 
{
    String[] myTree;

    /*
     *   PreConditions
     *        tree is a valid represntation fo a binary tree
     *        tree != null
     *        tree.size() >= 0
     */
    public ForestPlayGround(String[] tree)
    {
        myTree = tree;
    }

    /*
     *    return the number of non null nodes in myTree
     */
    public int getNumNodes()
    {
        int numNodes = 0;
        for(int i = 0; i<myTree.length; i++)
        {
          if(myTree[i]!=null)
          {
            numNodes++;
          }
        }

        return numNodes;
    }

    /*
     *    A leaf is a node in the tree in which both children have 0 children.
     *    An empty tree contains NO leafs
     */
    public int getNumLeafs()
    {

      
      int numLeafs = 0;
      int left, right, counter;
      
      for(int i = 1; i<myTree.length; i++)
      {
        if(myTree[i]==null)
        {
          continue;
        }
        
        System.out.println(i);
        counter = 0;
        left = (2*i)+1;
        right = (2*i)+2;
        if(left>myTree.length-1)
        {
          counter++;
        }
        else if(myTree[left]==null)
        {
          counter++;
        }
        if(right>myTree.length-1)
        {
          counter++;
        }
        else if(myTree[right]==null)
        {
          counter++;
            
        }
        if(counter==2)
        {
          numLeafs++;
        }
        
      }

      return numLeafs;
    }

    /*
     *    Precondition:   0 <= p < myTree.length
     *
     *    returns:
     *        the right child of myTree[p]
     *        null if myTree[p] does not have a right child
     */
    public String getRightChild(int p)
    {
      int right = (2*p)+2; 
      if(right<=myTree.length-1)
        {
          if(myTree[right]==null)
          {
            return null;
          }
          else
          {
            return myTree[right];
          }
           
        }
      return null;
      
        
    }

    /*
     *    Precondition:   0 <= p < myTree.length
     *
     *    returns:
     *        the left child of myTree[p]
     *        null if myTree[p] does not have a left child
     */
    public String getLeftChild(int p)
    {
      int left = (2*p)+1; 
      if(left<=myTree.length-1)
        {
          if(myTree[left]==null)
          {
            return null;
          }
          else
          {
            return myTree[left];
          }
           
        }
      return null;
    }

    /*
     *    Precondition:   0 <= p < myTree.length
     *                    myTree[p] != null
     *
     *    returns:
     *        the parent of myTree[p]
     *        null if myTree[p] does not have a parent
     */
    public String getParent(int p)
    {
      if(p==0)
      {
        return null;
      }
      
      int parent = (p-1)/2; 
      if(parent>=0)
        {
          if(myTree[parent]==null)
          {
            return null;
          }
          else
          {
            return myTree[parent];
          }
           
        }
      return null;
    }
    
    public int getIndexOfParent(int p)
    {
      int parent = (p-1)/2; 
      return parent;
    }
    public int getIndexOfLeft(int p)
    {
      int left = (2*p)+1;  
      return left;
    }
        public int getIndexOfRight(int p)
    {
      int right = (2*p)+2; 
      return right;
    }


    /*
     *    Precondition:   0 <= p < myTree.length
     *                    myTree[p] != null
     *
     *    returns:
     *        the List of all ancestors (parent and their parent ans so on) of myTree[p]
     *        an empty List if myTree[p] does not have a parent
     */
    public List<String> getAncestors(int p)
    {
        
        List<String> ans = new ArrayList<String>();
        int temp = getIndexOfParent(p);
        while(temp>=1)
        {
          ans.add(myTree[temp]);
          temp = getIndexOfParent(temp);
          
        }
        if(myTree[0]!=null && (myTree[1]!=null || myTree[2]!=null))
        {
          ans.add(myTree[0]);
        }
        
        return ans;
    }

    /*
     * Preconditions:
     *    myTree[p] != null
     *    0 <= p < myTree.length
     */
    public List<String> getDescendants(int p){
        List<String> ans = new ArrayList<String>();
        if(getLeftChild(p)!=null){
            ans.add(getLeftChild(p));
            for(String str: getDescendants(2*p+1))
                ans.add(str);
        }
        if(getRightChild(p)!=null){
            ans.add(getRightChild(p));
            for(String str: getDescendants(2*p+2))
                ans.add(str);
        }
        return ans;
    }

    /*
     *    In a complete binary tree every level, except possibly the last, is completely filled,
     *    and all nodes in the last level are as far left as possible.
     *    
     *    This implies that the end of the array may contain multiple nulls
     *                               and the array/tree may still be complete
     */
public boolean isComplete()
{
    boolean foundNull = false;
    for(String s:myTree)
    {
        if(s==null)
        {
            foundNull=true;
        }
        else if(foundNull)
        {
            return false;
        }
    
  }
  return true;
  }

    /*
     *    A full binary tree is a tree in which every node in the tree has either 0 or 2 children.
     */
    public boolean isFull()
     {
  
    if(myTree.length==0)
    {
      return true;
    }

    for(int i = 0; i<myTree.length; i++)
    {
      if(getRightChild(i)!=null && getLeftChild(i)!=null)
      {
        continue;
      }
      else if(getRightChild(i)==null && getLeftChild(i)==null)
      {
        continue;
      }
      else
      {
        return false;
      }
      
    }
    return true;
  }
}
This post is licensed under CC BY 4.0