DOC PREVIEW
Penn CIT 594 - Trees Lecture Notes

This preview shows page 1-2-3-4-5-6 out of 19 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

TreesDefinition of a treeMore definitionsData structure for a treeADT for a treeA Tree ADT, IA Tree ADT, IITraversing 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 treeA tree is like a binary tree, except that a node may have any number of childrenDepending on the needs of the program, the children may or may not be orderedLike a binary tree, a tree has a root, internal nodes, and leavesEach node contains an element and has branches leading to other nodes (its children)Each node (other than the root) has a parentEach node has a depth (distance from the root)ACB D EGF H J KIL M N3More definitionsAn empty tree has no nodesThe descendents of a node are its children and the descendents of its childrenThe ancestors of a node are its parent (if any) and the ancestors of its parentThe subtree rooted at a node consists of the given node and all its descendentsAn 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 setThe branching factor of a node is the number of children it hasThe branching factor of a tree is the average branching factor of its nodes4Data structure for a treeA 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 a linked list class TreeNode { Object element; Vector children;}If we don’t care about the order of children, we might use a Set instead of a Vector5ADT for a treeIt must be possible to:Construct a new treeIf a tree can be empty, this may require a header nodeAdd a child to a nodeGet (iterate through) the children of a nodeAccess (get and set) the value in a nodeIt should probably be possible to:Remove a child (and the subtree rooted at that child)Get the parent of a node6A Tree ADT, IHere is a Tree ADT defined in Java Collections by David A. Watt and Deryck F. Brown: public interface Tree { // ...method declarations... public interface Node { public Object getElement(); public void setElement(Object elem); }}An interesting aspect of this ADT is that it uses an inner interfaceAn interface can’t have an inner classThe details are not important for our purposesThe inner interface is referred to by Tree.Node7A Tree ADT, II public interface Tree { // Accessors public Tree.Node root(); public Tree.Node parent(Tree.Node node); public int childCount(Tree.Node node); // Transformers public void makeRoot(Object elem); public Tree.Node addChild(Tree.Node node, Object elem); public void remove(Tree.Node node); // Iterator public Iterator children(Tree.Node node); // Inner interface for tree nodes public interface Node { public Object getElement(); public void setElement(Object elem); }}8Traversing a treeYou can traverse a tree in preorder: void preorderPrint(node) { 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 inorderWhy not?9Other tree manipulationsThere’s really nothing new to talk about; you’ve seen it all with binary treesA tree consists of nodes, each node has references to some other nodes—you know how to do all this stuffThere are some useful algorithms for searching trees, and with some modifications they also apply to searching graphsLet’s move on to some applications of trees10File systemsFile systems are almost always implemented as a tree structureThe nodes in the tree are of (at least) two types: folders (or directories), and plain filesA folder typically has children—subfolders and plain filesA 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 leaf11Family treesIt turns out that a tree is not a good way to represent a family treeEvery child has two parents, a mother and a fatherParents frequently remarryAn “upside down” binary tree almost worksSince it is a biological fact (so far) that every child has exactly two parents, we can use left child = mother and right child = fatherThe terminology gets a bit confusingIf you could go back far enough, it becomes a mathematical certainty that the mother and father have some ancestors in common12Part of a genealogyIsaacDavidPaulaStevenDanielleWinfredCarolChesterElaineEugenePauline13Game treesTrees are used heavily in implementing games, particularly board gamesA node represents a position on the boardThe children of a node represent all the possible moves from that positionMore precisely, the branches from a node represent the possible moves; the children represent the new positionsPlanning ahead (in a game) means choosing a path through the treeHowever—You can’t have a cycle in a treeIf you can return to a previous position in a game, you have a cycleGraphs can have cycles14Binary trees for expressionsOrdered trees can be used to represent arithmetic expressionsTo evaluate an expression (given as a node):If it is a leaf, the element in it specifies the valueIf the element is a number, that number is the valueIf the element is a variable, look up its value in a tableIf it is not a leaf,Evaluate the children and combine them according to the operation specified by the element+2 2The expression 2+2+2*3 4The expression 2+3*4*4+2 3The expression (2+3)*415(General) trees for expressionsYou can use binary trees for expressions if you have only unary and binary operatorsJava has a ternary operatorTrees can be used to represent statements as well as expressionsStatements can be evaluated as easily as expressions The expression x > y ? x :


View Full Document

Penn CIT 594 - Trees Lecture Notes

Documents in this Course
Trees

Trees

17 pages

Searching

Searching

24 pages

Pruning

Pruning

11 pages

Arrays

Arrays

17 pages

Stacks

Stacks

30 pages

Recursion

Recursion

25 pages

Hashing

Hashing

24 pages

Recursion

Recursion

24 pages

Graphs

Graphs

25 pages

Storage

Storage

37 pages

Trees

Trees

21 pages

Arrays

Arrays

24 pages

Hashing

Hashing

24 pages

Recursion

Recursion

25 pages

Graphs

Graphs

23 pages

Graphs

Graphs

25 pages

Stacks

Stacks

25 pages

Recursion

Recursion

25 pages

Quicksort

Quicksort

21 pages

Quicksort

Quicksort

21 pages

Graphs

Graphs

25 pages

Recursion

Recursion

25 pages

Searching

Searching

24 pages

Counting

Counting

20 pages

HTML

HTML

18 pages

Recursion

Recursion

24 pages

Pruning

Pruning

11 pages

Graphs

Graphs

25 pages

Load more
Download Trees Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Trees Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Trees Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?