COSC2100: Data Structures and Algorithms

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 CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Trees1 More on Trees University Fac. of Sci. & Eng. Bus. School Law School CS Dept. EE Dept. Math. Dept.
CS 171: Introduction to Computer Science II
Trees Briana B. Morrison Adapted from Alan Eugenio.
© 2004 Goodrich, Tamassia Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
© 2004 Goodrich, Tamassia Trees1 this is a tree. © 2004 Goodrich, Tamassia Trees2 What is a Tree? In computer science, a tree is an abstract model of.
Trees COMP53 Oct 31, What is a Tree? A tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child relation.
Trees1 Part-C Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
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.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Trees.
Make Money Fast! Stock Fraud Apply shortcuts Bank Robbery Just For Laughs Trees This one is cool eITnotes.com For more notes and topics visit:
Trees CSCI 3333 Data Structures. Acknowledgement  Dr. Bun Yue  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich  Dr.
Chapter 7:Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data Structures.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2010 Goodrich, Tamassia.
Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery © 2013 Goodrich, Tamassia, Goldwasser.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
CH 7. TREES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CH 7 : TREE ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
© 2004 Goodrich, Tamassia Trees1 Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
1 COMP9024: Data Structures and Algorithms Week Five: Trees Hui Wu Session 1, 2015
Trees. Trees: – A trunk from the roots – Divides into branches – Ends in leaves.
Trees 5/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Trees 5/10/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Data Structure and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Fundamentals of Programming II Introduction to Trees
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery Trees
Week 6 - Wednesday CS221.
Trees 二○一八年八月二十六日 Part-C Trees Trees.
Trees.
Tree.
Trees Chuan-Ming Liu Computer Science & Information Engineering
Section 8.1 Trees.
Data Structures & Algorithm Design
© 2013 Goodrich, Tamassia, Goldwasser
Tonga Institute of Higher Education
Binary Trees, Binary Search Trees
Trees 9/21/2018 9:58 PM Trees this is a tree Trees.
CS223 Advanced Data Structures and Algorithms
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Trees.
Week nine-ten: Trees Trees.
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery Trees
Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb CLRS, Section 10.4.
Trees Palestine Gaza West Bank 48 Stolen Land Trees Trees
Binary Trees, Binary Search Trees
Trees.
CS210- Lecture 9 June 20, 2005 Announcements
Chapter 20: Binary Trees.
Trees Make Money Fast! Stock Fraud Ponzi Scheme Bank Robbery
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Trees.
Presentation transcript:

COSC2100: Data Structures and Algorithms Tree Basics and Binary Tree

Review So far, we have learnt two fundamental data storage structures Array Linked List An ordered array Search a key in the array: O(lgN) with binary search Insert or delete a new key: O(N) worst case Linked list Search a key in the list: O(N) worst case Insert a key: O(1)

Overview We are going to learn another fundamental data storage structure Binary Tree With a special binary tree, Balanced Binary Search Tree Search: O(lgN) Insert and Delete: O(lgN) The basics about trees Terminology Tree traversals

Tree Structure in Real Life College College of Art and Science Departments BSCI MSCS CHEM Course types MATH COSC COSC courses COSC1000 COSC2100 COSC3100 Other examples: Family tree Table of contents Company organization chart

A file system that stores documentations for several courses Trees 11/10/2018 5:42 PM Trees in Computers /usr/rt/courses cosc1020 cosc2100 cosc3100 programs homework lectures A file system that stores documentations for several courses Trees

Tree Data Structure A tree consists of nodes connected by edges Trees 11/10/2018 5:42 PM Tree Data Structure A tree consists of nodes connected by edges Nodes are represented as circles in the graph Edges are lines in the graph Trees are hierarchical structures B C A D E G H X Y Z Trees

