// 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 // Implementation of General Tree Nodes: // Left Child/Right Sibling implementation (with parent pointer) public class GTNodeImpl implements GTNode { private GTNode rent; private GTNode leftchild; private GTNode rightsib; private Object element; public GTNodeImpl(Object value) { // constructor with no context rent = leftchild = rightsib = null; element = value; } // constructor with opportunity to link into tree context public GTNodeImpl(Object value, GTNode par, GTNode leftc, GTNode rights) { element = value; rent = par; leftchild = leftc; rightsib = rights; } // Return the value public Object value() { return element; } // TRUE if this node is a leaf public boolean isLeaf() { return leftchild == null; } // Return the parent public GTNode parent() { return rent; } // Return the leftmost child public GTNode leftmost_child() { return leftchild; } // Return the right sibling public GTNode right_sibling() { return rightsib; } // Set the value public void setValue(Object value) { element = value; } // Set the parent public void setParent(GTNode par) {rent = par; } // Add a new leftmost child public void insert_first(GTNode n) { ((GTNodeImpl)n).rent = this; ((GTNodeImpl)n).rightsib = leftchild; leftchild = n; } // Insert a new right sibling public void insert_next(GTNode n) { ((GTNodeImpl)n).rent = rent; ((GTNodeImpl)n).rightsib = rightsib; rightsib = n; } // Remove the leftmost child public void remove_first() { if (leftchild == null) return; leftchild = ((GTNodeImpl)leftchild).rightsib; } // Remove the right sibling public void remove_next() { if (rightsib == null) return; rightsib = ((GTNodeImpl)rightsib).rightsib; } }