-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDepthLists.cs
37 lines (31 loc) · 1.07 KB
/
DepthLists.cs
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
using System;
using System.Collections.Generic;
using PracticeQuestionsSharp.DataStructures;
namespace PracticeQuestionsSharp.Exercises.Binary_Tree
{
public static class DepthLists
{
//Return a list for each depth of a binary tree.
// Array-based tree with IEnumerable implemented to move through the array in order.
public static List<List<T>> GetDepthLists<T>(this MinHeap<T> tree) where T : IComparable<T>
{
if (tree == null || tree.IsEmpty) return null;
var depthLists = new List<List<T>>();
depthLists.Add(new List<T>());
int nodesAtDepth = 1;
int currentNodes = 0;
foreach (T node in tree)
{
if (currentNodes == nodesAtDepth)
{
nodesAtDepth *= 2;
currentNodes = 0;
depthLists.Add(new List<T>());
}
depthLists[depthLists.Count - 1].Add(node);
currentNodes++;
}
return depthLists;
}
}
}