Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save vinodkiran/1fd913c1cd37d697d81a89a462e9a50f to your computer and use it in GitHub Desktop.

Select an option

Save vinodkiran/1fd913c1cd37d697d81a89a462e9a50f to your computer and use it in GitHub Desktop.
using System;
namespace CSharpBtree
{
public class BTree
{
public Node rootNode;
//assumption: array is sorted and not null
public BTree(int[] array)
{
rootNode = constructNode(array);
}
private int[] spliceLeft(int[] array, int splicePoint)
{
int[] newArray = new int[splicePoint];
for (int i = 0; i < splicePoint; i++)
{
newArray[i] = array[i];
}
return newArray;
}
private int[] spliceRight(int[] array, int splicePoint)
{
int[] newArray = new int[splicePoint];
int b = 0;
for (int i = splicePoint+1; i < array.Length; i++)
{
newArray[b] = array[i];
b++;
}
return newArray;
}
private Node constructNode(int[] array)
{
if (array.Length == 0)
{
return null;
}
int midPoint = array.Length/2;
Node node = new Node(array[midPoint]);
Node lNode = constructNode(spliceLeft(array, midPoint));
if (lNode != null)
{
node.SetLeftNode(lNode);
}
Node rNode = constructNode(spliceRight(array, midPoint));
if (rNode != null)
{
node.SetRightNode(rNode);
}
return node;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment