Objectives. In this session, you will learn to:

Similar documents
Chapter 20: Binary Trees

TREES. Trees - Introduction

Trees and Tree Traversals. Binary Trees. COMP 210: Object-Oriented Programming Lecture Notes 8. Based on notes by Logan Mayfield

Trees. (Trees) Data Structures and Programming Spring / 28

Binary Trees, Binary Search Trees

Programming II (CS300)

Data Structures - Binary Trees and Operations on Binary Trees

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

CE 221 Data Structures and Algorithms

Tree Structures. A hierarchical data structure whose point of entry is the root node

Each element of a binary tree is called a node of the tree. The following figure shows a binary tree with 9 nodes where A is the root.

Binary Trees

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

March 20/2003 Jayakanth Srinivasan,

CSI33 Data Structures

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Binary Trees. Height 1

CSE 230 Intermediate Programming in C and C++ Binary Tree

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

Binary Trees. Reading: Lewis & Chase 12.1, 12.3 Eck Programming Course CL I

Trees. CSE 373 Data Structures

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Unit III - Tree TREES

CSC148 Week 6. Larry Zhang

Why Do We Need Trees?

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1.

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Associate Professor Dr. Raed Ibraheem Hamed

Trees. Tree Structure Binary Tree Tree Traversals

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Graphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

Topic 14. The BinaryTree ADT

Trees. Trees. CSE 2011 Winter 2007

CSI33 Data Structures

[ DATA STRUCTURES ] Fig. (1) : A Tree

BBM 201 Data structures

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Data Structures and Algorithms for Engineers

Introduction to Binary Trees

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Trees. Truong Tuan Anh CSE-HCMUT

Binary Trees Fall 2018 Margaret Reid-Miller

Data Structures and Algorithms

ITEC2620 Introduction to Data Structures

Data Structure - Binary Tree 1 -

CS24 Week 8 Lecture 1

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

COSC 2011 Section N. Trees: Terminology and Basic Properties

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

INF2220: algorithms and data structures Series 1

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

CSE 214 Computer Science II Introduction to Tree

A Hierarchical Structure. Lecture11: Tree I. Tree Data Structures. Unix/Linux file systems. Bohyung Han CSE, POSTECH

Trees, Binary Trees, and Binary Search Trees

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

tree nonlinear Examples

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Algorithms and Data Structures

Successor/Predecessor Rules in Binary Trees

Garbage Collection: recycling unused memory

Principles of Computer Science

CSC148H Week 8. Sadia Sharmin. July 12, /29

Introduction to Computers and Programming. Concept Question

Cpt S 122 Data Structures. Data Structures Trees

INF2220: algorithms and data structures Series 1

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Data Structures And Algorithms

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Binary Trees. Examples:

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Trees 3/19/14. Mammal. Dog Pig Cat

Data Structures - Binary Trees 1

A Hierarchical Structure. Lecture11: Trees I. Tree Data Structures. Unix/Linux file systems. Bohyung Han CSE, POSTECH

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

Stacks, Queues and Hierarchical Collections

IX. Binary Trees (Chapter 10)

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR


Binary Trees and Binary Search Trees

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

CS 206 Introduction to Computer Science II

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

Chapter Contents. Trees. Tree Concepts. Hierarchical Organization. Hierarchical Organization. Hierarchical Organization.

Trees. Estruturas de Dados / Programação 2 Árvores. Márcio Ribeiro twitter.com/marciomribeiro. Introduc)on. Hierarchical structure

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Transcription:

TRS

Objectives n this session, you will learn to: Store data in a tree istinguish types of inary tree Traverse a inary Tree norder PreOrder PostOrder onstruct a inary Tree onstruct an expression inary tree

Storing ata in a Tree onsider a scenario where you are required to represent the directory structure of your operating system. The directory structure contains various folders and files. folder may further contain any number of sub folders and files. n such a case, it is not possible to represent the structure linearly because all the items have a hierarchical relationship among themselves. n such a case, it would be good if you have a data structure that enables you to store your data in a nonlinear fashion. irectory structure:

efining Trees tree is a nonlinear data structure that represent a hierarchical relationship among the various data elements. Trees are used in applications in which the relation between data elements needs to be represented in a hierarchy. K J L M

efining Trees (ontd.) ach element in a tree is referred to as a node. The topmost node in a tree is called root. root K J L M node

efining Trees (ontd.) ach node in a tree can further have subtrees below its hierarchy. root K J L M node

Tree Terminology Let us discuss various terms that are most frequently used with trees. Leaf node: t refers to a node with no children. Nodes,,,,, J, L, and M are leaf nodes. K J L M

