Created
July 9, 2021 07:32
-
-
Save vinodkiran/1fd913c1cd37d697d81a89a462e9a50f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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