Presentation is loading. Please wait.

Presentation is loading. Please wait.

Building and Traversing Trees

Similar presentations


Presentation on theme: "Building and Traversing Trees"— Presentation transcript:

1 Building and Traversing Trees
COMP 103 Building and Traversing Trees

2 RECAP-TODAY RECAP The last lecture covered tree concepts and terminology TODAY Using Linked Structures to represent Trees in Java Building binary/general trees: top down, bottom up Traversing Binary/General Trees: print all, count nodes, find node What kind of data structure can we use to represent a tree? Each node needs to store an item of data Also needs links to children:fixed # of links for n-ary; list/set for general May also need a link to its parent? G T K M Z A

3 Representing tree data: linked nodes?
Using Linked Structures, like Linked List Nodes: Binary Tree Nodes: General Tree Nodes Binary: left, right Ternary: left, middle, right Use array for larger n G T K M Z A G M K G K M Can use array or linked representation for list/set of children

4 Representing tree data: arrays?
Array of objects, each with item, indexes for children (and maybe parent) Array for the items Arrays for the links between parents and children, or Compute where the parents and children will be? (we will return to this later) K 5 4 A -1 G 3 M 1 -1 Z -1 T -1 1 2 3 4 5 6 Root G T K M Z A A G M Z T K -1 1 5 -1 3 4 K M T Z A G 1 2 3 4 5 6

5 Binary Trees : BinTreeNode
public class BinTreeNode<E> { private E item; private BinTreeNode<E> parent = null; private BinTreeNode<E> left = null, right = null; public BinTreeNode(E it) { item = it; } public BinTreeNode(E it, BinTreeNode<E> p , BinTreeNode<E> l, BinTreeNode<E> r ) { item = it; parent = p; left = l; right = r; } public void setParent(BinTreeNode<E> p) { parent = p; } public BinTreeNode<E> getParent() { return parent; } public void setLeft ( BinTreeNode<E> l) { left = l; } public BinTreeNode<E> getLeft() { return left; } // similarly setRight, getRight,... // similarly getItem, setItem, … } data children Parent – not always needed G M K

6 Building a Binary Tree : Top Down
BinTreeNode<String> root = new BinTreeNode<String>(“G”); BinTreeNode<String> k = new BinTreeNode<String>(“K”); k.setParent(root); root.setLeft(k); BinTreeNode<String> m = new BinTreeNode<String>(“M”); m.setParent(root); root.setRight(m); BinTreeNode<String> t = new BinTreeNode<String>(“T”); t.setParent(k); k.setLeft(t); BinTreeNode<String> z = new BinTreeNode<String>(“Z”); z.setParent(k); k.setRight(z); BinTreeNode<String> a = new BinTreeNode<String>(“A”); a.setParent(m); m.setLeft(a); G T K M Z A

7 Building a Binary Tree : Bottom Up
BinTreeNode<String> t= new BinTreeNode<String>(“T”); BinTreeNode<String> z = new BinTreeNode<String>(“Z”); BinTreeNode<String> k = new BinTreeNode<String>(“K”, t, z); BinTreeNode<String> a = new BinTreeNode<String>(“A”); BinTreeNode<String> m = new BinTreeNode<String>(“M”, a, null); BinTreeNode<String> root = new BinTreeNode<String>(“G”, k, m); or..., how about this??! BinTreeNode<String> root = new BinTreeNode<String>(“G”, new BinTreeNode<String>(“K”, new BinTreeNode<String>(“K”), new BinTreeNode<String>(“Z”) ), new BinTreeNode<String>(“M”, new BinTreeNode<String>(“A”), null ) ); G T K M Z A

8 General Tree Representation
public class GenTreeNode<E> {    private E item;    private GenTreeNode<E> parent;    private Set<GenTreeNode<E>> children: : } root item parent ch item parent ch item parent ch item parent ch item parent ch item parent ch

9 General Trees : GenTreeNode
public class GenTreeNode<E> { private E item; private GenTreeNode<E> parent = null; private Set<GenTreeNode<E>> children = new HashSet<GenTreeNode<E>>(); public GenTreeNode(E it) { item = it; } public GenTreeNode(E it, GenTreeNode<E> p, Set<GenTreeNode<E>> c ) { item = it; parent = p; children = c; } public void setParent(GenTreeNode<E> p) { parent = p; } public GenTreeNode<E> getParent() { return parent; } public void setChildren( Set<GenTreeNode<E>> c) { children = c; } public Set<GenTreeNode<E>> getChildren() { return children; } public void addChildren( GenTreeNode<E> c) { children.add(c); } … } G K M data Parent – not always needed Set of Nodes to store children Of course! we can use the usual set/list operations on children

10 Building a General Tree : Top Down
GenTreeNode<String> root = new GenTreeNode<String>(“G”); GenTreeNode<String> k = new GenTreeNode<String>(“K”); k.setParent(root); root.addChild(k); GenTreeNode<String> m = new GenTreeNode<String>(“M”); m.setParent(root); root.addChild(m); GenTreeNode<String> t = new GenTreeNode<String>(“T”); t.setParent(k); k.addChild(t); GenTreeNode<String> z = new GenTreeNode<String>(“Z”); r.setParent(k); k.addChild(z); GenTreeNode<String> a = new GenTreeNode<String>(“A”); a.setParent(m); m.addChild(a); Or build the list of children separately and use setChildren. Ex: Build the same tree bottom up. G T K M Z A

11 Traversing a binary tree
Vist root and both children – if they exist May visit root before or after the children private void listAll(BinTreeNode<E> node) { System.out.println(node.item.toString()) ; if ( node.getLeft() != null ) listAll(node.getLeft()); if ( node.getRight() != null ) listAll(node.getRight()); } OR System.out.println(node.item.toString() );

12 Count nodes, find node Traversing and returning a value:
private int count(BinTreeNode<E> node) { int ans = 1; if ( node.getLeft() != null ) ans += count(node.getLeft()); if ( node.getRight() != null ) ans += count(node.getRight()); return ans; } Finding a node: private GenTreeNode<E> find(GenTreeNode<E> node, E name) { if (node.item.hasName(name) ) return node; for (GenTreeNode<E> ch : node.children) { GenTreeNode<E> ans = find(ch, name); if (ans != null) return ans; return null; Search is a traversal with a possible early exit Search is a traversal with an early exit

13 Traversing a general tree
Use a loop to step through the children Unlike the binary tree, we won’t need to explicitly test whether children exist private void listAll(GenTreeNode<E> node) { System.out.println(node.item.toString()); for (GenTreeNode<E> ch : node.children) listAll(ch); } OR


Download ppt "Building and Traversing Trees"

Similar presentations


Ads by Google