Presentation is loading. Please wait.

Presentation is loading. Please wait.

A.A. 06-07DA1: Binary Tree1 Binary Tree. A.A. 06-07DA1: Binary Tree2 Implementazione usa Tree LinkedBinaryTree BinaryTree NodePositionList PositionList.

Similar presentations


Presentation on theme: "A.A. 06-07DA1: Binary Tree1 Binary Tree. A.A. 06-07DA1: Binary Tree2 Implementazione usa Tree LinkedBinaryTree BinaryTree NodePositionList PositionList."— Presentation transcript:

1 A.A. 06-07DA1: Binary Tree1 Binary Tree

2 A.A. 06-07DA1: Binary Tree2 Implementazione usa Tree LinkedBinaryTree BinaryTree NodePositionList PositionList Position BTPosition Position implements extends implements usa implements I C C I I I C I

3 A.A. 06-07DA1: Binary Tree3 Interfaccia BinaryTree /** * An interface for a binary tree, where each node can have zero, one, * or two children. */ public interface BinaryTree extends Tree { /** Returns the left child of a node. */ public Position left(Position v) ; /** Returns the right child of a node. */ public Position right(Position v) ; /** Returns whether a node has a left child. */ public boolean hasLeft(Position v); /** Returns whether a node has a right child. */ public boolean hasRight(Position v); }

