1 CSC 211 Data Structures Lecture 24 Dr. Iftikhar Azim Niaz 1.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II Chapter 10 Trees I. 2 Topics Terminology.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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.
Marc Smith and Jim Ten Eyck
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.
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
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.
COSC2007 Data Structures II
Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications.
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 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree ADTs Tree concepts. Applications of Trees. A Tree ADT – requirements, contract. Linked implementation of Trees. Binary Tree ADTs. Binary Search.
Tree Data Structures. Introductory Examples Willliam Willliam BillMary Curt Marjorie Richard Anne Data organization such that items of information are.
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.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
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.
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
Binary Tree.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
Foundation of Computing Systems Lecture 4 Trees: Part I.
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 : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
TREE Ahsan Ahmed College of Computer and Information Science Majma’ah University 1.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
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.
CSCE 3110 Data Structures & Algorithm Analysis
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
CMSC 341 Introduction to Trees.
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
Introduction to Trees IT12112 Lecture 05.
Trees and Binary Trees.
Week nine-ten: Trees Trees.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Trees, Binary Search Trees
Trees.
Tree.
CS210- Lecture 9 June 20, 2005 Announcements
EE Data Structures and Algorithms
Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

1 CSC 211 Data Structures Lecture 24 Dr. Iftikhar Azim Niaz 1

2 Last Lecture Summary Stacks Concept Stack Operations  Push and Pop Stack Implementation  StaticArray Based  DynamicLinked List Stack Applications  Balanced Symbol Checking  Prefix, Infix and Postfix 2

3 Objectives Overview Trees Concept Examples and Applications Definition Terminology Types of Trees General Trees  Representation and Traversal Binary Tree  Types and Representations

4 Tree - Introduction Trees are very flexible, versatile and powerful non-linear data structure It can be used to represent data items possessing hierarchical relationship A tree can be theoretically defined as a finite set of one or more data items (or nodes) such that  There is a special node called the root of the tree  Remaining nodes (or data item) are partitioned into number of subsets each of which is itself a tree, are called subtree A tree is a set of related interconnected nodes in a hierarchical structure

5 Tree - Introduction Some data is not linear (it has more structure!)  Family trees  Organizational charts Linked lists etc don’t store this structure information. Linear implementations are sometimes inefficient or otherwise sub-optimal for our purposes Trees offer an alternative  Representation  Implementation strategy  Set of algorithms

6 Tree Examples Where have you seen a tree structure before? Examples of trees:  Directory tree  Family tree  Company organization chart  Table of contentsetc.

7 Tree Example – Tic Tac Toe XX XX OXX O X O X O X OX

8 Tree Example - Chess

9 Tree Example – Taxonomy Tree

10 Tree Example – Decision Tree tool that uses a tree-like graph or model of decisions and their possible consequences  including chance event outcomes, resource costs, and utility It is one way to display an algorithm.

11 What is a Tree Useful for? Artificial Intelligence – planning, navigating, games Representing things:  Simple file systems  Class inheritance and composition  Classification, e.g. taxonomy (the is-a relationship again!)  HTML pages  Parse trees for language  3D graphics

12 Tree - Definition n A tree is a finite set of one or more nodes such that: n There is a specially designated node called the root. n The remaining nodes are partitioned into n>=0 disjoint sets T 1,..., T n, where each of these sets is a tree. n We call T 1,..., T n the subtrees of the root r n Each of whose roots are connected by a directed edge from r n A tree is a collection of N nodes, one of which is the root and N-1 edges

13 Tree – Basic Terminology Each data item within a tree is called a 'node’ The highest data item in the tree is called the 'root' or root node  First node in hierarchical arrangement of data Below the root lie a number of other 'nodes'. The root is the 'parent' of the nodes immediately linked to it and these are the 'children' of the parent node Leaf node has no children