Tree Terminology (ontd.) Subtree: portion of a tree, which can be viewed as a separate tree in itself is called a subtree. subtree can also contain just one node called the root node. hildren of a node: The roots of the subtrees of a node are called the children of the node. Tree with root, containing nodes,,, and is a subtree of node.,,, and are children of node. is the parent of these nodes. K J L M

Tree Terminology (ontd.) dge: link from the parent to a child node is referred to as an edge. egree of a node: t refers to the number of subtrees of a node in a tree. egree of node is 1 egree of node is 2 egree of node is 3 egree of node is 4 dge K J L M

Tree Terminology (ontd.) Siblings/rothers: t refers to the children of the same node. Nodes,, and are siblings of each other. Nodes,,, and are siblings of each other. K J L M

Tree Terminology (ontd.) nternal node: t refers to any node between the root and a leaf node. Level of a node: t refers to the distance (in number of nodes) of a node from the root. Root always lies at level 0. s you move down the tree, the level increases by one. Level 0 Nodes,,, and K are internal nodes. Level 1 K J L Level 2 M Level 3

Tree Terminology (ontd.) epth of a tree: Refers to the total number of levels in the tree. The depth of the following tree is 4. Level 0 Level 1 K J L Level 2 M Level 3

Just a minute onsider the following tree and answer the questions that follow: a. What is the depth of the tree? b. Which nodes are children of node? c. Which node is the parent of node? d. What is the level of node? e. Which nodes are the siblings of node? f. Which nodes are the siblings of node? g. Which nodes are leaf nodes? root nswer: a. 4 b. and c. d. 2 e. does not have any siblings f. The only sibling of is g.,,, and

efining inary Trees inary tree is a specific type of tree in which each node can have at most two children namely left child and right child. There are various types of binary trees: Strictly binary tree ull binary tree omplete binary tree Strictly binary tree: binary tree in which every node, except for the leaf nodes, has non-empty left and right children.

efining inary Trees (ontd.) ull binary tree: binary tree of depth d that contains exactly 2d 1 nodes. epth = 3 Total number of nodes = 23 1 = 7

efining inary Trees (ontd.) omplete binary tree: binary tree with n nodes and depth d whose nodes correspond to the nodes numbered from 0 to n 1 in the full binary tree of depth k. 0 1 3 2 4 0 5 1 6 ull inary Tree 3 2 4 0 5 1 3 omplete inary Tree 2 4 5 ncomplete inary Tree

Representing a inary Tree rray representation of binary trees: ll the nodes are represented as the elements of an array. 0 3 1 2 4 5 6 inary Tree [0] [1] [2] [3] [4] [5] [6] f there are n nodes in a binary tree, then for any node with index i, where 0 < i < n 1: Parent of i is at (i 1)/2. Left child of i is at 2i + 1: f 2i + 1 > n 1, then the node does not have a left child. Right child of i is at 2i + 2: rray Representation f 2i + 2 > n 1, then the node does have a right child.

Representing a inary Tree (ontd.) Linked representation of a binary tree: t uses a linked list to implement a binary tree. ach node in the linked representation holds the following information: ata Reference to the left child Reference to the right child f a node does not have a left child or a right child or both, the respective left or right child fields of that node point to NULL. ata Node

Representing a inary Tree (ontd.) root root. 52 36 24. 68 59 24 72 70 inary Tree 80 52.. 36 68.. 59. 72. 70 Linked Representation 80

inary Tree Node Struct node { nt info; Struct node *left,*right; }

OPRTONS ON TRS Traversing a inary Tree 1)TRVRSN You can implement various operations on a binary tree. common operation on a binary tree is traversal. Traversal refers to the process of visiting all the nodes of a binary tree once. There are three ways for traversing a binary tree: norder traversal Preorder traversal Postorder traversal

NORR TRVRSL Steps for traversing a tree in inorder sequence are as follows: 1. Traverse the left subtree 2. Visit root 3. Traverse the right subtree Let us consider an example.

norder Traversal (ontd.) is is not not NULL. NULL. The left subtree of node to traverse the left Therefore, move to node. subtree of. root

norder Traversal (ontd.) The left subtree of node is NULL. Therefore, visit node. root

norder Traversal (ontd.) Right subtree of is not NULL Therefore, move to the right subtree of node root

norder Traversal (ontd.) Left subtree of is empty. Therefore, visit node. root

norder Traversal (ontd.) Right subtree of is empty. Therefore, move to node. root

norder Traversal (ontd.) The left subtree of has been visited. Therefore, visit node. root

norder Traversal (ontd.) Right subtree of is not empty. Therefore, move to the right subtree of. root

norder Traversal (ontd.) Left subtree of is empty. Therefore, visit node. root

