Trees. Definition 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.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Trees. 2 Definition 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,
Binary Trees, Binary Search Trees COMP171 Fall 2006.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
Trees Chapter 8.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees. 2 Definition 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,
Trees. 2 Definition 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,
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Trees. 2 Definition of a tree A tree is a node with a value and zero or more children Depending on the needs of the program, the children may or may not.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
Trees CS /02/05 L7: Trees Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Definition.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Copyright © Curt Hill Other Trees Applications of the Tree Structure.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
Trees A non-linear implementation for collection classes.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
CS 201 Data Structures and Algorithms
CS 302 Data Structures Trees.
Csc 2720 Instructor: Zhuojun Duan
Trees.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
Trees.
Trees.
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
CE 221 Data Structures and Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Trees.
Binary Trees, Binary Search Trees
Trees Chapter 10.
CE 221 Data Structures and Algorithms
Trees.
Binary Trees, Binary Search Trees
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Trees

Definition 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) A CBDE GFHJKI LMN

More 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

Data 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 Vector

ADT 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 the value in a node It should probably be possible to: –Remove a child –Get the parent of a node

The textbook Tree ADT, I An interesting aspect of the Tree ADT described in the Java Collections textbook is that it uses an inner interface public interface Tree { //...method declarations... public interface Node { public Object getElement(); public void setElement(Object elem); } } The inner interface is referred to by Tree.Node

The textbook 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); } }

Traversing a tree You can traverse a tree in preorder: void preorderPrint() { System.out.println(this.element); Iterator iter = this.children.iterator(); while (iter.hasNext()) { preorderPrint(iter.next()); } } You can traverse a tree in postorder: void postorderPrint() { Iterator iter = this.children.iterator(); while (iter.hasNext()) { postorderPrint(iter.next()); } System.out.println(this.element); } You can’t usually traverse a tree in inorder –Why not?

Other tree manipulations There’s really nothing new to talk about 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, but (with minor modifications) they also apply to searching graphs, so we’ll do them later Let’s move on to some applications of trees

Expression trees 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 + 22 The expression * 34 The expression 2+3*4 * The expression (2+3)*4

Binary 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 : y ?: >x x y y The statement if (x > y) max = x; else max = y; y if > xxy max ==

Writing compilers and interpreters A compiler does three things: –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 An interpreter does three things: –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

I’ll never write a compiler... 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: –tell Mary "Meet me at noon" –fire phasers at 3, 7 –17.25, i, 0.95i –28°12"48' –3:30pm-5pm Parsing is less important in these days of GUIs, but it’s still pretty important –Java provides basic support for parsing with its StringTokenizer and StreamTokenizer classes –Remember my Lisp applet?

Family 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 –There are lots of shared subtrees (since two people can have the same parent or parents) so what we have isn’t exactly a binary tree

Game 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 cycles

The End