Trees Definition of a tree n A tree

  • Slides: 19
Download presentation
Trees

Trees

Definition of a tree n A tree is like a binary tree, except that

Definition of a tree n 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 n Like a binary tree, a tree has a root, internal nodes, and leaves A n Each node contains an element and has branches leading to other nodes (its children) B C D E n Each node (other than the root) has a parent F G H I J K n Each node has a depth (distance from the root) L M N n 2

More definitions n n n n An empty tree has no nodes The descendents

More definitions n n n n 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 nodes 3

Data structure for a tree n A node in a binary tree can be

Data structure for a tree n A node in a binary tree can be represented as follows: class Binary. Tree. Node { Object value; Binary. Tree. Node left. Child, right. Child; } n 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 Tree. Node { Object element; Vector children; } n If we don’t care about the order of children, we might use a Set instead of a Vector 4

ADT for a tree n It must be possible to: n Construct a new

ADT for a tree n It must be possible to: n Construct a new tree n n n 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: n n Remove a child (and the subtree rooted at that child) Get the parent of a node 5

A Tree ADT, I n Here is a Tree ADT defined in Java Collections

A Tree ADT, I n 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 get. Element(); public void set. Element(Object elem); } } n An interesting aspect of this ADT is that it uses an inner interface n n n An interface can’t have an inner class The details are not important for our purposes The inner interface is referred to by Tree. Node 6

A Tree ADT, II public interface Tree { // Accessors public Tree. Node root();

A Tree ADT, II public interface Tree { // Accessors public Tree. Node root(); public Tree. Node parent(Tree. Node node); public int child. Count(Tree. Node node); // Transformers public void make. Root(Object elem); public Tree. Node add. Child(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 get. Element(); public void set. Element(Object elem); } } 7

Traversing a tree n You can traverse a tree in preorder: void preorder. Print(node)

Traversing a tree n You can traverse a tree in preorder: void preorder. Print(node) { System. out. println(node); Iterator iter = node. children. iterator(); while (iter. has. Next()) { preorder. Print(iter. next()); } } n You can traverse a tree in postorder: void postorder. Print(node) { Iterator iter = node. children. iterator(); while (iter. has. Next()) { postorder. Print(iter. next()); } System. out. println(node); } n You can’t usually traverse a tree in inorder n Why not? 8

Other tree manipulations n n There’s really nothing new to talk about; you’ve seen

Other tree manipulations n n 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 trees 9

File systems n File systems are almost always implemented as a tree structure n

File systems n File systems are almost always implemented as a tree structure n n 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 n n n 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 leaf 10

Family trees n It turns out that a tree is not a good way

Family trees n It turns out that a tree is not a good way to represent a family tree n n n Every child has two parents, a mother and a father Parents frequently remarry An “upside down” binary tree almost works n n n 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 common 11

Part of a genealogy Isaac Steven Paul a David Chester Danielle Elaine Eugene Winfred

Part of a genealogy Isaac Steven Paul a David Chester Danielle Elaine Eugene Winfred Carol Pauline 12

Game trees n n n Trees are used heavily in implementing games, particularly board

Game trees n n n 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 n 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— n n n 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 cycles 13

Binary trees for expressions n Ordered trees can be used to represent arithmetic expressions

Binary trees for expressions n Ordered trees can be used to represent arithmetic expressions + 2 2 The expression 2+2 * + * 3 4 The expression 2+3*4 n 2 4 3 The expression (2+3)*4 To evaluate an expression (given as a node): n If it is a leaf, the element in it specifies the value n n n 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, n Evaluate the children and combine them according to the operation specified by the element 14

(General) trees for expressions n n You can use binary trees for expressions if

(General) trees for expressions n n You can use binary trees for expressions if you have only unary and binary operators Java has a ternary operator if ? : > x y y The expression x > y ? x : y x = = y max x max y The statement if (x > y) max = x; else max = y; n n Trees can be used to represent statements as well as expressions Statements can be evaluated as easily as expressions 15

More trees for statements while (n >= 1) { exp = x * exp;

More trees for statements while (n >= 1) { exp = x * exp; n--; } for (int i = 0; i < n; i++) a[i] = 0; for while >= n = ; 1 = exp x * -- int n i < 0 i n ++ = i [] 0 a i exp 16

Writing compilers and interpreters n A compiler does three things: n n An interpreter

Writing compilers and interpreters n A compiler does three things: n n An interpreter does three things: n n Parses the input program (converts it into an abstract syntax tree) (Optionally) optimizes the abstract syntax tree Traverses the tree and outputs assembly language or machine code to do the same operations Parses the input program (converts it into an abstract syntax tree) (Optionally) optimizes the abstract syntax tree Traverses the tree in an order controlled by the node contents, and performs the operations as it goes Parsing is usually the hard part, but there is a very simple technique (called recursive descent parsing) that can be used if the language is carefully designed and you don’t care too much about efficiency or good error messages 17

I’ll never need to write a compiler. . . n n n Are you

I’ll never need to write a compiler. . . n n n Are you sure? If you can’t parse text inputs, you are limited to reading simple things like numbers and Strings If you can parse text input, you can make sense of: n n n tell Mary "Meet me at noon" fire phasers at 3, 7 17. 25, 0. 203 + 8. 97 i, 0. 95 i 28° 12"48' 3: 30 pm-5 pm Parsing is less important in these days of GUIs, but it’s still pretty important n Java provides basic support for parsing with its String. Tokenizer and Stream. Tokenizer classes 18

The End 19

The End 19