Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.

Slides:



Advertisements
Similar presentations
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Binary Tree Terminology Linear versus hierarchical data Tree – connected graph with no cycles Child Parent Descendant Sibling Ancestor Leaf vs. internal.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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.
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.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
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.
Chapter 11 A Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 A-2 Terminology A tree consists of vertices and edges, –An edge connects to.
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 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R2. 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.
Tree Data Structures.
Compiled by: Dr. Mohammad Omar Alhawarat
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.
Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
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.
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.
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.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Trees (Unit 7).
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
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.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Trees Another Abstract Data Type (ADT)
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
Csc 2720 Instructor: Zhuojun Duan
Section 8.1 Trees.
ITEC 2620M Introduction to Data Structures
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 Another Abstract Data Type (ADT)
Trees Another Abstract Data Type (ADT)
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Trees, Binary Search Trees
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Binary Tree Properties & Representation
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees

Prof. Amr Goneid, AUC2 Trees General Binary Trees Tree Traversal

Prof. Amr Goneid, AUC3 1. General A Tree is a non-linear, hierarchical one-to- many data structure. A special tree is the Binary Tree abdecfg

Prof. Amr Goneid, AUC4 General Can be implemented using arrays, structs and pointers Used in problems dealing with: Searching Hierarchy Ancestor/descendant relationship Classification

Prof. Amr Goneid, AUC5 Tree Terminology A tree consists of a finite set of nodes (or vertices) and a finite set of edges that connect pairs of nodes. A node is a container for data An edge represents a relationship between two nodes. a node a an edge ba

Prof. Amr Goneid, AUC6 Tree Terminology A non-empty tree has a root node (e.g.node a). Every other node can be reached from the root via a unique path (e.g. a-d-f). a is the parent of nodes b and c because there is an edge going from a down to each. Moreover, b and c are children of a. dagbecf root parent of g child of a

Prof. Amr Goneid, AUC7 Tree Terminology The descendants of a are (b, c, d, and e), nodes that can be reached by paths from a. The ancestors of e are (c and a), nodes found on the path from e to a. Nodes (b, d, and e) are leaf nodes (they have no children). Each of the nodes a and c has at least one child and is an internal node. caebd Siblings

Prof. Amr Goneid, AUC8 Tree Terminology Each node in the tree (except leaves) may have one or more subtrees For a tree with n nodes there are n-1 edges abdecfg

Prof. Amr Goneid, AUC9 General  A Tree of degree m = 2,3,…is a tree in which a parent node has at most m children.  The root is at level (L = 1) and the height h = maximum level Level 1 Level 2 Level 3 A tree with m = 3 and height h = 3

Prof. Amr Goneid, AUC10 General Let n(L) = number of nodes in level (L). A level is full if n(L) = m n(L-1) with n(1) = 1 The above recurrence relation can be solved by successive substitution:

Prof. Amr Goneid, AUC11 General A tree of degree m is FULL if all levels are completely filled. In this case, the Total Number of Nodes (N) is:

Prof. Amr Goneid, AUC12 2.The Binary Tree A binary tree is a tree in which a parent has at most two children. It consists of a root and two disjoint subtrees (left and right). The root is at level (L = 1) and the height h = maximum level The maximum number of nodes in level L is abdecf Level 1 Level 2 Level 3 A binary tree of height h = 3 Left Right

Prof. Amr Goneid, AUC13 The Full Binary Tree A binary tree is full iff the number of nodes in level L is 2 L-1 abdecfg

Prof. Amr Goneid, AUC14 The Full Binary Tree A full binary tree of height h has n nodes, where

Prof. Amr Goneid, AUC15 The Full Binary Tree

Prof. Amr Goneid, AUC16 The Full Binary Tree The cost of search for nodes in level L is L 2 L-1 Hence, the total search cost is

Prof. Amr Goneid, AUC17 The Balanced Tree A balanced binary tree has the property that the heights of the left and right subtrees differ at most by one level. i.e. |h L – h R | ≤ 1 A Full tree is also a balanced tree hLhL hRhR

Prof. Amr Goneid, AUC18 A binary tree is Complete iff the number of Nodes at level 1 <= L <= h-1 is 2 L-1 and leaf nodes at level h occupy the leftmost positions in the tree i.e. all levels are filled except the rightmost of the last level. Complete Binary Tree Missing Leaves

Prof. Amr Goneid, AUC19 Complete Binary Tree A complete binary tree can be efficiently implemented as an array, where a node at index i has children at indexes 2i and 2i+1 and a parent at index i/2 (with one-based indexing). DE BC A ABCDE

Prof. Amr Goneid, AUC20 A binary tree is a recursive structure e.g. it can be defined recursively as: if (not empty tree) 1. It has a root 2. It has a (Left Subtree) 3. It has a (Right Subtree) Recursive structure suggests recursive processing of trees (e.g. Traversal) Binary Tree as a Recursive Structure bc a de f

Prof. Amr Goneid, AUC21 Binary Tree as a Recursive Structure b,d,ec,f a de f a,b,c,d,e,f bc Empty

Prof. Amr Goneid, AUC22 3. Tree Traversal Traversal is to visit every node ( to display, process, …) exactly once It can be done recursively There are 3 different binary tree traversal orders: Pre-Order: Root is visited before its two subtrees In-Order: Root is visited in between its two subtrees Post-Order:Root is visited after its two subtrees

Prof. Amr Goneid, AUC23 Pre-Order Traversal Algorithm: complexity is O(n) PreOrder ( tree ) { if ( not empty tree) { Visit (root); PreOrder (left subtree); PreOrder (right subtree); } The resulting visit order = {a} {b, d, e} {c, f } bc a de f

Prof. Amr Goneid, AUC24 Pre-Order Traversal Pre-Order Traversal is also called Depth-First traversal

Prof. Amr Goneid, AUC25 In-Order Traversal Algorithm: complexity is O(n) InOrder ( tree ) { if ( not empty tree) { InOrder (left subtree); Visit (root); InOrder (right subtree); } The resulting visit order = {d, b, e} {a} {f, c } bc a de f

Prof. Amr Goneid, AUC26 Post-Order Traversal Algorithm: complexity is O(n) PostOrder ( tree ) { if ( not empty tree) { PostOrder (left subtree); PostOrder (right subtree); Visit (root); } The resulting visit order = {d, e, b} {f, c } {a} bc a de f

Prof. Amr Goneid, AUC27 Example: Expression Tree The expression A – B * C + D can be represented as a tree. In-Order traversal gives: A – B * C + D This is the infix representation Pre-Order traversal gives: + - A * B C D This is the prefix representation Post-Order traversal gives: A B C * - D + This is the postfix (RPN) representation - D + A* B C

Prof. Amr Goneid, AUC28 mukundan/dsal/BTree.html Binary Tree Traversal Demo