Appendix: Java Codes For Dnode, Dlist, Bnode, Binarytree and Hashmap For Reference
Appendix: Java Codes For Dnode, Dlist, Bnode, Binarytree and Hashmap For Reference
Java codes for DNode, DList, BNode, BinaryTree and HashMap for
reference
class BNode{
private Object element;
private BNode parent,leftChild,rightChild;
public BNode(){
this(null,null,null,null);
}
/** Creates a node with the given element, parent,left child and right child. */
public BNode(Object ele, BNode p, BNode left, BNode right){
parent=p;
element=ele;
leftChild=left;
rightChild=right;
}
/**Accessor methods: */
public Object getElement(){
return element;
}
public BNode getParent(){
return parent;
}
public BNode getLeftChild(){
return leftChild;
}
public BNode getRightChild(){
return rightChild;
}
/**Modifier methods: */
public void setElement(Object ele){
element=ele;
}
public void setParent(BNode p){
parent=p;
}
public void setLeftChild(BNode left){
leftChild=left;
}
public void setRightChild(BNode right){
rightChild=right;
}
}
public class BinaryTree {
class DNode {
private DNode prev;
private Object element;
private DNode next;
/** Creates a node with null references to its element and next node. */
public DNode() {
this(null, null, null);
}
/** Creates a node with the given element and next node. */
public DNode(DNode p, Object e, DNode n) {
prev = p;
element = e;
next = n;
}
// Accessor methods:
public Object getElement() {
return element;
}
public DNode getNext() {
return next;
}
public DNode getPrev() {
return prev;
}
// Modifier methods:
public void setElement(Object newElem) {
element = newElem;
}
public void setNext(DNode newNext) {
next = newNext;
}
dd=test.tailer.getPrev();
test.remove(dd);
System.out.println("remove the second last, The list is "+test.toString());
dd=test.tailer;
test.remove(dd);
System.out.println("REmove the last The list is "+test.toString());
dd=test.header;
test.remove(dd);
System.out.println("REmove the header The list is "+test.toString());
}
protected int numElts; // Number of elements in the list
protected DNode header, tailer; // Special sentinels
/** Constructor that creates an empty list; O(1) time */
public DList() {
numElts = 0;
header = null; // create header
tailer = null;// create tailer
//header.setNext(tailer); // make header and tailer point to each other
}
/** Insert the given element after the given position, returning the
* new position; O(1) time */
public DNode insertAfter(DNode p, Object element) {
numElts++;
DNode newNode = new DNode(p, element, p.getNext());
if (p.getNext()!=null) {p.getNext().setPrev(newNode);}
else {tailer =newNode;}
p.setNext(newNode);
numElts++;
return newNode;
}
/** Insert the given element at the beginning of the list, returning
* the new position; O(1) time */
public DNode insertFirst(Object element) {
numElts++;
DNode newNode = new DNode(null, element, header);
if (header == null) {
header=newNode;
tailer=newNode;
}
else {header.setPrev(newNode); header=newNode;}
numElts++;
return newNode;
}
/** Insert the given element at the end of the list, returning
* the new position; O(1) time */
while (p !=null) {
s += p.getElement();
if (p.getNext()!=null) {
s += ", ";
}
p = p.getNext();
}
s += "]";
return s;
}
}
}
return false;
}