Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.

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

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Computer Science 2 Data Structures and Algorithms V section 2 Introduction to Trees Professor: Evan Korth New York University.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
CS 261 – Winter 2010 Trees. Ubiquitous – they are everywhere in CS Probably ranks third among the most used data structure: 1.Vectors and Arrays 2.Lists.
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
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.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
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 
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.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Topic 17 Introduction to Trees
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
Compiled by: Dr. Mohammad Omar Alhawarat
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.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Data Structures TREES.
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.
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.
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.
Data Structures Lakshmish Ramaswamy. Tree Hierarchical data structure Several real-world systems have hierarchical concepts –Physical and biological systems.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
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 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.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
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.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Trees A non-linear implementation for collection classes.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
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 Saurav Karmakar
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Binary Trees.
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Tree.
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Trees 7/14/2009.
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
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Trees.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6

 Definition of Trees  Representing Rooted Tree  Tree Traversal  Preorder Traversal  Postorder Traversal  Level Order Traversal

 Tree:  Set of nodes and edges that connect them  Exactly one path between any 2 nodes  Rooted Tree:  One distinguished node is called the root  Every node C, except root, has one parent P, the first node on path from c to the root. C is P’s child  Root has no parent  A node can have any number of children

 Leaf  Node with no children  Siblings  Nodes with same parent  Ancestors  nodes on path from d to rott, including d, d’s parent, d’s grand parent, … root  Descendant  If A is B’s ancestor, then B is A’s Descendant

 Length of path  Number of edges in path  Depth of node n  Length of path from n to root  Depth of root is zero  Height of node n  Length of path from n to its deepest descendant  Height of any leaf is zero  Height of a tree = Height of the root  Subtree rooted at n  The tree formed by n and its descendants

 G & T  Each node has 3 references stored in a list ▪ Item ▪ Parent ▪ Children  Another Option: Sibling Tree  Siblings are directly linked

Next Sibling Parent Item First Child Class SibTreeNode { Object item ; SibTreeNode parent ; SibTreeNodefirstChild; SibTreeNodenextSibling; } Class SibTreeNode { Object item ; SibTreeNode parent ; SibTreeNodefirstChild; SibTreeNodenextSibling; }

Next Sibling Parent Item First ChildNext Sibling Parent Item First ChildNext Sibling Parent Item First ChildNext Sibling Parent Item First ChildNext Sibling Parent Item First ChildNext Sibling Parent Item First ChildNext Sibling Parent Item First ChildNext Sibling Parent Item First Child

 Rooted Tree  Preorder Traversal  Postorder Traversal  Level Order Traversal  Binary Tree  Inorder Traveral

 Visit each node before recursively visiting its children, left to right A BC DEFGH A A’s First Child B B’s First Child D D has no child, then sibling E E has no child, then sibling F F has no child, no sibling B has no child, no sibling A has no child, then sibling C C’s First Child G G has no child, then sibling H H has no child, no sibling

Class SibTreeNode { public void preorder() { this.visit(); if(firstChild!=null){ firstChild.preorder(); } if(nextSibling!=null){ nextSibling.preorder(); } Class SibTreeNode { public void preorder() { this.visit(); if(firstChild!=null){ firstChild.preorder(); } if(nextSibling!=null){ nextSibling.preorder(); }  Preorder Traversal Realization by Recursion

 Preorder Traversal Realization by Stacks A BC DEFGH A B C A Stack: Visiting Sequence: B D E F DEFC G H GH

 Visit each node’s children (left-to-right) before the node itself A BC DEFGH

 Postorder Traversal Realization by Recursion Class SibTreeNode { public void postorder() { if(firstChild!=null) { firstChild.postorder(); } this.visit(); if(nextSibling!=null) { nextSibling.postorder(); } Class SibTreeNode { public void postorder() { if(firstChild!=null) { firstChild.postorder(); } this.visit(); if(nextSibling!=null) { nextSibling.postorder(); }

 Visit root, then all (height-1) nodes, then all (height-2) nodes, … etc. A BC DEFGH

 Level Order Traversal Realization by Queues Queue: A A BC DEFGH B C D E F G H A Visiting Sequence: BDEFCGH

 A Binary Tree  No node has > 2 children  Every child is either left child or a right child, even if it is the only child

 Binary Tree Node Right Child Parent Item Left Child Class BiTreeNode { Object item ; BiTreeNode parent ; BiTreeNode leftChild; BiTreeNode rightChild; } Class BiTreeNode { Object item ; BiTreeNode parent ; BiTreeNode leftChild; BiTreeNode rightChild; }

Right Child Parent Item Left ChildRight Child Parent Item Left ChildRight Child Parent Item Left ChildRight Child Parent Item Left ChildRight Child Parent Item Left ChildRight Child Parent Item Left Child

 Visit left child, then node, then right child Class BiTreeNode { public void inorder() { if(leftChild!=null){ leftChild.inorder(); } this.visit(); if(rightChild!=null) { rightChild.inorder(); } Class BiTreeNode { public void inorder() { if(leftChild!=null){ leftChild.inorder(); } this.visit(); if(rightChild!=null) { rightChild.inorder(); }

 Visualization of inorder traversal A BC DEF