tree Data Structures in python Traversals.pptx

195 views 56 slides Apr 26, 2024
Slide 1
Slide 1 of 56
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56

About This Presentation

It is all about trees


Slide Content

TREE DATA STRUCTURE EXAMPLE: COMPUTER FILE SYSTEM

A HYPOTHETICAL FAMILY TREE

A tree consists of a finite set of elements, called nodes, and a finite set of directed lines, called branches, that connect the nodes. TREE ADT Tree is a non- linear data structure which organizes data in recursive hierarchical structure. In tree data structure, every individual element is called as Node . Node in a tree data structure stores the actual data of that particular element and link to next element in hierarchical structure.

STRUCTURE OF A TREE There is one and only one path between every pair of nodes in a tree.

TREE TERMINOLOGY

ROOT NODE In a tree data structure, the first node is called as Root Node . Every tree must have a root node. In any tree, there must be only one root node .

EDGE The connecting link between any two nodes is called as EDGE . In a tree with ' N ' number of nodes there will be a maximum of ' N- 1 ' number of edges.

PARENT NODES

CHILD NODES

ANCESTOR AND DESCENDANT Ancestors: Any node in the path from the current node to the root node. Descendant: Any node in the path from the current node to the leaf nodes.. Ancestor(J) = E,B,A Descendant(C) = G,K,H

SIBLINGS

LEAF NODES The leaf nodes are also called as External Nodes or Terminal Nodes

INTERNAL NODES

DEGREE The Degree of a node is total number of children it has. The highest degree of a node among all the nodes in a tree is called as ' Degree of Tree '

LEVEL In a tree each step from top to bottom is called as a Level and the Level count starts with '0' and incremented by one at each step. Level- distance from the root node.

HEIGHT Height of all leaf nodes is 0.

DEPTH Depth of the root node is '0'

PATH Length of a Path is total number of edges in that path.

SUBTREE Each child node with a parent node forms a subtree recursively . Any connected structure below root is a subtree. Collection of subtrees makes a tree.

FOREST A forest is a set of disjoint trees .

COMPARISON

ReoI Leeł Noda &aval0 Levei 1

APPLICATION OF TREE

CLASSIFY THE TWO TRAVERSALS PERFORMED IN TREE ADT. A traversal of a tree requires that each node of the tree be visited once A typical reason to traverse a tree is to display the data stored at each node of the tree Traversal Types preorder Inorder Postorder level-order

LEVEL ORDER TRAVERSAL

Traversal Starts at the root node and explores as deep as possible along each branch and backtrack to sibling branch until all nodes are visited. It is a recursive approach for all subtrees.

PREORDER TRAVERSAL Processing order: Root → Left → Right Algorithm Visit the root Traverse the left sub tree i.e. call Preorder (left sub tree) Traverse the right sub tree i.e. call Preorder (right sub tree)

EXAMPLE

Traverse the entire tree starting iron the root rode keeping yourself to the left. Preorder Traversal : A , B , D , E , C , F , G

INORDER TRAVERSAL Processing order: Left → Root → Right Algorithm Traverse the left sub tree i.e. call Inorder (left sub tree) Visit the root Traverse the right sub tree i.e. call Inorder (right sub tree)

EXAMPLE

POSTORDER TRAVERSAL Processing order: Left → Right → Root Algorithm Traverse the left sub tree i.e. call Postorder (left sub tree) Traverse the right sub tree i.e. call Postorder (right sub tree) Visit the root

EXAMPLE

Postorder Traversal Shortcut

EXERCISE

Q1 1. Visit the following tree in a breadth first manner and print all the nodes.

ANSWER

Binary Tree Expression Tree Binary Search Tree Trees AVL Tree B Tree Heap Tree TYPES OF TREES

BINARY TREE AND ITS RELATED OPERATIONS A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary tree can have only 2 children, we typically name them the left and right child. A Binary Tree node contains following parts. Data Pointer to left child Pointer to right child

BINARY TREE A node can have 0,1, and 2 children in a binary tree

CONT.. In a tree each node contains information and may or may not contain links.

PROPERTIES OF BINARY TREE At any level ā€˜i’, maximum number of nodes possible is 2^i

Maximum number of nodes of height h= CONT..

Minimum number of nodes of height h=h+1 CONT..

CONT.. Maximum height of the tree if minimum number of nodes is given is (n- 1)

CONT.. Minimum height of the tree if maximum number of possible nodes is given by

TYPES OF BINARY TREE Full/Strict/ Proper Binary Tree Complete Binary Tree Perfect Binary Tree Degenerate Binary Tree Balanced Binary Tree Unbalanced Binary Tree

FULL BINARY TREE It is a binary tree where each node will contains exactly two children except leaf node

Full Binary Tree

COMPLETE BINARY TREE It is a binary tree where all levels are completely filled (except possibly the last level) and the last level has nodes as left as possible

Complete binary tree

PERFECT BINARY TREE It is a binary tree where all internal nodes have 2 children and all leaves are at same level A perfect binary tree is a complete binary tree or full binary tree but vice versa is not true

DEGENERATE BINARY TREE It is a binary tree where all internal nodes are having only one child

BALANCED BINARY TREE Find Balance Factor, B=H L - H R H L - height of left subtree; H R - height of right subtree The height of the left and right subtrees differs by no more than one i.e., its balance factor of every node should be –1, 0, or +1

UNBALANCED BINARY TREE
Tags