a.a. 06-07da1: binary tree1 binary tree. a.a. 06-07da1: binary tree2 implementazione usa tree...

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

Upload: gabrielle-hood

Post on 27-Mar-2015

216 views

Category:

Documents


3 download

TRANSCRIPT

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

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

Binary Tree

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

A.A. 06-07 DA1: Binary Tree 2

Implementazione

usa

Tree<E>

LinkedBinaryTree<E>

BinaryTree<E>

NodePositionList<E>

PositionList<E>

Position<E>

BTPosition<E>

Position<E>

implements

extends extends

implements

usa

implements

I

C

C

I

I

I

C

I

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

A.A. 06-07 DA1: Binary Tree 3

Interfaccia BinaryTree<E>

/** * An interface for a binary tree, where each node can have zero, one, * or two children. */public interface BinaryTree<E> extends Tree<E> { /** Returns the left child of a node. */ public Position<E> left(Position<E> v) ; /** Returns the right child of a node. */ public Position<E> right(Position<E> v) ; /** Returns whether a node has a left child. */ public boolean hasLeft(Position<E> v); /** Returns whether a node has a right child. */ public boolean hasRight(Position<E> v);}

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

A.A. 06-07 DA1: Binary Tree 4

Interfaccia BTPosition<E>

/** * 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<E> extends Position<E> { // inherits element() public void setElement(E o); public BTPosition<E> getLeft(); public void setLeft(BTPosition<E> v); public BTPosition<E> getRight(); public void setRight(BTPosition<E> v); public BTPosition<E> getParent(); public void setParent(BTPosition<E> v);}

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

A.A. 06-07 DA1: Binary Tree 5

Classe BTNode<E>

/** * 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<E> implements BTPosition<E> { private E element; // element stored at this node private BTPosition<E> left, right, parent; // adjacent nodes /** Default constructor */ public BTNode() { } /** Main constructor */ public BTNode(E element, BTPosition<E> parent, BTPosition<E> left, BTPosition<E> right) { setElement(element); setParent(parent); setLeft(left); setRight(right); }

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

A.A. 06-07 DA1: Binary Tree 6

Classe BTNode<E> (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<E> getLeft() { return left; } /** Sets the left child of this position */ public void setLeft(BTPosition<E> v) { left=v; } /** Returns the right child of this position */ public BTPosition<E> getRight() { return right; } /** Sets the right child of this position */ public void setRight(BTPosition<E> v) { right=v; } /** Returns the parent of this position */ public BTPosition<E> getParent() { return parent; } /** Sets the parent of this position */ public void setParent(BTPosition<E> v) { parent=v; }}

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

A.A. 06-07 DA1: Binary Tree 7

Classe LinkedBinaryTree<E>

import java.util.Iterator;/** * An implementation of the BinaryTree interface by means of a linked * structure. */public class LinkedBinaryTree<E> implements BinaryTree<E> { protected BTPosition<E> 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; }

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

A.A. 06-07 DA1: Binary Tree 8

Classe LinkedBinaryTree<E>: metodi dell'interfaccia Tree<E>

public int size() {return size;}

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

