TreesDefinition of a treeMore definitionsData structure for a treeGeneral requirements for an ADTADT for a treeA Tree ADT, I: Parents and valuesA Tree ADT, II: children and siblingsA Tree ADT, III: Iterator, other methodsTraversing a treeOther tree manipulationsFile systemsFamily treesPart of a genealogyGame treesBinary trees for expressions(General) trees for expressionsMore trees for statementsWriting compilers and interpretersI’ll never need to write a compiler...The EndTrees2Definition of a treeA tree is like a binary tree, except that a node may have any number of childrenDepending on the needs of the program, the children may or may not be orderedLike a binary tree, a tree has a root, internal nodes, and leavesEach node contains an element and has branches leading to other nodes (its children)Each node (other than the root) has a parentEach node has a depth (distance from the root)ACB D EGF H J KIL M N3More definitionsAn empty tree has no nodesThe descendents of a node are its children and the descendents of its childrenThe ancestors of a node are its parent (if any) and the ancestors of its parentThe subtree rooted at a node consists of the given node and all its descendentsAn ordered tree is one in which the order of the children is important; an unordered tree is one in which the children of a node can be thought of as a setThe branching factor of a node is the number of children it hasThe branching factor of a tree is the average branching factor of its nodes4Data structure for a treeA node in a binary tree can be represented as follows: class BinaryTreeNode { Object value; BinaryTreeNode leftChild, rightChild;}However, each node in a tree has an arbitrary number of children, so we need something that will hold an arbitrary number of nodes, such as a Vector or an ArrayList or a LinkedList class TreeNode { Object element; Vector<TreeNode> children;}We can use an array, but that’s expensive if we need to add or delete childrenIf the order of children is irrelevant, we may use a Set instead of a VectorIf order of children matters, we cannot use a Set5General requirements for an ADTThe constructors and transformers must together be able to create all legal values of the ADTA constructor or transformer should never create an illegal valueIt’s nice if the constructors alone can create all legal values, but sometimes this results in constructors with too many parameters for reasonable convenienceThe accessors must be able to extract any data needed by the application6ADT for a treeIt must be possible to:Construct a new treeIf a tree can be empty, this may require a header nodeAdd a child to a nodeGet (iterate through) the children of a nodeAccess (get and set) the value in a nodeIt should probably be possible to:Remove a child (and the subtree rooted at that child)Get the parent of a node7A Tree ADT, I: Parents and valuesConstructor:public Tree(Object value)Values:public Object getValue()public void setValue(Object value)Parents and ancestors:public Tree getParent()public boolean hasAncestor(Tree ancestor)8A Tree ADT, II: children and siblingsChildren:public void addChild(Tree newChild)public void addChildren(ArrayList newChildren)public void detachFromParent()public boolean hasChildren()public ArrayList getChildren()public Tree GetFirstChild()public Tree getLastChild()Siblings:public boolean hasNextSibling()public Tree getNextSibling()public boolean hasPreviousSibling()public Tree getPreviousSibling()9A Tree ADT, III: Iterator, other methods Iterator (preorder traversal of the Tree):public Iterator iterator()public boolean hasNext()public Object next()public void remove()Convenience methods:public boolean isRoot()public boolean isLeaf()public int depth()Standard methods:public boolean equals(Object o)public String toString()public void print()10Traversing a treeYou can traverse a tree in preorder: void preorderPrint(node) { // doesn’t use Tree.iterator() System.out.println(node); Iterator iter = node.children.iterator(); while (iter.hasNext()) { preorderPrint(iter.next()); }}You can traverse a tree in postorder: void postorderPrint(node) { Iterator iter = node.children.iterator(); while (iter.hasNext()) { postorderPrint(iter.next()); } System.out.println(node);}You can’t usually traverse a tree in inorderWhy not?11Other tree manipulationsThere’s really nothing new to talk about; you’ve seen it all with binary treesA tree consists of nodes, each node has references to some other nodes—you know how to do all this stuffThere are some useful algorithms for searching trees, and with some modifications they also apply to searching graphsLet’s move on to some applications of trees12File systemsFile systems are almost always implemented as a tree structureThe nodes in the tree are of (at least) two types: folders (or directories), and plain filesA folder typically has children—subfolders and plain filesA folder also contains a link to its parent—in both Windows and UNIX, this link is denoted by ..In UNIX, the root of the tree is denoted by /A plain file is typically a leaf13Family treesIt turns out that a tree is not a good way to represent a family treeEvery child has two parents, a mother and a fatherParents frequently remarryAn “upside down” binary tree almost worksSince it is a biological fact (so far) that every child has exactly two parents, we can use left child = mother and right child = fatherThe terminology gets a bit confusingIf you could go back far enough, it becomes a mathematical certainty that the mother and father have some ancestors in common14Part of a genealogyIsaacDavidPaulaStevenDanielleWinfredCarolChesterElaineEugenePauline15Game treesTrees are used heavily in implementing games, particularly board gamesA node represents a position on the boardThe children of a node represent all the possible moves from that positionMore precisely, the branches from a node represent the possible moves; the children represent the new positionsPlanning ahead (in a game) means choosing a path through the treeHowever—You can’t have a cycle in a treeIf you can return to a previous position in a game, you have a cycleGraphs can
View Full Document