Tree Terminology I Root: the node at the top of the tree Trees 11/10/2018 5:42 PM Tree Terminology I A B D C G H E F I J K Root: the node at the top of the tree Node A Edge: directed connection from a node’s parent to the node Edges from A to B, from B to F, from F to K Parent: the node to which the given node is connected via the upward edge B’s parent: A A’s parent: null Child: the node to which the given node is connected via downward edges A’s child: B, C, D C’s child: G, H Trees

Tree Terminology II Internal node: node with at least one child Trees 11/10/2018 5:42 PM Tree Terminology II Internal node: node with at least one child Nodes A, B, C, F leaf node node without children Nodes E, I, J, K, G, H, D Siblings of a node The nodes with the same parent I’s siblings: J and K Subtree (CGH): tree consisting of a node and its children, children’s children… Root of the subtree: C Edge from A (C’s parent) to C subtree A B D C G H E F I J K Trees

Trees 11/10/2018 5:42 PM Tree Terminology III Path: a path form node n1 to nk is defined as a sequence of nodes n1, n2, …, nk such that ni is the parent of ni+1 for 1 <= i < k Length: the length of this path is the number of edges on the path The depth of a node ni is the length of path form the root to ni. The depth of the root is 0 The depth of the tree is the largest depth among leaves. The height of ni is the length of the longest path from ni to a leaf. All leaves are at height 0. The height of a tree is the height of the root. Trees

Examples Exercises: Parent of J: children of J: B D C G H E F I J K Exercises: Parent of J: children of J: Path from A to F and its length: Height and depth of F:

More Exercises Which node is the root? Which nodes are leaves? The tree’s depth? For node E, list its parent children siblings height depth B C D E F G H I J K L M

Yet More Exercises Which node is the root? Which nodes are leaves? The tree’s depth? For node A, list its parent children siblings height depth A

Trees 11/10/2018 5:42 PM Binary Trees A A binary tree is a tree in which every node has at most two children left child and right child A node in a binary tree doesn’t necessarily have two children only a left child only a right child or no children at all B C D E G H I Trees

Representing Tree in Java Code Trees 11/10/2018 5:42 PM Representing Tree in Java Code class Node { int iData; Node left; Node right; //constructor //display } class Tree { private Node root; //constructor //methods for insertion, deletion… } Node is the building blocks for Tree, just as Link for LinkList Node root is the only entry to a Tree object, as Link first to a LinkList object Trees

Traversing a Tree To traverse a tree means to visit all the nodes in some specified order Inorder traversal Preorder traversal Postorder traversal

Trees 11/10/2018 5:42 PM Inorder Traversal Algorithm inOrder(v) If v==null Return inOrder (left (v)) System.out.print(v.iData + “ “) inOrder (right (v)) A node is visited after its left subtree and before its right subtree What is printed for the given tree if executing inOrder(root) 6 2 8 1 4 7 9 3 5 Trees

Preorder Traversal A node is visited before its children Trees 11/10/2018 5:42 PM Preorder Traversal A node is visited before its children If visiting a node is printing the string, what will be printed for the given tree? Algorithm preOrder(v) visit(v) for each child w of v preorder (w) 1 Make Money Fast! 2 5 1. Motivations 2. Methods 7 6 3 4 2.1 Stock Fraud 2.3 Bank Robbery 1.1 Greed 1.2 Avidity Trees

Postorder Traversal A node is visited after its descendants Trees 11/10/2018 5:42 PM Postorder Traversal A node is visited after its descendants Application: compute space used by files in a directory and its subdirectories Demonstrate how it works. Algorithm postOrder(v) for each child w of v postOrder (w) visit(v) 7 cs16/ 3 6 homeworks/ programs/ 1 2 4 5 h1c.doc 3K h1nc.doc 2K DDR.java 10K Robot.java 20K Trees

Pop Quiz A By visiting the node, we print the character stored in the node Show the result of Preorder Inorder Postorder of the given tree. B C D E F G H I J K L M

Summary Tree Terminologies Tree traversals Hierarchical data structure Root Internal, external (leaf) nodes Parent Depth Height Path Tree traversals Preorder, inorder, and postorder