Trees Weiss sec. 7.3.5 (preview) ch. 18 (sort of).

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CSE 143 Lecture 18 Binary Trees read slides created by Marty Stepp and Hélène Martin
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees, Binary Trees, and Binary Search Trees COMP171.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
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.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
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 Lecture 12 CS2110 – Fall Announcements  Prelim #1 is tonight!  Olin 155  A-L  5:30  M-Z  5:30  A4 will be posted today  Mid-semester.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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 : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Building Java Programs Binary Trees reading: 17.1 – 17.3.
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.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
CS Prelim Review – 10/15/09  First prelim is TOMORROW at 7:30 PM  Review session – Tonight 7:30 PM, Phillips 101  Things you should do:  Review every.
TREES Lecture 11 CS2110 – Spring Readings and Homework  Textbook, Chapter 23, 24  Homework: A thought problem (draw pictures!)  Suppose you use.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
1 CMSC 341 Introduction to Trees Textbook sections:
Trees A non-linear implementation for collection classes.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Recursive Objects (Part 4)
Trees Lecture 12 CS2110 – Spring 2017.
Trees Lecture 12 CS2110 – Fall 2017.
Trees Lecture 12 CS2110 – Fall 2016.
Trees.
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
CS223 Advanced Data Structures and Algorithms
slides created by Alyssa Harding
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Trees.
Trees Lecture 9 CS2110 – Fall 2009.
Design and Analysis of Algorithms
Binary Trees, Binary Search Trees
Trees.
Announcements Prelim 1 on Tuesday! A4 will be posted today
Trees.
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Trees Lecture 10 CS2110 – Spring 2013.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Trees Weiss sec (preview) ch. 18 (sort of)

Data Structures So Far Arrays –random access, fixed size, hard to insert/delete java.util.ArrayList –random access, dynamic resize, hard to insert/delete Linked Lists –sequential access, dynamic resize, easy to insert/delete Something new: –semi-random access, dynamic resize, semi-easy to insert/delete

Trees Iterative description: –a set of cells –each cell has 0 or more successors (children) –one cell is called the root –each cell has 1 predecessor (parent), except the root which has no parent Recursive description: –a tree can be empty –a tree can be a cell with 0 or more non-empty trees as children Binary tree: –a tree where each cell has at most 2 children General tree Binary tree Not a tree List-like tree

Terminology edge A  B –A is parent of B –B is child of A path R  A  B –R and A are ancestors of B –A and B are descendants of R leaf node has no descendants depth of node is length of path from root to the node –depth(A) = 1 –depth(B) = 2 height of node: length of longest path from node to leaf –height(A) = 1 –height(B) = 0 height of tree = height of root –in example, height of tree = Binary tree A B Left sub-tree of root Right sub-tree of root Root of tree C R D

Binary Tree Trivia At most 2 d cells at depth d In tree of height h: –at least h+1 elements –at most 2 h+1 – 1 elements depth Height 2, minimum number of nodes Height 2, maximum number of nodes

Code public class TreeCell { private Object elt; private TreeCell left; private TreeCell right; // Construct a tree with only a single cell public TreeCell(Object elt) { this(elt, null, null); // defer to three- argument constructor } // Construct a tree with children public TreeCell (Object elt, TreeCell left, TreeCell right) { this.elt = elt; this.left = left; this.right = right; } /* getters and setters: getElt, getLeft, getRight, setElt, … */ } TreeCell elt left right 5

General Trees: GTreeCell One approach: array of children public class GTreeCell { protected Object elt; protected GTreeCell [ ] children; … }

General Trees: GTreeCell Second approach: linked list of children public class GTreeCell { protected Object elt; protected ListCell children; … }

General Trees: GTreeCell Third approach: TreeCell doubles as a ListCell –Store the next pointer from ListCell in the TreeCell object –Each cell maintains link to left child and right sibling public class GTreeCell { protected Object elt; protected GTreeCell left; protected GTreeCell sibling; // essentially ListCell.next General tree Tree represented using GTreeCell 5 Q: How to enumerate children?

General Trees: GTreeCell Last approach: Circular sibling list –Let the sibling list “wrap around” –Helps with some operations General tree Tree represented using GTreeCell 5

Applications Trees can represent structure of a language as an abstract syntax tree (AST) Example grammar: E : (E + E) E : integer | variable What can we do with AST? –integration, differentiation, etc. –constant-expression elimination –re-arrange expressions -34 (x + 3) + x 3 ((2+y) + (5+7)) + 2 y TextTree representation

Recursion on Trees Similar to recursion on lists or integers Base case: empty tree (or one-cell tree) Recursive case: –solve for subtrees –combine these solutions to get solution for whole tree

Search: Recursive Find out if object o is in the tree: static boolean search(TreeCell t, Object o); Recursive version is trivial; Try the iterative version at home… public static boolean search(TreeCell t, Object o) { if (t == null) return false; else return t.getElt().equals(o) | | search(t.getLeft(), o) | | search(t.getRight(), o); } ((2+y) + (5+7)) + 2 y

Traversal Ordering search( ) traverses tree in following recursive order: –first process root node –then process left subtree –then process right subtree pre-order walk: root, left, right in-order walk: left, root, right post-order walk: left, right, root + 2 y

Variations Write a header class called Tree –tree methods can be instance & static methods of Tree Maintain reference to parent in each cell –analagous to doubly-linked list