CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 

Slides:



Advertisements
Similar presentations
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Advertisements

Trees1 More on Trees University Fac. of Sci. & Eng. Bus. School Law School CS Dept. EE Dept. Math. Dept.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees, Binary Trees, and Binary Search Trees COMP171.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
General Info Check out gilgarn.org. QUIZ DO YOU: – Love CSC 171 ? – Want a job? – Like to exert power over others? – Want to improve CSC UR?
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
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.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; } 
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 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Tree Data Structures.
Compiled by: Dr. Mohammad Omar Alhawarat
Trees, Binary Trees, and Binary Search Trees COMP171.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
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.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
2/11/ IT 179 Recursive Definition of Tree Structures 1.Empty is a tree; the root is null 2.A node points to a finite number of the roots of some.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 CMSC 341 Introduction to Trees Textbook sections:
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Trees. Trees: – A trunk from the roots – Divides into branches – Ends in leaves.
CSCE 3110 Data Structures & Algorithm Analysis
Trees Saurav Karmakar
CS 201 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
MCS680: Foundations Of Computer Science
Week 6 - Wednesday CS221.
Trees 2 CSC 172 SPRING 2004 LECTURE 15.
Tree.
CMSC 341 Introduction to Trees.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Introduction to Trees IT12112 Lecture 05.
slides created by Alyssa Harding
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Trees.
Data Structures and Algorithms for Information Processing
Trees Definitions Implementation Traversals K-ary Trees
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
Binary Trees, Binary Search Trees
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

CSC 172 DATA STRUCTURES

LISTS We have seen lists: public class Node { Object data; Node next; } 

TREES Now, look at trees: public class Node { Object data; Node left; Node right; }        

ROOTED TREES Collection of nodes, one of which is the root Nodes != root have a unique parent node Each non-root can reach the root by following parent links one or more times

DEFINITIONS If node p is the parent of node c then c is a child of p Leaf : no children Interior node : has children Path : list of nodes (m 1,m 2,…,m k ) such that each is the parent of the following path “from m 1 to m k ” Path length = k-1, number of links, not nodes

If there is a path from m to n, then m is an ancestor of n and n is a descendant of m Note m == n is possible Proper ancestors, descendants : m != n Height of a node n is the length of the longest path from n to a leaf Height of a tree is the height of its root Depth of a node is the length of the path from the root to n Subtree rooted at n is all the descendants of n

The children of any given note are often ordered “from the left” Child c 1 is to the left of c 2 then all the nodes in the subtree rooted at c 1 are “to the left” of those in the subtree rooted at c 2 Nodes may have labels, which are values or data associated with the nodes

Example: UNIX File Systems

/ /bin/dev/usr … /dev/term/dev/sound/dev/tty01. /usr/anna/usr/jon/usr/ted

Example: Expression Trees Labels are operands or operators Leaves : operands Interior nodes : operators Children are roots of sub-expressions to which the operator is applied

(x+1)*(x-y+4) x1x y *+- - 4

Example Musical Time Span Reduction

Recursion on Trees Many algorithms to process trees are designed with a basis (leaves) and induction (interior nodes) Example: If we have an expression tree we can get infix (operator between operands - common) prefix (operator before operands – like function calls) postfix (operator after operands – good for compilers)

Expression Tree to Postfix Basis For a leaf, just print the operand Induction: For an interior node apply algorithm to each child from left print the operator

(x+1)*(x-y+4) x1x y * x 1 + x y – 4 - *

TREE DATA STRUCTURES

BINARY TREES Some trees are binary: public class Node { Object data; Node left; Node right; }        

Some trees are not binary / /bin/dev/usr … /dev/term/dev/sound/dev/tty01. /usr/anna/usr/jon/usr/ted How do we implement such trees?

LMC-RS Leftmost-Child, Right-Sibling Tree Representation Each node has a reference to 1. It’s leftmost child 2. It’s right sibling – the node immediately to the right having the same parent Advantage: represents trees without limits or pre- specified number of children Disadvantage: to find the i th child of node n, you must traverse a list n long

LMC-RS public class Node { Object data; Node l_child; Node r_sibling; }       

Proving Tree Properties Structural Induction Basis = leaves (one-node trees) Induction = interior nodes (trees with => 2 nodes) Assume the statement holds for the subtrees at the children of the root and prove the statement for the whole tree

Tree Proof Example Consider a LMC-RS tree S(T): T has one more  reference than it has nodes Basis: T is a single node – 2  references  

Induction T has a root r and one or more sub trees T 1, T 2,…,T k BTIH: each of these trees, by itself has one more  than nodes How many nodes all together? How many  references? How many nodes do I add to make one tree? How many  references do we reduce to make one tree?

T 1 n 1 nodes n 1 +1  T 2 n 2 nodes n 2 +1  T k n k nodes n k +1  … ?  ?  ?  One more node One more  Still “k” extra 

T 1 n 1 nodes n 1 +1  T 2 n 2 nodes n 2 +1  T k n k nodes n k +1  … ? ?  ? One more node One more  Still “k” extra  How many less?

Example: Pair Quiz S(T): A full binary tree of height h has (2 h+1 – 1) nodes Basis? Induction?

Example: S(T): A full binary tree of height h has 2 h+1 – 1 nodes Basis? Nodes = 1, height == 0, = 1 Induction?

T 1 h height 2 h+1 -1 nodes Height = h+1 T 2 h height 2 h+1 -1 nodes

A tree viewed recursively

Binary Tree public class BinTree { Object data; BinTree leftChild; BinTree rightChild; ….// accessor methods }

Exercise:Size of a tree Recursive view S T = S L + S R + 1

Size of a Tree Quiz public static int size (BinaryNode t) { }

Size of a Tree public static int size (BinaryNode t) { if (t == null) return 0 ; else return 1 + size(t.left) + size(t.right); }

Height of a tree Recursive view of height calculation H T = Max(H L,H R ) + 1;

Height of a Tree public static int height (BinaryNode t) { if (t == null) return -1 ; else return 1 + Math.max(height(t.left)),(height(t.right)); }

Tree Traversal (a) Preorder (b) Postorder (c) Inorder

Preorder public void printPreOrder() { }

Preorder public void printPreOrder() { System.out.println(element); if (left != null) left.printPreOrder(); if (right != null) right.printPreOrder(); }

Inorder public void printInOrder() { }

Inorder public void printInOrder() { if (left != null) left.printInOrder(); System.out.println(element); if (right != null) right.printInOrder(); }

Postorder public void printPostOrder() { }

Postorder public void printPostOrder() { if (left != null) left.printPostOrder(); if (right != null) right.printPostOrder(); System.out.println(element); }