Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 213 – Large Scale Programming Lecture 10: Tree & Binary Tree Implementation.

Similar presentations


Presentation on theme: "CSC 213 – Large Scale Programming Lecture 10: Tree & Binary Tree Implementation."— Presentation transcript:

1 CSC 213 – Large Scale Programming Lecture 10: Tree & Binary Tree Implementation

2 Today’s Goal Discuss Tree & BinaryTree implementations Linked Node-based implementations Array-based implementation Trade-offs of each

3 Trees Represents a hierarchical relationship Shows parent-child interactions csc213/ homeworks/ ideas.todo 1M programs/ LLQueue.java 10K DLNode.java 25K hard.txt 3K impossible.txt 2K Tree.java 20K Mammal Cat Ape LionHumanChimpanzee ProfessorsYankees Fan

4 Overloaded Term: Node Nodes make up links in linked list Nodes also make up items in a Tree Like in linked list, implements Position Therefore includes element() method Node is used in many situations Really just a generic term Usually used with classes implementing Position

5 Tree Interface Collection methods: int size() boolean isEmpty() Iterable methods: Iterator iterator() Iterable positions() Accessor methods: Position root() Position parent(p) Iterable > children(p) Query methods: boolean isInternal(p) boolean isExternal(p) boolean isRoot(p) Update method: E replace (p, e) Classes often include other methods Mostly add or remove data May throw InvalidPositionExcep tion

6 Node for Tree public class TNode implements Position { private E element; private TNode parent; private Sequence > kids; // Usually has getter & setter for element, parent // Often has getter for kids // Frequently will also include: public boolean isRoot() { return (parent==null); } public int getChildCount() { return kids.size(); } public TNode getChild(int i) { return kids.get(i); } public void addChild(TNode kid) { kids.insertLast(kid); } }

7  Linked Structure for Tree B D A CE F B  ADF  C  E

8 BinaryTree Interface Tree methods: int size() boolean isEmpty() Iterator iterator() Iterable positions() Position root() Position parent(p) Iterable > children(p) boolean isInternal(p) boolean isExternal(p) boolean isRoot(p) E replace (p, e) Child existence methods: boolean hasLeft(p) boolean hasRight(p) Child accessor methods: Position left(p) Position right(p) Rarely call children() on binary trees Much easier to use left() & right() But must be defined, since included in interface

9 Linked Node for BinaryTree public class BTNode implements Position { private E element; private BTNode parent; private BTNode left; private BTNode right; // Includes getters & setters for above // Often also includes: public boolean isRoot() { return (parent==null); } }

10 Linked Structure for BinaryTree B C A D   BACD  

11 Array-based BinaryTree Relies on each Node being at specific index Once placed, Node should not move Makes using ArrayList (Vector) difficult Root stored at index 0 Left child at index 1 Right child at index 2 Left child’s left child at rank 3 Right child’s left child at rank 5 Node at index n ’s left child is at index 2n + 1 Node at index n ’s right child is at index 2n + 2

12 Array-based Implementation Limits tree’s eventual depth Places limit on maximum size Can overcome limit using an ArrayList (Vector) May need to insert null to pad empty ranks Then use replaceAtRank() to add new nodes 0 12 5 6 4 9 10 A HG FE D C B J 3

13 Array-based BANode public class BANode implements Position { private E element; private int myIndex; private boolean hasLeft; private boolean hasRight; // Includes getters & setters for above // Includes getters & setters for left, right // Often also includes: public boolean isRoot() { return (myIndex == 0); } } Getters for left, right, & parent computes index Return -1 if the field has not yet been set setXYZ methods set boolean field to true All rely on BinaryTree class to hold actual array

14 For Next Lecture Discuss Preorder, Inorder, & Postorder tree traversals How these are implemented Why we need these different traversals When the traversals could be used


Download ppt "CSC 213 – Large Scale Programming Lecture 10: Tree & Binary Tree Implementation."

Similar presentations


Ads by Google