14 Tree – Basic Terminology If nodes share a common parent, then they are 'sibling' nodes, just like a family. The link joining one node to another is called the 'branch'. This tree structure is a general tree Leaves: nodes with no children (also known as external nodes) Internal Nodes: nodes with children The ancestors of a node are all the nodes along the path from the root to the node

15 Tree – Basic Terminology ‘A’ is root node Each data item in a tree is known as node It specifies the data information and links to other data items Degree of a node is the number of sub-trees of a node in a given tree In the example  Degree of node A is 3  Degree of node B is 2  Degree of node C is 2  Degree of node D is 3  Degree of node J is 4

16 Tree – Basic Terminology Degree of a tree is the maximum degree of node in a given tree Degree of node J is 4 All other nodes have less or equal degree So degree of the tree is 4 A node with degree zero (0) is called terminal node or a leaf Nodes M,N,I,O etc are leaf nodes Any node whose degree is not zero is called a non-terminal node

17 Tree – Basic Terminology Levels of a Tree The tree is structured in different levels The entire tree is leveled in such a way that the root node is always of level 0 Its immediate children are at level 1 and their immediate children are at level 2 and so on up to the terminal nodes If a node is at level n then its children will be at level n+1

18 Tree – Basic Terminology Depth of a Treeis the maximum level of any node in a given tree The number of levels from root to the leaves is called depth of a tree. Depth of tree is 4 The term height is also used to denote the depth of a tree Height (of node): length of the longest path from a node to a leaf. All leaves have a height of 0 The height of root is equal to the depth of the tree

19 Tree – Basic Terminology A vertex (or node) is a simple object that can have a name and can carry other associated information An edge is a connection between two vertices A path in a tree is a list of distinct vertices in which successive vertices are connected by edges in the tree The defining property of a tree is that there is precisely one path connecting any two nodes

20 Tree – Basic Terminology Example: {a, b, d, i } is path Path

21 Path in a Tree A path from node n 1 to n k is defined as a sequence of nodes n 1, n 2, …, n k such that n i is the parent of n i+1 for 1<= i <= k. The length of this path is the number of edges on the path namely k-1. The length of the path from a node to itself is 0. There is exactly one path from from the root to each node.

22 What is a Tree? Models the parent/child relationship between different elements  Each child only has one parent From mathematics:  “a directed acyclic graph”  At most one path from any one node to any other node Different kinds of trees exist. Trees can be used for different purposes. In what order to we visit elements in a tree?

23 How Many Types of Trees are there? Far too many:  General tree  Binary Tree  Red-Black Tree  AVL Tree  Partially Ordered Tree  B+ Trees  … and so on Different types are used for different things  To improve speed  To improve the use of available memory  To suit particular problems

24 General tree

25 Representation of Trees List Representation  ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )  The root comes first, followed by a list of sub-trees datalink 1link 2...link n How many link fields are needed in such a representation?

26 A Tree Node Every tree node:  object – useful information  children – pointers to its children nodes O OO O O

27 Left Child – Right Sibling ABCD E F G H I J K LM data left child right sibling

28 Tree ADT Objects: any type of objects can be stored in a tree Methods: accessor methods  root() – return the root of the tree  parent(p) – return the parent of a node  children(p) – returns the children of a node query methods  size() – returns the number of nodes in the tree  isEmpty() - returns true if the tree is empty  elements() – returns all elements  isRoot(p), isInternal(p), isExternal(p)

29 Tree Implementation typedef struct tnode { int key; struct tnode* lchild; struct tnode* sibling; } *ptnode;  Create a tree with three nodes (one root & two children)  Insert a new node (in tree with root R, as a new child at level L)  Delete a node (in tree with root R, the first child at level L)

30 Tree Traversal Two main methods:  Preorder  Postorder Recursive definition PREorder:  visit the root  traverse in preorder the children (subtrees) POSTorder  traverse in postorder the children (subtrees)  visit the root

31 Tree - Preorder Traversal preorder traversal Algorithm preOrder(v) “visit” node v for each child w of v do recursively perform preOrder(w)

