From f789821c88ff09ce88f1eb115e53671493cea9a7 Mon Sep 17 00:00:00 2001 From: AndyLiuLei <61106135+AndyLiuLei@users.noreply.github.com> Date: Sun, 16 Feb 2020 16:01:39 +0800 Subject: [PATCH] Update BTree.java need to update the key of the firstchild of the node while insert and return a new node --- src/main/java/edu/princeton/cs/algs4/BTree.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/edu/princeton/cs/algs4/BTree.java b/src/main/java/edu/princeton/cs/algs4/BTree.java index 35183e845..e052ff313 100644 --- a/src/main/java/edu/princeton/cs/algs4/BTree.java +++ b/src/main/java/edu/princeton/cs/algs4/BTree.java @@ -181,7 +181,15 @@ private Node insert(Node h, Key key, Value val, int ht) { else { for (j = 0; j < h.m; j++) { if ((j+1 == h.m) || less(key, h.children[j+1].key)) { + int k = j ; Node u = insert(h.children[j++].next, key, val, ht-1); + + // update key + Node nextNode = h.children[k].next ; + if (null != nextNode ) { + h.children[k].key = nextNode.children[0].key ; + } + if (u == null) return null; t.key = u.children[0].key; t.next = u;