public class ABin Tree implements Bin Tree private
public class ABin. Tree implements Bin. Tree{ private Object [] values; private Object EMPTY_NODE = new Object(); private int maxlength=100; private Int. Pos ROOT; public ABin. Tree() { values= new Object[maxlength]; ROOT = new Int. Pos(1); values[ROOT. ip]=EMPTY_NODE; } public ABin. Tree(int max) { maxlength =max; values= new Object[maxlength]; ROOT = new Int. Pos(1); values[ROOT. ip]=EMPTY_NODE; } public Pos insert. Left(Pos p) { values[((Int. Pos)p). ip*2]=EMPTY_NODE; return new Int. Pos(((Int. Pos)p). ip*2); } public Pos insert. Right(Pos p){ values[((Int. Pos)p). ip*2+1]=EMPTY_NODE; return new Int. Pos(((Int. Pos)p). ip*2+1); } public void set. Label(Object o, Pos p) { values[((Int. Pos)p). ip]=o; } public boolean is. Empty() { return (values[((Int. Pos)ROOT). ip] == EMPTY_NODE) && (values[((Int. Pos)ROOT). ip+1] == null) && (values[((Int. Pos)ROOT). ip+2] == null); } ABin. Tree. java
public boolean has. Left. Child(Pos p){ return values[((Int. Pos)p). ip*2] !=null; } public boolean has. Right. Child(Pos p){ return values[((Int. Pos)p). ip*2+1] !=null; } public boolean has. Label(Pos p) { return values[((Int. Pos)p). ip] != EMPTY_NODE; } public Object inspect. Label(Pos p){ return values[((Int. Pos)p). ip]; } public Pos root () { return ((Pos)ROOT); } public Pos left. Child(Pos p){ return new Int. Pos(((Int. Pos)p). ip*2); } public Pos right. Child(Pos p){ return new Int. Pos(((Int. Pos)p). ip*2+1); } public Pos parent(Pos p){ return new Int. Pos(((Int. Pos)p). ip/2); } public Pos delete. Node(Pos p) { //Must be a leaf and can not be the root values[((Int. Pos)p). ip]=null; return parent(p); } ABin. Tree. java
public void print. Out(){ if (!is. Empty()) print. Out(root()); } private void print. Out(Pos p){ String str; if (has. Label(p)){ str = "rot: " + inspect. Label(p); }else{ str = "TOM"; } System. out. println(str); if (has. Left. Child(p)){ System. out. println("Vänster barn till " + str); print. Out(left. Child(p)); } if (has. Right. Child(p)){ System. out. println("Höger barn till " + str); print. Out(right. Child(p)); } } protected class Int. Pos implements Pos { int ip; private Int. Pos (int ip) { this. ip =ip; } public boolean eq(Pos p) { return (((Int. Pos)p). ip == this. ip; } }// Int. Pos }//ABin. Tree. java
public class OTree implements Ordered. Tree{ protected Node root; public OTree() { root = new Node(null, null); } public Pos insert. First. Child (Pos p){ Node el; el = new Node((Node)p, null, null); ((Node)p). firstchild=el; return el; } public Pos root() { return root; } public Pos parent(Pos p) { return ((Node)p). parent; } public Pos first. Child(Pos p) { return ((Node)p). firstchild; } public Pos next. Sibling(Pos p) { return ((Node)p). nextsibling; } public void set. Label(Object o, Pos p){ ((Node)p). label=o; } public Object inspect. Label(Pos p) { return ((Node)p). label; } OTree. java
public boolean has. Label(Pos p) { return ((Node)p). label != null; } public Pos insert. Next. Sibling(Pos p){ Node el; el = new Node(((Node)p). parent, null, (Node)p, ((Node)p). nextsibling); ((Node)p). nextsibling = el; return el; } public Pos delete. Node(Pos p){ ((Node)p). prevsibling. nextsibling = ((Node)p). nextsibling; return ((Node)p). nextsibling; } public boolean is. Root(Pos p) { return root. eq(p); } public boolean is. Leaf(Pos p) { return ((Node)p). firstchild == null; } public boolean is. Last. Sibling(Pos p) { return ((Node)p). nextsibling ==null; } public boolean is. Empty(){ return (root. firstchild == null) && (root. label == null); } OTree. java
public void print. Out(){ if (!is. Empty()){ print. Out(root()); } } private void print. Out(Pos p){ if (has. Label(p)) System. out. println(inspect. Label(p)); else System. out. println("TOM"); if (!is. Leaf(p)){ System. out. println("->"); Pos p 2 = first. Child(p); print. Out(p 2); while (!is. Last. Sibling(p 2)){ p 2 = next. Sibling(p 2); print. Out(p 2); } System. out. println("<-"); } } public static void main(String [] args){ OTree tree = new OTree(); Pos p = tree. root(); tree. set. Label(new Integer(1), p); Pos p 2 = tree. insert. First. Child(p); tree. set. Label(new Integer(2), p 2); Pos p 3 = tree. insert. Next. Sibling(p 2); tree. set. Label(new Integer(3), p 3); p = p 2; p 2 = tree. insert. First. Child(p); tree. set. Label(new Integer(4), p 2); p 3 = tree. insert. Next. Sibling(p 2); tree. set. Label(new Integer(5), p 3); tree. print. Out(); } OTree. java
protected class Node implements Pos { protected Object label; protected Node nextsibling; prevsibling; parent; 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
public interface Ordered. Tree { public Pos insert. First. Child (Pos p); public Pos insert. Next. Sibling (Pos p); public Pos root(); public Pos parent(Pos p); public Pos first. Child(Pos p); public Pos next. Sibling(Pos p); void set. Label(Object o, Pos p); public Object inspect. Label(Pos p); public boolean has. Label(Pos p); public Pos delete. Node(Pos p); } Ordered. Tree. java public interface Bin. Tree { public Pos insert. Left(Pos p); public Pos insert. Right(Pos p); public void set. Label(Object o, Pos p); public boolean is. Empty(); public boolean has. Left. Child(Pos p); public boolean has. Right. Child(Pos p); public boolean has. Label(Pos p); public Object inspect. Label (Pos p); public Pos root (); public Pos left. Child(Pos p); public Pos right. Child(Pos p); public Pos parent(Pos p); public Pos delete. Node(Pos p); } Bin. Tree. java
- Slides: 8