// Source code example for "A Practical Introduction // to Data Structures and Algorithm Analysis" // by Clifford A. Shaffer, Prentice Hall, 1998. // Copyright 1998 by Clifford A. Shaffer class TTNode { // 2-3 tree node implementation private Elem lkey; // The node's left key private Elem rkey; // The node's right key private int numKeys; // Number of key values stored private TTNode left; // Pointer to left child private TTNode center; // Pointer to middle child private TTNode right; // Pointer to right child public TTNode() { center = left = right = null; numKeys = 0; } public TTNode(Elem l, Elem r, TTNode p1, TTNode p2, TTNode p3) { lkey = l; rkey = r; if (r == null) numKeys = 1; else numKeys = 2; left = p1; center = p2; right = p3; } // Add key and subtree to internal node public void addKey(Elem val, TTNode child) { Assert.notFalse(numKeys==1, "Illegal addKey"); numKeys = 2; if (val.key() < lkey.key()) { rkey = lkey; lkey = val; right = center; center = child; } else { rkey = val; right = child; } } public int numKeys() { return numKeys; } public TTNode lchild() { return left; } public TTNode rchild() { return right; } public TTNode cchild() { return center; } public TTNode setCenter(TTNode val) { return center = val; } public Elem lkey() { return lkey; } // Left key value public Elem rkey() { return rkey; } // Right key value public boolean isLeaf() { return left == null; } public void setLkey(Elem val) { // Change left key Assert.notFalse(val!=null, "Can't nullify left value"); lkey = val; } public void setRkey(Elem val) { // Change right key Assert.notFalse(numKeys!=0,"Can't set right key of empty node"); if ((val == null)&&(numKeys == 2)) // Clear subtree {right = null; numKeys = 1;} rkey = val; } public String toString() { if (numKeys==1) return lkey.toString(); else return lkey.toString() + " | " + rkey.toString(); } } // class TTNode