0% found this document useful (0 votes)
8 views8 pages

Appendix: Java Codes For Dnode, Dlist, Bnode, Binarytree and Hashmap For Reference

The document contains Java code for various data structures including a binary tree (BinaryTree and BNode classes), a doubly linked list (DList and DNode classes), and a simple HashMap implementation (pairNum class). It provides methods for adding, removing, and accessing elements in these data structures. The main methods demonstrate the functionality of these structures with sample data.

Uploaded by

selina030328
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Appendix: Java Codes For Dnode, Dlist, Bnode, Binarytree and Hashmap For Reference

The document contains Java code for various data structures including a binary tree (BinaryTree and BNode classes), a doubly linked list (DList and DNode classes), and a simple HashMap implementation (pairNum class). It provides methods for adding, removing, and accessing elements in these data structures. The main methods demonstrate the functionality of these structures with sample data.

Uploaded by

selina030328
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Appendix:

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 {

protected BNode root;


protected int size;

/**Creates an empty binary tree. */


public BinaryTree(){
root=null;
size=0;
}

/**Returns the number of nodes in the tree. */


public int size(){
return size;
}

/**Returns whether the tree has any nodes or not. */


public boolean isEmpty(){
return (size==0);
}

/**Returns the root of the tree. */


public BNode root(){
return root;
}

/**Returns the parent of a given node. */


public BNode parent(BNode p){
return p.getParent();
}

/**Returns the left child of a given node. */


public BNode leftChild(BNode p){
return p.getLeftChild();
}

/**Returns the right child of a given node. */


public BNode rightChild(BNode p){
return p.getRightChild();
}

/**Returns the object stored in a given node. */


public Object element(BNode p){
return p.getElement();
}
/**Returns whether a node has a left child. */
public boolean hasLeft(BNode p){
return p.getLeftChild()!=null;
}

/**Returns whether a node has a right child. */


public boolean hasRight(BNode p){
return p.getRightChild()!=null;
}

/**Adds a root node to an empty tree. */


public BNode addRoot(Object ele){
if(!isEmpty()){
System.out.println("Tree already has a root.");
return null;
}
size=1;
root=new BNode(ele,null,null,null);
return root;
}

/**Inserts a left child at a given node. */


public BNode insertLeft(BNode p,Object ele){
if(hasLeft(p)){
System.out.println("Node already has a left child.");
return null;
}
BNode left=new BNode(ele,p,null,null);
p.setLeftChild(left);
size++;
return left;
}

/**Inserts a right child at a given node. */


public BNode insertRight(BNode p,Object ele){
if(hasRight(p)){
System.out.println("Node already has a right child.");
return null;
}
BNode right=new BNode(ele,p,null,null);
p.setRightChild(right);
size++;
return right;
}

/**delete a leaf node of the binary tree*/


public Object deleteLeaf(BNode p){
if(!(hasLeft(p)==false && hasRight(p)==false)){
System.out.println("The node is not a leaf");
return null;
}
BNode parent=parent(p);
if(p==leftChild(parent)){
parent.setLeftChild(null);
}
else if(p==rightChild(parent)){
parent.setRightChild(null);
}
size--;
return p.getElement();
}
public static void main(String[] args) {
/* generate a BinaryTree bt with 9 nodes */
BinaryTree bt = new BinaryTree();
BNode root = bt.addRoot("6");
BNode two = bt.insertLeft(root, "2");
System.out.println("The root is :"+bt.root().getElement());
System.out.println("The left child of root is :"+
(bt.leftChild(bt.root())).getElement());
System.out.println("The right child of root is :"+(bt.rightChild(bt.root())));

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;
}

public void setPrev(DNode newPrev) {


prev = newPrev;
}
}

public class DList


{
public static void main(String args[])
{
DList test = new DList();
test.insertFirst((Integer) 5);
test.insertFirst((Integer) 4);
test.insertFirst((Integer) 3);

System.out.println("After insert 3 nodes The list is "+test.toString());


DNode dd=(test.header).getNext();// dd is 4
test.insertAfter(dd, (Integer) 9);
System.out.println("After insert 9 after 4 The list is "+test.toString());
dd=test.tailer;
test.insertAfter(dd, (Integer) 10);
System.out.println("After insert 10 after tailer The list is "+test.toString());
dd=test.header;
test.insertAfter(dd, (Integer) 11);
System.out.println("After insert 11 after header The list is "+test.toString());

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
}

/** Returns the number of elements in the list; O(1) time */


public int size() { return numElts; }
/** Returns whether the list is empty; O(1) time */
public boolean isEmpty() { return (numElts == 0); }
/** Returns the first position in the list; O(1) time */

/** 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 */

/**Remove the given position from the list; O(1) time */


public Object remove(DNode p) {
if (numElts==0) {System.out.println("empty list"); return null;}
if (p==null) {System.out.println("THe node is empty"); return null;}
numElts--;
DNode pPrev = p.getPrev();
DNode pNext = p.getNext();

Object pElem = p.getElement();


if(pPrev != null) {pPrev.setNext(pNext);}
else { header=pNext; }

if (pNext != null) pNext.setPrev(pPrev);


else tailer=p.getPrev();

// unlink the position from the list and make it invalid


p.setNext(null);
p.setPrev(null);
return pElem;
}

/** Returns a textual representation of the list */


public String toString() {
String s = "[";
if (numElts==0) {s="[]"; return s;}
DNode p = header;

while (p !=null) {
s += p.getElement();

if (p.getNext()!=null) {
s += ", ";
}
p = p.getNext();

}
s += "]";
return s;
}
}

//******** HashMap. **************


public class pairNum {
public static boolean problem(int[] a, int[] b, int z) {
// create the hasMap

HashMap<Integer, String> h = new HashMap<Integer, String>();


// put all numbers in array b into a hashMap
for(int i=0; i<b.length; i++) {
h.put(b[i], "a");
}
for (int i=0; i <a.length;i++) {
int y=z-a[i];
if (h.containsKey(y)) { System.out.println("a[i] is"+a[i]+"b[j] is"+y);
return true;}

}
return false;
}

public static void main(String[] args) {


int [] a= {1, 2, 3, 4, 5, 6};
int [] b= {8,9, 10, 11, 12, 13};
System.out.println("The out is:"+problem(a, b, 14));

You might also like