norder Traversal (ontd.) Right subtree of is empty. Therefore, move to node. root

norder Traversal (ontd.) Left subtree of has been visited. Therefore, visit node. root

norder Traversal (ontd.) Right subtree of is not empty. Therefore, move to the right subtree of. root

norder Traversal (ontd.) Left subtree of is not empty. Therefore, move to the left subtree of. root

norder Traversal (ontd.) Left subtree of is empty. Therefore, visit node. root

norder Traversal (ontd.) Right subtree of is empty. Therefore, move to node. root

norder Traversal (ontd.) The left subtree of node has been visited. Therefore, visit node. root

norder Traversal (ontd.) Right subtree of is not empty. Therefore, move to the right subtree of node. root

norder Traversal (ontd.) Left subtree of is not empty. Therefore, move to the left subtree of node. root

norder Traversal (ontd.) Left subtree of is empty. Therefore, visit. root

norder Traversal (ontd.) Right subtree of is empty. Therefore, move to node. root

norder Traversal (ontd.) Visit node. root

norder Traversal (ontd.) Right subtree of is empty. root Traversal complete

LORTM LORTM NORRTRVRS() { 1. set top=0, stack[top]=null, ptr = root 2. Repeat while ptr!=null 2.1 set top=top+1 2.2 set stack[top]=ptr 2.3 set ptr=ptr->left 3. Set ptr=stack[top], top=top-1 4. Repeat while ptr!=null 4.1 print ptr->info 4.2 if ptr->right!=null then 4.2.1set ptr=ptr->right 4.2.2 goto step 2 4.3 Set ptr=stack[top], top=top-1 }

PRORR TRVRSL Steps for traversing a tree in preorder sequence are as follows: 1. Visit root 2. Traverse the left subtree 3. Traverse the right subtree

Preorder Traversal (ontd.) Perform the preorder traversal of the following tree. Preorder Traversal:

LORTM LORTM PRORRTRVRS() { 1. set top=0, stack[top]=null, ptr = root 2. Repeat while ptr!=null 2.1 print ptr -> info 2.2 if (ptr -> right!= NULL) 2.2.1 top = top +1 2.2.2 set stack [ top] = ptr -> right 2.3 if ( ptr -> left!= NULL) 2.3.1 ptr=ptr -> left else 2.3.1 ptr=stack[top], top=top-1 }

Postorder Traversal Steps for traversing a tree in postorder sequence are as follows: 1. Traverse the left subtree 2. Traverse the right subtree 3. Visit the root

Postorder Traversal (ontd.) Perform the postorder traversal of the following tree. Postorder Traversal:

LORTM LORTM POSTORRTRVRS() { 1. set top = 0, stack [top] = NULL, ptr = root 2. Repeat while ptr!=null 2.1 top = top +1, stack [ top ] = ptr 2.2 if (ptr -> right!= NULL) 2.2.1 top = top +1 2.2.2 set stack [ top] = - ( ptr -> right ) 2.3 ptr = ptr -> left 3. ptr = stack [top], top = top-1 4. Repeat while ( ptr > 0 ) 4.1 print ptr -> info 4.2 ptr = stack [top], top = top-1 5. if (ptr < 0) 5.1 set ptr = - ptr 5.2 o to step 2 }

Recursive Traversal mplementation

Just a minute n traversal method, root is processed before traversing the left and right subtrees. nswer: Preorder

xpression inary Tree Traversal f an expression is represented as a binary tree, the inorder traversal of the tree gives us an infix expression, whereas the postorder traversal gives us a postfix expression as shown in igure.

onstruction of inary Tree iven an order of traversal of a tree, it is possible to construct a tree; for example, consider the folowing order: norder = We can construct the binary trees shown in igure by using this order of traversal

onstruction of inary Tree Therefore, we conclude that given only one order of traversal of a tree, it is possible to construct a number of binary trees; a unique binary tree cannot be constructed with only one order of traversal. or construction of a unique binary tree, we require two orders, in which one has to be inorder; the other can be preorder or postorder. or example, consider the following orders: norder = Postorder =

onstruction of inary Tree norder = Postorder = We can construct the unique binary tree shown in igure by using these orders of traversal: unique binary tree constructed using its inorder and postorder.

Just a minute onstruct the binary tree with the following: norder:- Postorder:-

Postorder Traversal (ontd.)

Summary n this session, you learned that: tree is a nonlinear data structure that represents a hierarchical relationship among the various data elements. binary tree is a specific type of tree in which each node can have a maximum of two children. inary trees can be implemented by using arrays as well as linked lists, depending upon requirement. Traversal of a tree is the process of visiting all the nodes of the tree once. There are three types of traversals, namely inorder, preorder, and postorder traversal.