Trees (Unit 7).

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

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Introduction to Trees Chapter 6 Objectives
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Tree Terminology Linear versus hierarchical data Tree – connected graph with no cycles Child Parent Descendant Sibling Ancestor Leaf vs. internal.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
© 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.
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.
Trees CS212 & CS-240 D.J. Foreman. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called.
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 
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.
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.
Tree Data Structures. Introductory Examples Willliam Willliam BillMary Curt Marjorie Richard Anne Data organization such that items of information are.
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.
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.
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.
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.
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.
Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
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.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
TREE Ahsan Ahmed College of Computer and Information Science Majma’ah University 1.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Tree.
Section 8.1 Trees.
Tree traversal from Introduction to Trees Chapter 6 Objectives
Lecture 18. Basics and types of Trees
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
Data Structures: Trees and Binary Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Trees, Binary Search Trees
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Tree.
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
8.2 Tree Traversals Chapter 8 - Trees.
Introduction to Trees Chapter 6 Objectives
Trees.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Trees (Unit 7)

Outline Trees: Definition and Basic Terminologies Representation of Trees Binary Trees: Basic Terminologies and Types Representation of Binary Trees Array representation of binary trees Linked representation of binary trees Binary Tree Traversals Inorder traversal Postorder traversal Preorder traversal Threaded Binary Trees Applications

A tree is defined as a finite set of one or more nodes such that There is a specially designated node called the root and The rest of the nodes could be partitioned into t disjoint sets (t >0) each set representing a tree Ti, i=1,2, . . . t known as subtree of the tree. A node in the definition of the tree represents an item of information, and the links between the nodes termed as branches, represent an association between the items of information.

Basic terminologies degree of the node leaf nodes or terminal nodes non terminal nodes children siblings ancestors degree of a tree hierarchical structure height or depth of a tree forest 3 2 1 4

Representation of a tree List Representation Linked List Representation

(A (B(F,G,H), C, D(I), E(J,K(L))) ) List representation (A (B(F,G,H), C, D(I), E(J,K(L))) ) A B C D E F G H I J K L Level 1 Level 2 Level 3 Level 4

(b) Linked list representation of the tree DATA LINK 1 LINK 2 … LINK n (a) General node structure F K T A B G H I C D E L J (b) Linked list representation of the tree

An alternative elegant linked representation (a) General node structure 1 A C BH F G H D I E J O K L (b) Linked representation of the tree TAG DATA / DOWNLINK LINK 0/1 1

Binary Trees A binary tree has the characteristic of all nodes having at most two branches, that is, all nodes have a degree of at most 2. A binary tree can therefore be empty or consist of a root node and two disjoint binary trees termed left subtree and right subtree.

A G C D B E F Level 1 Level 2 Level 3 Fig. 2

Important observations regarding binary trees: The maximum number of nodes on level i of a binary tree is 2i-1, i>1 The maximum number of nodes in a binary tree of height h is 2h-1, h>1 For any non empty binary tree, if to is the number of terminal nodes and t2 is the number of nodes of degree 2, then to=t2+1

A binary tree of height h which has all its permissible maximum number of nodes viz., 2h-1 intact is known as a full binary tree of height h. A binary tree with n’ nodes and height h is complete if its nodes correspond to the nodes which are numbered 1 to n (n’ n) in a full binary tree of height h. Also, the height of a complete binary tree with n elements has a height h given by

A binary tree which is dominated solely by left child nodes or right child nodes is called a skewed binary tree or more specifically left skewed binary tree or right skewed binary tree respectively. a b c d n o Left skewed Right skewed m p

Types of Binary Trees Skewed – only one child Complete – always two children Balanced – “mostly” two children more formal definitions exist, above are intuitive ideas Skewed binary tree Balanced binary tree Complete binary tree

Binary Trees Properties Balanced Height = O( log(n) ) for n nodes Useful for searches Skewed Height = O(n) for n nodes Similar to linked list Skewed binary tree Balanced binary tree

A complete binary tree obeys the following properties with regard to its node numbering: If a parent node has a number i then its left child has the number 2i (2i<n). If 2i>n then i has no left child. If a parent node has a number i, then its right child has the number 2i+1 (2i+1<n). If 2i+1>n then i has no right child. If a child node (left or right) has a number i then the parent node has the number i /2 if i 1. If i =1 then i is the root and hence has no parent.

Representation Of Binary Trees Array Representation 10 5 4 2 1 a c b e d f 10 a b c d e f 1 2 3 4 5 6 7 8 9 11 11

Linked representation b d c e f LCHILD DATA RCHILD

Binary Tree Traversal A binary tree traversal requires that each node of the tree be processed once and only once in a predetermined sequence. In the depth-first traversal processing process along a path from the root through one child to the most distant descendant of that first child before processing a second child.

Pre order/In Order/Post Order traversals Pre Order: Contents of the root are printed, followed by the left subtree and then the right subtree (Visit – left – right) In Order: Left sub-tree traversed first before visiting the parent, followed by traversal of the right sub-tree (left - Visit – right) Post Order: Contents of left subtree are printed first, followed by right subtree and finally the root node (left - right – Visit) f j c h k a d i

Threaded Binary Trees A.J. Perlis and C.Thornton devised a prudent method to utilize the (n+1) empty pointers, introducing what are called threads. For a node NODE if RCHILD(NODE) is NIL then the null pointer is replaced by a thread which points to the node which would occur after NODE when the binary tree is traversed in inorder. Again if LCHILD (NODE) is NIL then the null pointer is replaced by a thread to the node which would immediately precede NODE when the binary tree is traversed in inorder.

A B F G C D E Inorder Traversal: G F B A D C E LEFT THREAD TAG LCHILD DATA RCHILD RIGHT THREAD TAG (True or False)

(a) Empty threaded binary tree (b) Non empty threaded binary tree true false LCHILD DATA RCHILD (a) Empty threaded binary tree T f A B t F C D E G (b) Non empty threaded binary tree LEFT THREAD TAG RIGHT THREAD TAG Head node f : false t : true

Applications Expression Trees ((A+B)*C-D) G Traversals of an expression tree Conversion of Infix Expression to Postfix Expression