/** Returns whether a node is internal. */ public boolean isInternal(Position<E> v) { checkPosition(v); // auxiliary method return (hasLeft(v) || hasRight(v)); }

/** Returns whether a node is external. */ public boolean isExternal(Position<E> v) {return !isInternal(v); }

/** Returns whether a node is the root. */ public boolean isRoot(Position<E> v) { checkPosition(v); return (v == root()); }

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

A.A. 06-07 DA1: Binary Tree 9

Classe LinkedBinaryTree<E>: metodi dell'interfaccia Tree<E> (2)

/** Returns an iterable collection of the tree nodes. */ public Iterable<Position<E>> positions() { PositionList<Position<E>> positions = new NodePositionList<Position<E>>(); if(size != 0) preorderPositions(root(), positions); // assign positions in preorder return positions; }

/** Returns an iterator of the elements stored at the nodes */ public Iterator<E> iterator() { Iterable<Position<E>> positions = positions(); PositionList<E> elements = new NodePositionList<E>(); for (Position<E> pos: positions) elements.addLast(pos.element()); return elements.iterator(); // An iterator of elements }

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

A.A. 06-07 DA1: Binary Tree 10

Classe LinkedBinaryTree<E>: metodi dell'interfaccia Tree<E> (3)

/** Returns the parent of a node. */ public Position<E> parent(Position<E> v) { BTPosition<E> vv = checkPosition(v); Position<E> parentPos = vv.getParent(); if (parentPos == null) return null; return parentPos; }

/** Returns an iterable collection of the children of a node. */ public Iterable<Position<E>> children(Position<E> v) { PositionList<Position<E>> children = new NodePositionList<Position<E>>(); if (hasLeft(v)) children.addLast(left(v)); if (hasRight(v)) children.addLast(right(v)); return children; }

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

A.A. 06-07 DA1: Binary Tree 11

Classe LinkedBinaryTree<E>: metodi dell'interfaccia Tree<E> (4)

/** Returns the root of the tree.*/ public Position<E> root(){ if (isEmpty()) return null; return root; }

/** Replaces the element at a node. */ public E replace(Position<E> v, E o) { BTPosition<E> vv = checkPosition(v); E temp = v.element(); vv.setElement(o); return temp; }

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

A.A. 06-07 DA1: Binary Tree 12

Classe LinkedBinaryTree<E>: metodi dell'interfaccia BinaryTree<E>

/** Returns whether a node has a left child. */ public boolean hasLeft(Position<E> v){ BTPosition<E> vv = checkPosition(v); return (vv.getLeft() != null); } /** Returns whether a node has a right child. */ public boolean hasRight(Position<E> v){ BTPosition<E> vv = checkPosition(v); return (vv.getRight() != null); }

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

A.A. 06-07 DA1: Binary Tree 13

Classe LinkedBinaryTree<E>: metodi dell'interfaccia BinaryTree<E> (2)

/** Returns the left child of a node. */ public Position<E> left(Position<E> v) { BTPosition<E> vv = checkPosition(v); Position<E> leftPos = vv.getLeft(); if (leftPos == null) return null; return leftPos; }

/** Returns the right child of a node. */ public Position<E> right(Position<E> v) { BTPosition<E> vv = checkPosition(v); Position<E> rightPos = vv.getRight(); if (rightPos == null) return null; return rightPos; }

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

A.A. 06-07 DA1: Binary Tree 14

Classe LinkedBinaryTree<E>

/** Inserts a left child at a given node. */ public Position<E> insertLeft(Position<E> v, E e){ BTPosition<E> vv = checkPosition(v); Position<E> leftPos = vv.getLeft(); if (leftPos != null) return null; BTPosition<E> ww = createNode(e, vv, null, null); vv.setLeft(ww); size++; return ww;}

/** Inserts a right child at a given node. */ public Position<E> insertRight(Position<E> v, E e){ BTPosition<E> vv = checkPosition(v); Position<E> rightPos = vv.getRight(); if (rightPos != null) return null; BTPosition<E> w = createNode(e, vv, null, null); vv.setRight(w); size++; return w;}

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

A.A. 06-07 DA1: Binary Tree 15

Classe LinkedBinaryTree<E>

/** Removes a node with zero or one child. */ public E remove(Position<E> v){ BTPosition<E> vv = checkPosition(v); BTPosition<E> leftPos = vv.getLeft(); BTPosition<E> rightPos = vv.getRight(); if (leftPos != null && rightPos != null) return null; BTPosition<E> 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; } ...

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

A.A. 06-07 DA1: Binary Tree 16

Classe LinkedBinaryTree<E>

... // continue public E remove(Position<E> v) else { // v is not the root BTPosition<E> 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<E> addRoot(E e) { if(!isEmpty()) return null; size = 1; root = createNode(e,null,null,null); return root; }

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

A.A. 06-07 DA1: Binary Tree 17

Classe LinkedBinaryTree<E>

/** Attaches two trees to be subtrees of an external node. */ public void attach(Position<E> v, BinaryTree<E> T1, BinaryTree<E> T2) { BTPosition<E> vv = checkPosition(v); if (!T1.isEmpty()) { BTPosition<E> r1 = checkPosition(T1.root()); vv.setLeft(r1); r1.setParent(vv); // T1 should be invalidated } if (!T2.isEmpty()) { BTPosition<E> r2 = checkPosition(T2.root()); vv.setRight(r2); r2.setParent(vv); // T2 should be invalidated } }

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

A.A. 06-07 DA1: Binary Tree 18

Classe LinkedBinaryTree<E>

/** Return the sibling of a node */ public Position<E> sibling(Position<E> v) { BTPosition<E> vv = checkPosition(v); BTPosition<E> parentPos = vv.getParent(); if (parentPos != null) { BTPosition<E> sibPos; BTPosition<E> leftPos = parentPos.getLeft(); if (leftPos == vv) sibPos = parentPos.getRight(); else sibPos = parentPos.getLeft(); if (sibPos != null) return sibPos;}

return null;}

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

A.A. 06-07 DA1: Binary Tree 19

Classe LinkedBinaryTree<E>

/** Swap the elements at two nodes */ public void swapElements(Position<E> v, Position<E> w){ BTPosition<E> vv = checkPosition(v); BTPosition<E> 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<E> v, E l, E r) { insertLeft(v, l); insertRight(v, r); }

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

A.A. 06-07 DA1: Binary Tree 20

Classe LinkedBinaryTree<E>

/** If v is a good binary tree node, cast to BTPosition, else throw exception */ protected BTPosition<E> checkPosition(Position<E> v) { if (v == null || !(v instanceof BTPosition)) return null; return (BTPosition<E>) v; }

/** Creates a new binary tree node */ protected BTPosition<E> createNode(E element, BTPosition<E> parent, BTPosition<E> left, BTPosition<E> right) { return new BTNode<E>(element,parent,left,right); }

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

A.A. 06-07 DA1: Binary Tree 21

Classe LinkedBinaryTree<E>

/** 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<E> v, PositionList<Position<E>> 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<E> v, PositionList<Position<E>> 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 }}