1 / 8

public class ABinTree implements BinTree{ private Object [] values;

public class ABinTree implements BinTree{ private Object [] values; private Object EMPTY_NODE = new Object(); private int maxlength=100; private IntPos ROOT; public ABinTree() { values= new Object[maxlength]; ROOT = new IntPos(1);

hhoagland
Download Presentation

public class ABinTree implements BinTree{ private Object [] values;

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. public class ABinTree implements BinTree{ private Object [] values; private Object EMPTY_NODE = new Object(); private int maxlength=100; private IntPos ROOT; public ABinTree() { values= new Object[maxlength]; ROOT = new IntPos(1); values[ROOT.ip]=EMPTY_NODE; } public ABinTree(int max) { maxlength =max; values= new Object[maxlength]; ROOT = new IntPos(1); values[ROOT.ip]=EMPTY_NODE; } public Pos insertLeft(Pos p) { values[((IntPos)p).ip*2]=EMPTY_NODE; return new IntPos(((IntPos)p).ip*2); } public Pos insertRight(Pos p){ values[((IntPos)p).ip*2+1]=EMPTY_NODE; return new IntPos(((IntPos)p).ip*2+1); } public void setLabel(Object o,Pos p) { values[((IntPos)p).ip]=o; } public boolean isEmpty() { return (values[((IntPos)ROOT).ip] == EMPTY_NODE) && (values[((IntPos)ROOT).ip+1] == null) && (values[((IntPos)ROOT).ip+2] == null); } ABinTree.java

  2. public boolean hasLeftChild(Pos p){ return values[((IntPos)p).ip*2] !=null; } public boolean hasRightChild(Pos p){ return values[((IntPos)p).ip*2+1] !=null; } public boolean hasLabel(Pos p) { return values[((IntPos)p).ip] != EMPTY_NODE; } public Object inspectLabel(Pos p){ return values[((IntPos)p).ip]; } public Pos root () { return ((Pos)ROOT); } public Pos leftChild(Pos p){ return new IntPos(((IntPos)p).ip*2); } public Pos rightChild(Pos p){ return new IntPos(((IntPos)p).ip*2+1); } public Pos parent(Pos p){ return new IntPos(((IntPos)p).ip/2); } public Pos deleteNode(Pos p) { //Must be a leaf and can not be the root values[((IntPos)p).ip]=null; return parent(p); } ABinTree.java

  3. public void printOut(){ if (!isEmpty()) printOut(root()); } private void printOut(Pos p){ String str; if (hasLabel(p)){ str = "rot: " + inspectLabel(p); }else{ str = "TOM"; } System.out.println(str); if (hasLeftChild(p)){ System.out.println("Vänster barn till " + str); printOut(leftChild(p)); } if (hasRightChild(p)){ System.out.println("Höger barn till " + str); printOut(rightChild(p)); } } protected class IntPos implements Pos { int ip; private IntPos (int ip) { this.ip =ip; } public boolean eq(Pos p) { return (((IntPos)p).ip == this.ip; } }// IntPos }//ABinTree ABinTree.java

  4. public class OTree implements OrderedTree{ protected Node root; public OTree() { root = new Node(null,null,null,null); } public Pos insertFirstChild (Pos p){ Node el; el = new Node((Node)p,null,null,null); ((Node)p).firstchild=el; return el; } public Pos root() { return root; } public Pos parent(Pos p) { return ((Node)p).parent; } public Pos firstChild(Pos p) { return ((Node)p).firstchild; } public Pos nextSibling(Pos p) { return ((Node)p).nextsibling; } public void setLabel(Object o,Pos p){ ((Node)p).label=o; } public Object inspectLabel(Pos p) { return ((Node)p).label; } OTree.java

  5. public boolean hasLabel(Pos p) { return ((Node)p).label != null; } public Pos insertNextSibling(Pos p){ Node el; el = new Node(((Node)p).parent,null,(Node)p, ((Node)p).nextsibling); ((Node)p).nextsibling = el; return el; } public Pos deleteNode(Pos p){ ((Node)p).prevsibling.nextsibling = ((Node)p).nextsibling; return ((Node)p).nextsibling; } public boolean isRoot(Pos p) { return root.eq(p); } public boolean isLeaf(Pos p) { return ((Node)p).firstchild == null; } public boolean isLastSibling(Pos p) { return ((Node)p).nextsibling ==null; } public boolean isEmpty(){ return (root.firstchild == null) && (root.label == null); } OTree.java

  6. public void printOut(){ if (!isEmpty()){ printOut(root()); } } private void printOut(Pos p){ if (hasLabel(p)) System.out.println(inspectLabel(p)); else System.out.println("TOM"); if (!isLeaf(p)){ System.out.println("->"); Pos p2 = firstChild(p); printOut(p2); while (!isLastSibling(p2)){ p2 = nextSibling(p2); printOut(p2); } System.out.println("<-"); } } public static void main(String [] args){ OTree tree = new OTree(); Pos p = tree.root(); tree.setLabel(new Integer(1), p); Pos p2 = tree.insertFirstChild(p); tree.setLabel(new Integer(2), p2); Pos p3 = tree.insertNextSibling(p2); tree.setLabel(new Integer(3), p3); p = p2; p2 = tree.insertFirstChild(p); tree.setLabel(new Integer(4), p2); p3 = tree.insertNextSibling(p2); tree.setLabel(new Integer(5), p3); tree.printOut(); } OTree.java

  7. protected class Node implements Pos { protected Object label; protected Node nextsibling; protected Node prevsibling; protected Node parent; protected Node firstchild; protected Node(Node parent,Node firstchild, Node prev, Node next) { this.nextsibling = next; this.prevsibling= prev; this.parent=parent; this.firstchild=firstchild; } public boolean eq(Pos p) { return (((Node)p).nextsibling == this.nextsibling) && (((Node)p).prevsibling == this.prevsibling) && (((Node)p).parent == this.parent) && (((Node)p).firstchild == this.firstchild); } }// class Node }// class Otree OTree.java

  8. public interface OrderedTree { public Pos insertFirstChild (Pos p); public Pos insertNextSibling (Pos p); public Pos root(); public Pos parent(Pos p); public Pos firstChild(Pos p); public Pos nextSibling(Pos p); void setLabel(Object o,Pos p); public Object inspectLabel(Pos p); public boolean hasLabel(Pos p); public Pos deleteNode(Pos p); } OrderedTree.java public interface BinTree { public Pos insertLeft(Pos p); public Pos insertRight(Pos p); public void setLabel(Object o,Pos p); public boolean isEmpty(); public boolean hasLeftChild(Pos p); public boolean hasRightChild(Pos p); public boolean hasLabel(Pos p); public Object inspectLabel (Pos p); public Pos root (); public Pos leftChild(Pos p); public Pos rightChild(Pos p); public Pos parent(Pos p); public Pos deleteNode(Pos p); } BinTree.java

More Related