Compiled by: Dr. Mohammad Omar Alhawarat

Slides:



Advertisements
Similar presentations
Introduction to Trees Chapter 6 Objectives
Advertisements

Data Structures: A Pseudocode Approach with C 1 Chapter 6 Objectives Upon completion you will be able to: Understand and use basic tree terminology and.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
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 & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; } 
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
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.
Data Structures TREES.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
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.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
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.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary 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.
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 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
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.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCE 210 Data Structures and Algorithms
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
CMSC 341 Introduction to Trees.
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
Introduction to Trees IT12112 Lecture 05.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Compiled by: Dr. Mohammad Omar Alhawarat Chapter 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees

Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root node) to every other node in the tree

Hierarchical Organization Example: File Directories

Tree Terminology Nodes at a given level are children of nodes of previous level Node with children is the parent node of those children Nodes with same parent are siblings Node with no children is a leaf node The only node with no parent is the root node All others have one parent each

Example of a Tree root A C B D G E F

Example of a Tree In a tree, every pair of linked nodes have a parent-child relationship (the parent is closer to the root) root A C B D G E F

Example of a Tree (Cont.) For example, C is a parent of G root A C B D G E F

Example of a Tree (Cont.) E and F are children of D root A C B D G E F

Example of a Tree (Cont.) The root node is the only node that has no parent. root A C B D G E F

Example of a Tree (Cont.) Leaf nodes (or leaves for short) have no children. root A C B D G E F

Subtrees root A subtree is a part of a tree that is a tree in itself A C I K D E F J G H subtree

Subtrees (Cont.) root It normally includes each node reachable from its root. A B C I K D E F J G H subtree

Subtrees (Cont.) root Even though this looks like a subtree in itself… D E F J G H

Binary Trees A binary tree is a tree in which each node can only have up to two children…

Not a Binary Tree root A B C I K D E F J G H

Example of a Binary Tree root A The links in a tree are often called edges B C I K E J G H

Levels The level of a node is the number of edges in the path from the root node to this node root level 0 A level 1 B C level 2 I K E level 3 J G H

Full Binary Tree root A B C D E F G H I J K L M N O In a full binary tree, each node has two children except for the nodes on the last level, which are leaf nodes

Complete Binary Trees A complete binary tree is a binary tree that is either a full binary tree OR a tree that would be a full binary tree but it is missing the rightmost nodes on the last level

Complete Binary Trees (Cont.) root A B C D E F G H I

Complete Binary Trees (Cont.) root A B C D E F G H I J K L

Full Binary Trees A full binary tree is also a complete binary tree. root A B C D E F G H I J K L M N O

Full Binary Trees (Cont.) The formula for the maximum number of nodes is derived from the fact that each node can have only two descendants. Given a height of the binary tree, H, the maximum number of nodes in the full binary tree is given as follows:

Balanced Binary Trees To Assure that a binary tree is Balanced one the following algorithms is used: AVL Trees. Red-Black Trees. 2-3 Trees and the general case (B-Trees) B-Trees are used with Fetching data from Large Databases.

Binary Trees A binary tree is either empty or has the following form Where Tleft and Tright are binary trees

Binary Trees Every nonleaf in a full binary tree has exactly two children A complete binary tree is full to its next-to-last level Leaves on last level filled from left to right The height of a binary tree with n nodes that is either complete or full is log2(n + 1)

Binary Trees

Recursive definition of a tree A tree is a set of nodes that either: is empty or has a designated node, called the root, from which hierarchically descend zero or more subtrees, which are also trees.

Recursive definition of a tree (Cont.)

Tree Traversal Four meaningful orders in which to traverse a binary tree. Preorder Inorder Postorder Level order

Tree Traversal (Cont.)

Tree Traversal (Cont.) In Preorder, the root is visited before (pre) the subtrees traversals In Inorder, the root is visited in-between left and right subtree traversal In Postorder, the root is visited after (post) Preorder Traversal: Visit the root Traverse left subtree Traverse right subtree Inorder Traversal: Traverse left subtree Visit the root Traverse right subtree Postorder Traversal: Traverse left subtree Traverse right subtree Visit the root

Tree Traversal (Cont.) Visiting a node Processing the data within a node This is the action performed on each node during traversal of a tree A traversal can pass through a node without visiting it at that moment For a binary tree Visit the root Visit all nodes in the root's left subtree Visit all nodes in the root's right subtree

Tree Traversal (Cont.) Preorder traversal: visit root before the subtrees

Tree Traversal (Cont.) Inorder traversal: visit root between visiting the subtrees

Tree Traversal (Cont.) Postorder traversal: visit root after visiting the subtrees These are examples of a depth-first traversal.

Tree Traversal (Cont.) Level-order traversal: begin at the root, visit nodes one level at a time This is an example of a breadth-first traversal.

Tree Traversals – Example 1

Tree Traversals – Example 2 Assume: visiting a node is printing its label Preorder: 1 3 5 4 6 7 8 9 10 11 12 Inorder: 4 5 6 3 1 8 7 9 11 10 12 Postorder: 4 6 5 3 8 11 12 10 9 7 1 1 3 11 9 8 4 6 5 7 12 10

Tree Traversals – Example 3 Assume: visiting a node is printing its data Preorder: 15 8 2 6 3 7 11 10 12 14 20 27 22 30 Inorder: 2 3 6 7 8 10 11 12 14 15 20 22 27 30 Postorder: 3 7 6 2 10 14 12 11 8 22 30 27 20 15 6 15 8 2 3 7 11 10 14 12 20 27 22 30

Tree Traversals – Example 4

Examples of Binary Trees Expression Trees

Example: Expression Trees Cpt S 223 Example: Expression Trees Store expressions in a binary tree Leaves of tree are operands (e.g., constants, variables) Other internal nodes are unary or binary operators Used by compilers to parse and evaluate expressions Arithmetic, logic, etc. E.g., (a + b * c)+((d * e + f) * g) 43 43 43 Washington State University

Tree Traversal - Summary Level order traversal is sometimes called breadth- first. The other traversals are called depth-first. Traversal takes Θ(n) in both breadth-first and depth- first. Memory usage in a perfect tree is Θ(log n) in depth- first and Θ(n) in breadth-first traversal.

Questions? Questions ?