23 Tree Implementation
23 Tree Implementation
Today
Tree Implementation
COMP103 2006, Lecture 23
Implementing binary Code to iterate through a binary tree Implementation of General Trees Reading: Chapter 12
Binary Tree
Each node contains a value Each node has a left child and a right child (may be null).
public class BinaryTree<V> { private V value; private BinaryTree<V> left; private BinaryTree<V> right; /**Creates a new tree with one node (a root node) containing the specified value */ public BinaryTree(V value) { this.value = value; }
Created with novaPDF Printer (www.novaPDF.com). Please register to remove this message.
Using BinaryTree
A method that constructs a tree
public static void main(String[] args){ BinaryTree<String> mytree = new BinaryTree<String>("Me"); mytree.setLeft(new BinaryTree<String>("Dad")); mytree.setRight(new BinaryTree<String>("Mum")); mytree.getLeft().setLeft(new BinaryTree<String>("Grandfather")); mytree.getLeft().setRight(new BinaryTree<String>("Grandmother")); mytree.getRight().setLeft(new BinaryTree<String>("Grandpa")); mytree.getRight().setRight(new BinaryTree<String>("Grandma")); mytree.getRight().getRight().setRight( new BinaryTree<String>("GreatGrandma")); BinaryTree<String> gf = mytree.find("Grandfather"); if (gf!=null) gf.setRight(new BinaryTree<String>("GreatGrandmother")); }
myTree
Me
Using BinaryTree
BinaryTree<String> ma = mytree; while(!ma.getRight().isLeaf()) ma = ma.getRight();
Mum
Dad
printAll(mytree, "");
Created with novaPDF Printer (www.novaPDF.com). Please register to remove this message.
10
Using BinaryTree
public static void printAll(BinaryTree<String> tree, String indent){ System.out.println(indent+ tree.getValue()); if (tree.getLeft()!=null) printAll(tree.getLeft(), indent+" " ); if (tree.getRight()!=null) printAll(tree.getRight(), indent+" " ); }
11
12
Iterative Pre-order
Me Dad Mum Grandpa Grandma
Gfather
Gmother
GGmother
GGrandma
Created with novaPDF Printer (www.novaPDF.com). Please register to remove this message.
13
14
General Tree
A node in the tree can have any number of children This version will keep the children in order.
public class GeneralTree<V> { private V value; private List< GeneralTree<V> > children; public GeneralTree(V value) { this.value = value; this.children = new ArrayList< GeneralTree<V> >(); }
15
16
Get
Children are ordered, so can ask for the i th child
public V getValue() { return value; } public List< GeneralTree<V> > getChildren() { return children; } public GeneralTree<V> getChild(int i) { if (i>=0 && i < children.size()) return children.get(i); else return null; }
Created with novaPDF Printer (www.novaPDF.com). Please register to remove this message.
17
18
The Tree
John
Julian
Jake
Jack
John
Jill
19
20
More adding
GeneralTree<String> thrd = mytree.getChildren().get(2); thrd.addChild(new GeneralTree<String>("Justine")); GeneralTree<String> gc = mytree.find("Jack"); if (gc!=null) gc.addChild(new GeneralTree<String>("Jeremiah")); System.out.println ("2nd child of 1st child: "+ mytree.getChild(0).getChild(1).getValue());
printAll
printAll(mytree, ""); public static void printAll(GeneralTree<String> tree, String indent){ System.out.println(indent+ tree.getValue()); for(GeneralTree<String> child : tree.getChildren()) printAll(child, indent+" " ); }
Created with novaPDF Printer (www.novaPDF.com). Please register to remove this message.