4 A.A. 06-07DA1: Binary Tree4 Interfaccia BTPosition /** * Interface for a node of a binary tree. It maintains an element, a * parent node, a left node, and a right node. */ public interface BTPosition extends Position { // inherits element() public void setElement(E o); public BTPosition getLeft(); public void setLeft(BTPosition v); public BTPosition getRight(); public void setRight(BTPosition v); public BTPosition getParent(); public void setParent(BTPosition v); }

5 A.A. 06-07DA1: Binary Tree5 Classe BTNode /** * Class implementing a node of a binary tree by storing references to * an element, a parent node, a left node, and a right node. */ public class BTNode implements BTPosition { private E element; // element stored at this node private BTPosition left, right, parent; // adjacent nodes /** Default constructor */ public BTNode() { } /** Main constructor */ public BTNode(E element, BTPosition parent, BTPosition left, BTPosition right) { setElement(element); setParent(parent); setLeft(left); setRight(right); }

6 A.A. 06-07DA1: Binary Tree6 Classe BTNode (2) /** Returns the element stored at this position */ public E element() { return element; } /** Sets the element stored at this position */ public void setElement(E o) { element=o; } /** Returns the left child of this position */ public BTPosition getLeft() { return left; } /** Sets the left child of this position */ public void setLeft(BTPosition v) { left=v; } /** Returns the right child of this position */ public BTPosition getRight() { return right; } /** Sets the right child of this position */ public void setRight(BTPosition v) { right=v; } /** Returns the parent of this position */ public BTPosition getParent() { return parent; } /** Sets the parent of this position */ public void setParent(BTPosition v) { parent=v; } }

7 A.A. 06-07DA1: Binary Tree7 Classe LinkedBinaryTree import java.util.Iterator; /** * An implementation of the BinaryTree interface by means of a linked * structure. */ public class LinkedBinaryTree implements BinaryTree { protected BTPosition root; // reference to the root protected int size; // number of nodes /** Creates an empty binary tree. */ public LinkedBinaryTree() { root = null; // start with an empty tree size = 0; }

8 A.A. 06-07DA1: Binary Tree8 Classe LinkedBinaryTree : metodi dell'interfaccia Tree public int size() {return size;} public boolean isEmpty() {return (size == 0);} /** Returns whether a node is internal. */ public boolean isInternal(Position v) { checkPosition(v); // auxiliary method return (hasLeft(v) || hasRight(v)); } /** Returns whether a node is external. */ public boolean isExternal(Position v) {return !isInternal(v); } /** Returns whether a node is the root. */ public boolean isRoot(Position v) { checkPosition(v); return (v == root()); }

9 A.A. 06-07DA1: Binary Tree9 Classe LinkedBinaryTree : metodi dell'interfaccia Tree (2) /** Returns an iterable collection of the tree nodes. */ public Iterable > positions() { PositionList > positions = new NodePositionList >(); if(size != 0) preorderPositions(root(), positions); // assign positions in preorder return positions; } /** Returns an iterator of the elements stored at the nodes */ public Iterator iterator() { Iterable > positions = positions(); PositionList elements = new NodePositionList (); for (Position pos: positions) elements.addLast(pos.element()); return elements.iterator(); // An iterator of elements }

10 A.A. 06-07DA1: Binary Tree10 Classe LinkedBinaryTree : metodi dell'interfaccia Tree (3) /** Returns the parent of a node. */ public Position parent(Position v) { BTPosition vv = checkPosition(v); Position parentPos = vv.getParent(); if (parentPos == null) return null; return parentPos; } /** Returns an iterable collection of the children of a node. */ public Iterable > children(Position v) { PositionList > children = new NodePositionList >(); if (hasLeft(v)) children.addLast(left(v)); if (hasRight(v)) children.addLast(right(v)); return children; }

11 A.A. 06-07DA1: Binary Tree11 Classe LinkedBinaryTree : metodi dell'interfaccia Tree (4) /** Returns the root of the tree.*/ public Position root(){ if (isEmpty()) return null; return root; } /** Replaces the element at a node. */ public E replace(Position v, E o) { BTPosition vv = checkPosition(v); E temp = v.element(); vv.setElement(o); return temp; }

12 A.A. 06-07DA1: Binary Tree12 Classe LinkedBinaryTree : metodi dell'interfaccia BinaryTree /** Returns whether a node has a left child. */ public boolean hasLeft(Position v){ BTPosition vv = checkPosition(v); return (vv.getLeft() != null); } /** Returns whether a node has a right child. */ public boolean hasRight(Position v){ BTPosition vv = checkPosition(v); return (vv.getRight() != null); }

13 A.A. 06-07DA1: Binary Tree13 Classe LinkedBinaryTree : metodi dell'interfaccia BinaryTree (2) /** Returns the left child of a node. */ public Position left(Position v) { BTPosition vv = checkPosition(v); Position leftPos = vv.getLeft(); if (leftPos == null) return null; return leftPos; } /** Returns the right child of a node. */ public Position right(Position v) { BTPosition vv = checkPosition(v); Position rightPos = vv.getRight(); if (rightPos == null) return null; return rightPos; }

14 A.A. 06-07DA1: Binary Tree14 Classe LinkedBinaryTree /** Inserts a left child at a given node. */ public Position insertLeft(Position v, E e){ BTPosition vv = checkPosition(v); Position leftPos = vv.getLeft(); if (leftPos != null) return null; BTPosition ww = createNode(e, vv, null, null); vv.setLeft(ww); size++; return ww;} /** Inserts a right child at a given node. */ public Position insertRight(Position v, E e){ BTPosition vv = checkPosition(v); Position rightPos = vv.getRight(); if (rightPos != null) return null; BTPosition w = createNode(e, vv, null, null); vv.setRight(w); size++; return w;}

15 A.A. 06-07DA1: Binary Tree15 Classe LinkedBinaryTree /** Removes a node with zero or one child. */ public E remove(Position v){ BTPosition vv = checkPosition(v); BTPosition leftPos = vv.getLeft(); BTPosition rightPos = vv.getRight(); if (leftPos != null && rightPos != null) return null; BTPosition ww; // the only child of v, if any if (leftPos != null) ww = leftPos; else if (rightPos != null) ww = rightPos; else ww = null; // v is a leaf if (vv == root) { // v is the root if (ww != null) ww.setParent(null); root = ww; }...

16 A.A. 06-07DA1: Binary Tree16 Classe LinkedBinaryTree... // continue public E remove(Position v) else { // v is not the root BTPosition uu = vv.getParent(); if (vv == uu.getLeft()) uu.setLeft(ww); else uu.setRight(ww); if(ww != null) ww.setParent(uu); } size--; return v.element(); } /** Adds a root node to an empty tree */ public Position addRoot(E e) { if(!isEmpty()) return null; size = 1; root = createNode(e,null,null,null); return root; }

17 A.A. 06-07DA1: Binary Tree17 Classe LinkedBinaryTree /** Attaches two trees to be subtrees of an external node. */ public void attach(Position v, BinaryTree T1, BinaryTree T2) { BTPosition vv = checkPosition(v); if (!T1.isEmpty()) { BTPosition r1 = checkPosition(T1.root()); vv.setLeft(r1); r1.setParent(vv); // T1 should be invalidated } if (!T2.isEmpty()) { BTPosition r2 = checkPosition(T2.root()); vv.setRight(r2); r2.setParent(vv); // T2 should be invalidated }

18 A.A. 06-07DA1: Binary Tree18 Classe LinkedBinaryTree /** Return the sibling of a node */ public Position sibling(Position v) { BTPosition vv = checkPosition(v); BTPosition parentPos = vv.getParent(); if (parentPos != null) { BTPosition sibPos; BTPosition leftPos = parentPos.getLeft(); if (leftPos == vv) sibPos = parentPos.getRight(); else sibPos = parentPos.getLeft(); if (sibPos != null) return sibPos;} return null;}

19 A.A. 06-07DA1: Binary Tree19 Classe LinkedBinaryTree /** Swap the elements at two nodes */ public void swapElements(Position v, Position w){ BTPosition vv = checkPosition(v); BTPosition ww = checkPosition(w); E temp = w.element(); ww.setElement(v.element()); vv.setElement(temp); } /** Expand an external node into an internal node with two external * node children */ public void expandExternal(Position v, E l, E r) { insertLeft(v, l); insertRight(v, r); }

20 A.A. 06-07DA1: Binary Tree20 Classe LinkedBinaryTree /** If v is a good binary tree node, cast to BTPosition, else throw exception */ protected BTPosition checkPosition(Position v) { if (v == null || !(v instanceof BTPosition)) return null; return (BTPosition ) v; } /** Creates a new binary tree node */ protected BTPosition createNode(E element, BTPosition parent, BTPosition left, BTPosition right) { return new BTNode (element,parent,left,right); }

21 A.A. 06-07DA1: Binary Tree21 Classe LinkedBinaryTree /** Creates a list storing the the nodes in the subtree of a node, * ordered according to the preorder traversal of the subtree. */ protected void preorderPositions(Position v, PositionList > pos){ pos.addLast(v); if (hasLeft(v)) preorderPositions(left(v), pos); // recurse on left child if (hasRight(v)) preorderPositions(right(v), pos); // recurse on right child } /** Creates a list storing the the nodes in the subtree of a node, * ordered according to the inorder traversal of the subtree. */ protected void inorderPositions(Position v, PositionList > pos){ if (hasLeft(v)) inorderPositions(left(v), pos); // recurse on left child pos.addLast(v); if (hasRight(v)) inorderPositions(right(v), pos); // recurse on right child }


Download ppt "A.A. 06-07DA1: Binary Tree1 Binary Tree. A.A. 06-07DA1: Binary Tree2 Implementazione usa Tree LinkedBinaryTree BinaryTree NodePositionList PositionList."

Similar presentations


Ads by Google