Question: Im making a 2-3-4 tree but i keep getting this error. Can someone help me fix error. Exception in thread main java.lang.NullPointerException: Cannot read field
Im making a 2-3-4 tree but i keep getting this error. Can someone help me fix error.
Exception in thread "main" java.lang.NullPointerException: Cannot read field "numKeys" because "node" is null at TwoThreeFour.insertKey(TwoThreeFour.java:67) at TwoThreeFour.splitNode(TwoThreeFour.java:109) at TwoThreeFour.insertHelper(TwoThreeFour.java:44) at TwoThreeFour.insertHelper(TwoThreeFour.java:45) at TwoThreeFour.insert(TwoThreeFour.java:34) at Main.main(Main.java:10)
code:
import java.util.*; class TwoThreeFour { private TreeNode root; // TreeNode class represents a node in the 2-3-4 tree private class TreeNode { int[] keys; TreeNode[] children; int numKeys; boolean isLeaf; TreeNode parent; TreeNode() { keys = new int[3]; children = new TreeNode[4]; numKeys = 0; isLeaf = true; } } // Constructor for the 2-3-4 tree TwoThreeFour() { root = null; } // Public method to insert a key into the tree public void insert(int key) { if (root == null) { root = new TreeNode(); root.keys[0] = key; root.numKeys++; } else { insertHelper(root, key); } } // Private recursive method to insert a key into the tree private void insertHelper(TreeNode node, int key) { // If node is a leaf, insert key into node if (node.isLeaf) { if (node.numKeys == 3) { // Split the node if it's full splitNode(node); insertHelper(node, key); } else { insertKey(node, key); } } else { // Find the child node to insert key int i = 0; while (i < node.numKeys && key > node.keys[i]) { i++; } insertHelper(node.children[i], key); // Check if child node was split if (node.children[i].numKeys == 3) { // Split the child node if it's full splitNode(node.children[i]); } } } // Private method to insert a key into a node private void insertKey(TreeNode node, int key) { // Insert key into node in sorted order int i = node.numKeys - 1; while (i >= 0 && node.keys[i] > key) { node.keys[i+1] = node.keys[i]; i--; } node.keys[i+1] = key; node.numKeys++; } // Private method to split a full node private void splitNode(TreeNode node) { // Split the node into two new nodes TreeNode left = new TreeNode(); TreeNode right = new TreeNode(); int midKey = node.keys[1]; // Insert keys and children into the new nodes left.keys[0] = node.keys[0]; left.numKeys++; left.children[0] = node.children[0]; left.children[1] = node.children[1]; right.keys[0] = node.keys[2]; right.numKeys++; right.children[0] = node.children[2]; right.children[1] = node.children[3]; // If node is not a leaf, move middle child to new nodes if (!node.isLeaf) { left.children[2] = node.children[1]; right.children[2] = node.children[3]; } // Set parent node to point to new nodes if (node == root) { root = new TreeNode(); root.keys[0] = midKey; root.numKeys++; root.children[0] = left; root.children[1] = right; left.parent = root; right.parent = root; } else { insertKey(node.parent, midKey); int i = node.parent.numKeys - 1; while (i >= 0 && node.parent.children[i] != node) { i--; } node.parent.children[i] = left; node.parent.children[i+1] = right; left.parent = node.parent; right.parent = node.parent; } } // Public method to delete a key from the tree public void delete(int key) { if (root == null) { return; } else { deleteHelper(root, key); } } // Private recursive method to delete a key from the tree private void deleteHelper(TreeNode node, int key) { // Find the key to delete in the node int i = 0; while (i < node.numKeys && key > node.keys[i]) { i++; } if (i < node.numKeys && key == node.keys[i]) { // Key is found in the node if (node.isLeaf) { // If node is a leaf, delete key from node for (int j = i; j < node.numKeys - 1; j++) { node.keys[j] = node.keys[j+1]; } node.numKeys--; } else { // If node is not a leaf, replace key with predecessor or successor if (node.children[i].numKeys >= 2) { // Find predecessor TreeNode pred = node.children[i]; while (!pred.isLeaf) { pred = pred.children[pred.numKeys]; } int predKey = pred.keys[pred.numKeys-1]; deleteHelper(pred, predKey); node.keys[i] = predKey; } else if (node.children[i+1].numKeys >= 2) { // Find successor TreeNode succ = node.children[i+1]; while (!succ.isLeaf) { succ = succ.children[0]; } int succKey = succ.keys[0]; deleteHelper(succ, succKey); node.keys[i] = succKey; } else { // Merge child nodes and delete key from merged node TreeNode left = node.children[i]; TreeNode right = node.children[i+1]; left.keys[1] = node.keys[i]; left.numKeys = 2; left.children[2] = right.children[0]; left.children[3] = right.children[1]; if (!left.isLeaf) { left.children[4] = right.children[2]; left.numKeys++; } for (int j = i; j < node.numKeys - 1; j++) { node.keys[j] = node.keys[j+1]; node.children[j+1] = node.children[j+2]; } node.numKeys--; deleteHelper(left, key); } } } else { // Key is not found in the node, recursively search child nodes if (node.isLeaf) { return; } else { deleteHelper(node.children[i], key); } } } // Public method to print the tree in level order public void printLevelOrder() { if (root == null) { return; } else { Queue queue = new LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i < size; i++) { TreeNode node = queue.poll(); System.out.print("["); for (int j = 0; j < node.numKeys; j++) { System.out.print(node.keys[j]); if (j < node.numKeys - 1) { System.out.print(", "); } } System.out.print("]"); if (!node.isLeaf) { for (int j = 0; j <= node.numKeys; j++) { queue.add(node.children[j]); } } if (i < size - 1) { System.out.print(", "); } } System.out.println(); } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
