Disusun Oleh : Budi Arifitama Pertemuan ke-8. Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II Chapter 10 Trees I. 2 Topics Terminology.
Advertisements

TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
EC-211 DATA STRUCTURES LECTURE Tree Data Structure Introduction –The Data Organizations Presented Earlier are Linear in That Items are One After.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
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.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
COSC2007 Data Structures II
Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical.
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.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
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.
Topic 17 Introduction to Trees
درختها Trees ساختمان داده ها والگوريتمها مقايسه ليست خطي و درخت Linear lists are useful for serially ordered data. – (e 0, e 1, e 2, …, e n-1 ) – Days.
Compiled by: Dr. Mohammad Omar Alhawarat
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
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.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Data Structures TREES.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
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.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
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.
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.
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 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
The Tree ADT.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Fundamentals of Programming II Introduction to Trees
Csc 2720 Instructor: Zhuojun Duan
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.
Data Structures & Algorithm Design
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.
Trees.
Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Trees, Binary Search Trees
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Binary Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Disusun Oleh : Budi Arifitama Pertemuan ke-8

Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss Binary Tree Examine a binary tree example

 Tree Introduction  Term Associated With Tree  Binary Tree  Traverse Tree

A tree is a nonlinear data structure used to represent entities that are in some hierarchical relationship Examples in real life: Family tree Table of contents of a book Class inheritance hierarchy in Java Computer file system (folders and subfolders) Decision trees Top-down design 10-4

10-5 Root directory of C drive Documents and SettingsProgram FilesMy Music DesktopFavoritesStart MenuMicrosoft OfficeAdobe

Tree: a set of elements of the same type such that It is empty Or, it has a distinguished element called the root from which descend zero or more trees (subtrees) What kind of definition is this? What is the base case? What is the recursive part? 10-7

10-8 Leaf nodes Root Interior nodes

Nodes: the elements in the tree Edges: connections between nodes Root: the distinguished element that is the origin of the tree There is only one root node in a tree Leaf node: a node without an edge to another node Interior node: a node that is not a leaf node Empty tree has no nodes and no edges 10-9

Parent or predecessor: the node directly above in the hierarchy A node can have only one parent Child or successor: a node directly below in the hierarchy Siblings: nodes that have the same parent Ancestors of a node: its parent, the parent of its parent, etc. Descendants of a node: its children, the children of its children, etc Tree Terminology

Does a leaf node have any children? Does the root node have a parent? How many parents does every node other than the root node have? 10-11

A path is a sequence of edges leading from one node to another Length of a path: number of edges on the path Height of a (non-empty) tree : length of the longest path from the root to a leaf What is the height of a tree that has only a root node? By convention, the height of an empty tree is

Level of a node : number of edges between root and node It can be defined recursively: Level of root node is 0 Level of a node that is not the root node is level of its parent + 1 Question: What is the level of a node in terms of path length? Question: What is the height of a tree in terms of levels? 10-13

10-14 Level 0 Level 1 Level 2 Level 3

Subtree of a node: consists of a child node and all its descendants A subtree is itself a tree A node may have many subtrees 10-15

10-16 Subtrees of the root node

10-17 Subtrees of the node labeled E E

Degree or arity of a node: the number of children it has Degree or arity of a tree: the maximum of the degrees of the tree’s nodes 10-18

Ancestor (F)? Predesesor (F) ? Descendant (B)? Succesor (B)? Parent (I)? Child (C)? Sibling (G)? Size? Height? Root? Leaf? Degree (C)?

Gambarkan tree dari representasi berikut : D  F D  B K  J K  L B  A B  C H  D H  K F  E F  G J  I Tentukan : 1.Root 2.Leaf 3.Heigth 4.Child H 5.Parent A

 Finite (possibly empty) collection of elements.  A nonempty binary tree has a root element.  The remaining elements (if any) are partitioned into two binary trees.  These are called the left and right subtrees of the binary tree.

23  There are many variations on trees but we will start with binary trees  binary tree: each node has at most two children  the possible children are usually referred to as the left child and the right child parent left child right child

24  full binary tree: a binary tree is which each node was exactly 2 or 0 children

 What is the maximum height of a full binary tree with 11 nodes? A. 1 B. 3 C. 5 D. 7 E. Not possible to construct a full binary tree with 11 nodes. CS314 Binary Trees 25

26  complete binary tree: a binary tree in which every level, except possibly the deepest is completely filled. At depth n, the height of the tree, all nodes are as far left as possible Where would the next node go to maintain a complete tree? Binary Trees CS314

27  perfect binary tree: a binary tree with all leaf nodes at the same depth. All internal nodes have exactly two children.  a perfect binary tree has the maximum number of nodes for a given height  a perfect binary tree has (2 (n+1) - 1) nodes where n is the height of the tree  height = 0 -> 1 node  height = 1 -> 3 nodes  height = 2 -> 7 nodes  height = 3 -> 15 nodes Binary Trees CS314

 No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a tree.  A binary tree may be empty; a tree cannot be empty.

 The subtrees of a binary tree are ordered; those of a tree are not ordered. a b a b Are different when viewed as binary trees. Are the same when viewed as trees.

 Representasi ekspresi arithmatik

 Minimum number of nodes in a binary tree whose height is h.  At least one node at each of first h levels. minimum number of nodes is h

 All possible nodes at first h levels are present. Maximum number of nodes = … + 2 h-1 = 2 h - 1

 A binary tree can bee represented by an Array or a linked list.

HDKBFJLACEGI Array

H KD B A L J I leftChild elementrightChild root F C E G

tree[] 510 abcdefghij b a c d e f g hi j

a b 1 3 c 7 d 15

a b 1 3 c 7 d tree[] 0510 a-b---c d

 Penelusuran seluruh node pada binary tree.  Metode :  Preorder  Inorder  Postorder  Level order

 Preorder traversal 1. Cetak data pada root 2. Secara rekursif mencetak seluruh data pada subpohon kiri 3. Secara rekursif mencetak seluruh data pada subpohon kanan

a bc a b c

a bc d e f g hi j abdgheicfj

+ ab - c d + ef * / Gives prefix form of expression! /*+ab-cd+ef

 Inorder traversal 1. Secara rekursif mencetak seluruh data pada subpohon kiri 2. Cetak data pada root 3. Secara rekursif mencetak seluruh data pada subpohon kanan

a bc bac

a bc d e f g hi j gdhbeiafjc

 Postorder traversal 1. Secara rekursif mencetak seluruh data pada subpohon kiri 2. Secara rekursif mencetak seluruh data pada subpohon kanan 3. Cetak data pada root

a bc bca

a bc d e f g hi j ghdiebjfca

+ ab - cd + e f * / Gives postfix form of expression! ab+cd-*ef+/

 Telusuri pohon biner berikut dengan menggunakan metode pre, in, post, dan level traversal.

a.b.