@@ -10,6 +10,8 @@ public class MyBTree <K extends Comparable<K>, V> {
1010 */
1111 public final static int M = 5 ;
1212
13+ private Node root ;
14+
1315 /**
1416 * B树中节点的个数
1517 */
@@ -20,13 +22,30 @@ public class MyBTree <K extends Comparable<K>, V> {
2022 */
2123 private int hight = 0 ;
2224
25+ public MyBTree () {
26+ Node root = new Node (0 );
27+ this .root = root ;
28+ }
29+
2330 private static class Node {
2431 private int entryCount ;
2532 private Entry [] children = new Entry [M ];
33+
34+ public Node (int entryCount ) {
35+ this .entryCount = entryCount ;
36+ }
2637 }
2738
2839 private static class Entry {
40+ private Comparable key ;
41+ private Object value ;
42+ private Node next ;
2943
44+ public Entry (Comparable key , Object value , Node next ) {
45+ this .key = key ;
46+ this .value = value ;
47+ this .next = next ;
48+ }
3049 }
3150
3251 public int getSize () {
@@ -36,4 +55,97 @@ public int getSize() {
3655 public int getHight () {
3756 return this .hight ;
3857 }
58+
59+ public V get (K key ) {
60+ if (key == null ) {
61+ throw new NullPointerException ("key can`t be null" );
62+ }
63+ return search (key , root , hight );
64+ }
65+
66+ private V search (K key , Node n , int ht ) {
67+ if (ht == 0 ) {
68+ for (int i = 0 ; i < n .entryCount ; i ++) {
69+ if (eq (key , n .children [i ].key )) {
70+ return (V ) n .children [i ].value ;
71+ }
72+ }
73+ return null ;
74+ } else {
75+ for (int i = 0 ; i < n .entryCount ; i ++) {
76+ if (i + 1 == n .entryCount || less (key , n .children [i ].key )) {
77+ return search (key , n .children [i ].next , --ht );
78+ }
79+ }
80+ }
81+ return null ;
82+ }
83+
84+ public void put (K key , V value ) {
85+ if (key == null ) {
86+ throw new NullPointerException ("key can`t be null" );
87+ }
88+
89+ Node u = insert (key , value , root , hight );
90+ this .size ++;
91+ if (u == null )
92+ return ;
93+
94+ Node n = new Node (2 );
95+ n .children [0 ] = new Entry (root .children [0 ].key , null , root );
96+ n .children [1 ] = new Entry (u .children [0 ].key , null , u );
97+ this .hight ++;
98+ }
99+
100+ public Node insert (K key , V value , Node n , int ht ) {
101+ Entry entry = new Entry (key , value , null );
102+ int index = 0 ;
103+ if (ht == 0 ) {
104+ for (int i = 0 ; i < n .entryCount ; i ++) {
105+ if (i + 1 == n .entryCount || less (key , n .children [i ].key )) {
106+ index = i ;
107+ break ;
108+ }
109+ }
110+ } else {
111+ for (int i = 0 ; i < n .entryCount ; i ++) {
112+ if (i + 1 == n .entryCount || less (key , n .children [i ].key )) {
113+ index = i ;
114+ Node l = insert (key , value , n .children [i ].next , --ht );
115+ if (l == null )
116+ return null ;
117+ entry .key = l .children [0 ].key ;
118+ entry .next = l ;
119+ }
120+ }
121+ }
122+
123+ for (int i = n .entryCount - 1 ; i >= index ; i --) {
124+ n .children [i ] = n .children [i - 1 ];
125+ }
126+ n .children [index ] = entry ;
127+ n .entryCount ++;
128+ if (n .entryCount <= M ) {
129+ return null ;
130+ } else {
131+ return split (n );
132+ }
133+ }
134+
135+ private Node split (Node o ) {
136+ Node n = new Node (M / 2 );
137+ for (int i = 0 ; i < M / 2 ; i ++) {
138+ n .children [i ] = o .children [M / 2 + i ];
139+ }
140+ o .entryCount = M / 2 ;
141+ return n ;
142+ }
143+
144+ private boolean eq (Comparable key1 , Comparable key2 ) {
145+ return key1 .compareTo (key2 ) == 0 ;
146+ }
147+
148+ private boolean less (Comparable key1 , Comparable key2 ) {
149+ return key1 .compareTo (key2 ) < 0 ;
150+ }
39151}
0 commit comments