Trees - Ed. 2. and 3.: Chapter 6 - Ed. 4.: Chapter 7
Trees What is a tree? -Examples of trees -Tree interface and interface hierarchy Algorithms on binary trees -Binary tree interface -Traversal on a binary tree -Binary tree implementation -Traversal on a tree Sample case study application
Trees
Example:
A formal definition of trees: A treeT is a set ofnodes storing elements in aparent-child relationship with the following properties: T has a special noder, called theroot ofT. Each nodev ofT different fromr has aparent nodeu. r v u T
… …
Comparing Example 6.3 with Example 6.2, we can see an important difference between the ordered tree and the unordered tree. In an ordered tree, the order of the children of a node is significant while in an unordered tree, the order of the children of a node is not important.
The Tree Abstract Data Type
public interface Tree { public int size(); public Boolean isEmpty(); public ElementIterator elements(); public PositionIterator positions(); public void swapElements( Position v, Position w ); public Object replaceElement( Position v, Object e ); public Position root(); public Position parent( Position v ); public PositionIterator children( Position v ); public boolean isInternal( Position v ); public boolean isExternal( Position v ); public boolean isRoot( Position v ); } A Tree Interface in Java
Construction of Interface Hierarchy
IspectableContainer size isEmpty Elements IspectablePositionContainer positions PositionContainer swapElement replaceElement InspectableTree root parent children isRoot isInternal isExternal Tree
The Binary Tree Abstract Data Type
A Binary Tree Interface in Java