Created
August 22, 2013 18:21
-
-
Save nm3mon/6310893 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
static class Node { | |
int key; | |
String name; | |
Node next; | |
Node[] children; | |
Node(int key, String name) { | |
this.key = key; | |
this.name = name; | |
} | |
@Override public String toString() { | |
return name + " : " + key; | |
} | |
} | |
static class BTree { | |
Node root; | |
int size; | |
BTree(String rootName) { | |
root = new Node(0, rootName); | |
} | |
Node insert(int level, String nodeName, int childCount) { | |
Node refParent = root; | |
for (int i = 0; i < level; i++) { | |
refParent = refParent.children[0]; | |
} | |
Node prevChildLastNode = null; | |
while (refParent != null) { | |
int ordinal = 0; | |
refParent.children = new Node[childCount]; | |
for (int key = 0; key < childCount; key++) { | |
refParent.children[key] = new Node(ordinal, nodeName); | |
size++; | |
if (key > 0) { | |
refParent.children[key - 1].next = refParent.children[key]; | |
} | |
ordinal++; | |
} | |
if (prevChildLastNode != null) { | |
prevChildLastNode.next = refParent.children[0]; | |
} | |
prevChildLastNode = refParent.children[childCount-1]; | |
refParent = refParent.next; | |
} | |
return root; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment