Trees.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Computer Science 2 Data Structures and Algorithms V section 2 Introduction to Trees Professor: Evan Korth New York University.
Trees, Binary Trees, and Binary Search Trees COMP171.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
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 CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
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.
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.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
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.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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,
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.
CSC 172 DATA STRUCTURES. LISTS We have seen lists: public class Node { Object data; Node next; } 
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.
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.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems.
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 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
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.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
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.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Binary Trees.
Trees Saurav Karmakar
CS 201 Data Structures and Algorithms
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Binary Trees.
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Recursive Objects (Part 4)
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
Tree.
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
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
Binary Trees.
slides created by Alyssa Harding
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Binary Trees.
Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Trees Definitions Implementation Traversals K-ary Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Lecture 36 Section 12.2 Mon, Apr 23, 2007
CE 221 Data Structures and Algorithms
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Binary Trees.
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Binary Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Trees

Tree Consists of a set of nodes and directed edges connecting them Rooted trees One node is distinguished as root Every node c, except root, has exactly one incoming edge from another node p. p is c’s parent. There exists a unique path from root to any node

Tree Properties and Definitions A tree with N nodes has N-1 edges Siblings – Nodes with same parent Ancestor & descendant If there is a path from u to v, v is the descendant of u & u is the ancestor of v If u ≠ v, v is proper descendant of u & u is proper ancestor of v

Definitions (Contd.) Depth of a node – Length of path from root to node Depth of root is 0 Depth of any node is one more than its parent’s depth Height of a node – Length of path from node to deepest descendant leaf Size of a node – Number of descendants of the node (including itself) Height of a tree – Height of the root of the tree

Example

Recursive Definition A tree is Either empty Or it consists of root and zero or more of subtrees Roots of subtrees connected by an edge to root of tree

Illustration

Implementing Trees Straightforward method Every node in the tree has link to each of its children Number of children can vary greatly Child count may not be known before hand Leads to space wastage First Child – Next Sibling method Children are stored as linked lists Node stores link to leftmost child and right sibling

Illustration

Binary Tree A tree in which each node has at most two children Left child and right child Recursive definition A binary tree is either empty, or it consists of a root, a left tree and a right tree Applications Expression trees in compilers Huffman coding tree Binary search trees

Applications

Binary Tree Implementation Binary node Stores an individual node in a binary tree Consists of data (object to store), references to right child node and left child node (both binary nodes) Binary tree Stores the root node of the tree

BinaryNode and BinaryTree public class BinaryNode{ private Object element; private BinaryNode left; private BinaryNode right; } public class BinaryTree{ private BinaryNode root;

Methods in BinaryNode Two constructors First one takes in element, right node, and left node Second one does not take in any parameters (all are set to null) Setters for element, left, and right Getters for element, left, and right Computing size and height of nodes Tree traversal methods (Preorder, Inorder & Postorder)

Computing Size Recursive method that terminates at leaf nodes public int size(){ if(left == null && right == null) return(1); if(left == null) return(right.size() + 1); if(right == null) return(left.size() + 1); return(left.size() + right.size() + 1); }

Computing Height Recursive method terminating in leaves public int height(){ if(left == null && right == null) return(0); if(left == null) return(right.height() + 1); if(right == null) return(left.height() + 1); if(right.height() => left.height()) return(right.height() + 1); return(left.height() + 1);

Tree Traversals Accessing (or processing) individual nodes of a tree Usually implemented recursively Three kinds of tree traversal Preorder Inorder Postorder Our example considers printing node information

Preorder Traveral Process current node Process left subtree Process right subtree public void printPreOrder(){ System.out.println(element.toString()); if (left != null) left.printPreOrder(); if (right != null) right.printPreOrder(); return; }

printPreOrder Trace A C B E D A B D C E

Inorder Traveral Process left subtree Process current node Process right subtree public void printInOrder(){ if (left != null) left.printPreOrder(); System.out.println(element.toString()); if (right != null) right.printPreOrder(); return; }

printInOrder Trace A C B E D D B A C E

Postorder Traveral Process left subtree Process right subtree Process current node public void printPostOrder(){ if (left != null) left.printPreOrder(); if (right != null) right.printPreOrder(); System.out.println(element.toString()); return; }

printPostOrder Trace A C B E D D B E C A

Methods in BinaryTree Two constructors Setter and getter for root First takes in the element corresponding to root (left and right of root set to null) Second does not take in any parameters (empty tree) Setter and getter for root size () and height () implemented as root.size() and root.height() Traversal methods – Invoking the corresponding methods on root