Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 

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

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 Chapter 7 Trees. 2 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
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. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
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.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
COMP20010: Algorithms and Imperative Programming Lecture 1 Trees.
Tree Data Structures.
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.
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.
Chapter 6 (cont’) Binary Tree. 6.4 Tree Traversal Tree traversal is the process of visiting each node in the tree exactly one time. This definition does.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary 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.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
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.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
ISOM MIS 215 Module 5 – Binary Trees. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
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.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
Chapter 25 Binary Search Trees
Recursive Objects (Part 4)
Binary Search Tree (BST)
Tree.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Binary Tree Chapter 8 (cont’) Part2.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Introduction to Trees Chapter 6 Objectives
Presentation transcript:

Chapter 7 Trees_ Part2 TREES

Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself.  The depth of a node v can also be recursively defined as follows: If v is the root, then the depth of v is 0 Otherwise, the depth of v is one plus the depth of the parent of v.

Depth and Height con.. 3

4  The height of a node v in a tree T is also defined recursively: If v is an external node, then the height of v is 0 Otherwise, the height of v is one plus the maximum height of a child of v.

Depth and Height con.. 5

6

Tree Traversal  Tree traversal is the process of visiting each node in the tree exactly one time.  This definition does not specify the order in which the nodes are visited.  There are three simple ways to traverse a tree. They’re called preorder, inorder, and postorder. The order most commonly used for binary search trees is inorder. 7

Breadth-first Traversal  BFT is visiting each node starting from the highest(or lowest) level and moving down (or up) level by level, visiting nodes on each level from left to right(or from right to left).  So, we will get 4 possibilities.  This traverse an be done using queue: after a node is visited, its children, if any, are placed at the end of the queue, and the node at the beginning of the queue is visited.  All nodes on level n must be visited before visiting any nodes on level n+1 is accomplished. 8

 A top-down, left-to-right breadth first traversal of this tree is: 13, 10, 25, 2, 12, 20, 31, 29 Breadth-first Traversal con.. 9

Depth- First Traversal  DFT proceeds as far as possible to the left(or right), then backs up until the first crossroad, goes one step to the right(or left), and again as far as possible to the left(or right).  Repeat this process until all nodes are visited.  There are some variations of the depth-first traversal.  There are three tasks here: 1. V- visiting a node. 2. L- traversing the left subtree. 3. R- traversing the right subtree. 10

 The number of different orders can be reduce to three traversals where the move is always from left to right and focus on the first column. 1. VLR – preorder tree traversal 2. LVR – inorder tree traversal 3. LRV – postorder tree traversal  The real job is done by the system on the run-time stack (using double recursion)  Remember that visiting a node means doing something to it. Depth- First Traversal con.. 11

12  Given a binary tree having a root, left subtree(LST), right sub tree(RST): Preorder traversal NLR LR LR LR Inorder traversal LNR Postorder traversal LRN Standard Traversals Depth- First Traversal

 In a preorder traversal of a tree T, the root of T is visited first and then the subtrees rooted at its children are traversed recursively. If the tree is ordered, then the subtrees are traversed according to the order of the children. Preorder Traversal 13

Postorder Traversal 14  Another important tree traversal algorithm is the postorder traversal.  This algorithm can be viewed as the opposite of the preorder traversal, because it recursively traverses the subtrees rooted at the children of the root first, and then visits the root.

Preorder Traversal & Postorder Traversal  Preorder Traversal 1- Visit the node. 2- Call itself to traverse the node's left subtree. 3- Call itself to traverse the node's right subtree.  Postorder Traversal 1. Call itself to traverse the node’s left subtree. 2. Call itself to traverse the node’s right subtree. 3. Visit the node. 15

 Traversing the tree using preorder would generate the expression *A+BC. This is called prefix notation.  Note : parentheses are not required.  Traversing the tree using postorder would generate the expression ABC+*. This is called postfix notation.. 16

Preorder (another example) A B E CDF DC BA F E ABCDEF 17

A B E CDF DC BA F E Postorder (another example) CDBFEA 18

Binary Tree 19  A binary tree is an ordered tree with the following properties: 1. Every node has at most two children. 2. Each child node is labeled as being either a left child or a right child. 3. A left child precedes a right child in the ordering of children of a node.

Binary Tree con.. 20  A Recursive Binary Tree Definition Incidentally, we can also define a binary tree in a recursive way such that a binary tree is either empty or consists of: A node r, called the root of T and storing an element A binary tree, called the left subtree of T A binary tree, called the right subtree of T.

The Binary Tree ADT 21 A binary tree is a specialization of a tree that supports three additional accessor methods:  left(v): Return the left child of v; an error condition occurs if v has no left child.  right(v): Return the right child of v; an error condition occurs if v has no right child.  hasLeft(v): Test whether v has a left child.  hasRight(v): Test whether v has a right child.

Properties of Binary Trees 22  In a binary tree, level 0 has at most one node (the root), level 1 has at most two nodes (the children of the root), level 2 has at most four nodes, and so on. In general, level d has at most 2 d nodes.

Properties of Binary Trees con.. 23

Binary tree can be implemented as : 1. A linked list. 2. An Array.  Declare a node as a structure with an information field and two “pointers” fields.  However, it may has problems when deleting and inserting nodes

References: Text book, chapter 7: Trees End Of Chapter 26