32 Prerder Implementation public void preorder(ptnode t) { ptnode ptr; display(t->key); for(ptr = t->lchild; NULL != ptr; ptr = ptr->sibling) { preorder(ptr); }

33 Tree - Postorder Traversal postorder traversal Algorithm postOrder(v) for each child w of v do recursively perform postOrder(w) “visit” node v du (disk usage) command in Unix

34 Postorder Implementation public void postorder(ptnode t) { ptnode ptr; for(ptr = t->lchild; NULL != ptr; ptr = ptr->sibling) { postorder(ptr); } display(t->key); }

35 Binary Tree A special class of trees:  max degree for each node is 2 Recursive definition: A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and the right subtree. Any tree can be transformed into binary tree. by left child-right sibling representation

36 Binary Tree - Example J IM H L A B C D E F G K

37 Binary Tree Tree size is limited by the depth of the tree For binary tree, nodes at level k = n For N-ary tree, nodes at level k = n k - 1 Size for Binary is: … + n = 2 n – 1 and Size for N-ary is (n k-1 )/(n-1) where k is number of full levels.

38 Binary Tree A binary tree is a tree in which no node can have more than 2 children These children are described as “left child” and “right child” of the parent node A binary tree T is defined as a finite set of elements called nodes such that  T is empty if T has no nodes called the null or empty tree  T contains a special node R, called root node of T  remaining nodes of T form an ordered pair of disjoined binary trees T1 and T2. They are called left and right sub tree of R

39 Binary Tree ‘A’ is the root node of the tree ‘B’ is left child of ‘A’ and ‘C’ is right child of ‘A’ ‘A’ is the father of ‘B’ and ‘C’ The nodes ‘B’ and ‘C’ are called brothers

40 Sample of Binary Trees A B A B A B C G E I D H F Complete Binary Tree Skewed Binary Tree E C D

41 Maximum Nodes in Binary Tress The maximum number of nodes on level i of a binary tree is 2 i-1, I >= 1. The maximum number of nodes in a binary tree of depth k is 2 k -1, k >= 1. Prove by Induction

42 Relation between Number of Leaf Nodes and Nodes of Degree 2 For any nonempty binary tree, T, if n 0 is the number of leaf nodes and n 2 the number of nodes of degree 2, then n 0 =n 2 +1 proof: Let n and B denote the total number of nodes & branches in T. Let n 0, n 1, n 2 represent the nodes with no children, single child, and two children respectively. n= n 0 +n 1 +n 2, B+1=n, B=n 1 +2n 2 ==> n 1 +2n 2 +1= n, n 1 +2n 2 +1= n 0 +n 1 +n 2 ==> n 0 =n 2 +1

43 Full Binary Tree and Complete BT A full binary tree of depth k is a binary tree of depth k having 2 k -1 nodes, k >=0. A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k A B C G E I D H F A B C G E K D J F I H O N M L Complete binary tree Full binary tree of depth 4

44 Binary Tree Representation If a complete binary tree with n nodes (depth = log n + 1) is represented sequentially, then for any node with index i, 1<=i<=n, we have: parent(i) is at i/2 if i!=1. If i=1, i is at the root and has no parent. leftChild(i) is at 2i if 2i n, then i has no left child. rightChild(i) is at 2i+1 if 2i +1 n, then i has no right child.

45 Sequential Representation A B -- C -- D --. E [1] [2] [3] [4] [5] [6] [7] [8] [9]. [16] A B E C D [1] [2] [3] [4] [5] [6] [7] [8] [9] ABCDEFGHIABCDEFGHI A B CG E ID HF (1) waste space (2) insertion/deletion problem

46 Linked Representation typedef struct tnode *ptnode; typedef struct tnode { int data; ptnode left, right; }; data leftright data leftright

47 Summary Trees Concept Examples and Applications Definition Terminology Types of Trees General Trees  Representation and Traversal Binary Tree  Types and Representations