You are on page 1of 10

public class IntBSTNode { protected int key; protected IntBSTNode left, right; public IntBSTNode() { left = right = null;

} public IntBSTNode(int el) { this(el,null,null); } public IntBSTNode(int el,IntBSTNode lt,IntBSTNode rt) { key=el; left = lt; right = rt; } public void visit() { System.out.print(key + " "); } } /////////NUEVA CLASE public class IntBST { protected IntBSTNode root; public IntBST() { root = null; } public IntBSTNode search(IntBSTNode p, int el) {} //figure 6.9 public void breadthFirst() {} //figura 6.10 public void preorder() { preorder(root); } protected void preorder(IntBSTNode p) {} //figura 6.11 public void inorder()

{ inorder(root); } protected void inorder(IntBSTNode p) {} //figura 6.11 public void postorder() { postorder(root); } protected void postorder(IntBSTNOde p) {} //figura 6.11 public public public public public public public void void boid void void void void iterativePreorder() {} //figura 6.15 iterativeInorder() { } //figura 6.17 iterativePostorder() { } //fig 6.16 MorrisInorder() { } //fig6.20 insert(int el) {} //fig 6.23 deleteByMerging(int el) { } fig 6.29 deleteByCopying(BaseObject el) { } //fig 6.32

public IntBSTNode search(IntBSTNode p, int el) { while (p!= null) if(el ==p.key) return p; else if (el<p.key) p=p.left; else p = p.right; return null; } public void breadthFirst() { IntBSTNode p = root; Queue queue = new Queue; if(p!= null) { queue.enqueue(p); while(!queue.isempty()) { p=(IntBSTNode)queue.dequeue(); p.visit; if(p.left != null) queue.enqueue(p.left); if(p.right != null) queue.enqueue(p.right); }

} } //figura 6.11 protected void preorder(IntBSTNode p) { if(p!=null) { p.visit(); preorder(p.left); preorder(p.right); } } protected void inorder(IntBSTNode p) { if(p!=null) { inorder(p.left); p.visit(); inorder(p.right); } } protected void postorder(IntBSTNode p) { if(p!=null) { postorder(p.left); postorder(p.right); p.visit(); } } //figura 6.15 public void iterativePreorder() { IntBSTNode p = root; Stack travStack = new Stack(); if(p!=null) { travStack.push(p); while(!travStack.isEmpty()) { p=(IntBSTNode)travStack.pop(); p.visit(); if(p.right!=null) travStack.ush(p.right); if(p.left !=null) travStack.push(p.left) } } }

//figura 6.16 public void iterativePostorder() { BSTNode p = root, q=root; Stack travStack = new Stack(); while(p != null) { for(; p.left !=null; p=p.left) travStack.push(p); while(p!=null && (p.right == null || p.right == q)) { p.visit(); q=p; if(travStack.isEmpty()) return; p=(BSTNode) travStack.pop(); } travStack.push(p); p=p.right; } } //figura 6.17 public void iterativeInorder() { IntBSTNode p = root; Stack travStack = new Stack(); while(p!=null) { while(p!=null) { if(p.right !=null) travStack.push(p.right); travStack.push(p); p=p.left; } p=(IntBSTNode) travStack.pop(); while(!travStack.isEmpty()&&p.right==null) { p=visit(); p=(IntBSTNode)travstack.pop(); } p.visit(); if(!travStack.isEmpty()) p=(IntBSTNode])travStack.pop(); else p=null; } }

//figure 6.19

class IntThreadedTreeNode { protected int key; protected boolean sucessor; protected IntThreadedTreeNode left, right; public IntThreadedTreeNode() { left = right = null; sucessor = false; } public IntThreadedTreeNode(int el) { this(el,null,null); } public IntThreadedTreeNode(int el, IntThreadedTreeNode lt, IntThreadedTreeNode rt) { key = el; left = lt; right = rt; sucessor = false; } public void visit() { System.out.print(key+" "); } } /// otra clase public class IntThreadedTree { private IntThreadedNode root; public IntThreadedTree() { root=null; } protected void threadedInorder() { IntThreadedNode prev, p = root; if(p!=null) { while(p.left != null) p=p.left; while (p! = null) { p.visit(); prev = p; p=p.right; if(p!=null && !prev.sucessor) while(p.left !=null) p=p.left; } } } public void threadedInsert(int el) {

//figura 6.24 } } // figura 20 public void MorrisInorder() { IntBSTNode p = root, tmp; while(p! = null) if(p.left == null) { p.visit(); p=p.right; } else { tmp=p.left; while(tmp.right !=null && tmp.right !=p) tmp=tmp.right; if(tmp.right == null) tmp.right = p; p=p.left; } else{ p.visit(); tmp.right=null; p=p.right; } } }

// figura 6.23 public void insert(int el) { IntBSTNode p = root, prev = null; while(p!=null) { prev = p; if(p.key<el) p=p.right; else p=p.left; } if(root = null) root= new IntBSTNode(el); else if (prev.key < el) prev.right = new IntBSTNode(el);

else prev.left = new IntBSTNode(el); }

//figura 6.24 public void threadedInsert(int el) { IntThreadedNode newNode = new IntThreadedNode(el); if(root = null) { root = newNode; return; } IntThreadedNode p = root, prev = null; while(p ! = null) { prev = p; if(el<p.key) p=p.left; else if(!p.hasSuccessor) p=p.right; else break; } if(el < prev.key) { prev.left = newNode; newNode.hasSuccessor = true; newNode.right = prev; } else if (prev.hasSuccessor) { newNode.hasSuccessor = true; prev.hasSuccessor = false; newNode.right=prev.right; prev.right =newNode; } else prev.right = newNode; } //figura 6.29 public void deleteByMerging(int el) { IntBSTNode tmp, node, p = root, prev = null; while(p!=null && p.key !=el) { prev = p; if(p.key < el ) p=p.right; else p = p.left; } node = p;

if(p != null && p.key == el) { if(node.right == null) node = node.left; else if (node.left == null) node = node.right; else { tmp = node.left; while(tmp.right != null) tmp = tmp.right; tmp.right = node.right; node = node.left; } if (p== root) root = node; else if (prev.left == p) prev.left = node; else prev.right = node; } else if (root != null) System.out.println("Key" + el + " no esta en el arbol "); else System.out.println ("the tree is empty"); }

//figura 6.32 public void deleteByCopying(BaseObject el) { BSTNode node, p = root, prev = null; while (p! = null && !p.key.equals(el)) { prev = p; if(p.key.isLessThan(el)) p=p.right; else p = p.left; } node = p; if(p!=null && p.key.equals(el)) { if(node.right == null) node = node.left; else if (node.left == null) node = node.right; else{ BSTNode tmp = node.left; BSTNode previous = node; while (tmp.right != null)

{ previous = tmp.right; } node.key = tmp.key; if(previous == node) previous.left = tmp.left; else previous.right = tmp.left; } if (p== root) root = node ; else if (prev.left==p) prev.left = node; else prev.right = node; } else if (root != null) System.out.println("key" + el.toString() + " No esta en el arbol"); else System.out.println("El arbol esta vacio"); }

// FIGURA 6.10

You might also like