• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Trees
 

Trees

on

  • 534 views

 

Statistics

Views

Total Views
534
Views on SlideShare
534
Embed Views
0

Actions

Likes
1
Downloads
15
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Trees Trees Presentation Transcript

    • Presented By: Susheel Thakur M.Tech(C.S) Roll No- 324
    •     Have you ever thought how does the OS manages or files? Why do we have a hierarchical file system? How do files get saved and deleted under hierarchical directories ? SOLUTION: A hierarchical data structure called Trees. 11/13/13 06:38 AM Data Structures and Algorithms 2
    •  Tree Nodes  Each node can have 0 or more children  A node can have at most one parent   Binary tree  Tree with 0–2 children per node 11/13/13 06:38 AM Tree Data Structures and Algorithms Binary Tree 3
    •  Terminology  Root ⇒ no parent  Leaf ⇒ no child  Interior ⇒ non-leaf  Height ⇒maximum level of a node  Level ⇒ Length of path from root to node plus 1 Root node Interior nodes Height Leaf nodes 11/13/13 06:38 AM Data Structures and Algorithms 4
    •   General Tree – organization chart format Parenthetical Listing 11/13/13 06:38 AM Data Structures and Algorithms 5
    • 11/13/13 06:38 AM Data Structures and Algorithms 6
    •  Parenthetical Listing – the algebraic expression, where each open parenthesis indicates the start of a new level and each closing parenthesis completes the current level and moves up one level in the tree. 11/13/13 06:38 AM Data Structures and Algorithms 7
    • A (B (C D) E F (G H I) ) 11/13/13 06:38 AM Data Structures and Algorithms 8
    • 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 data 11/13/13 06:38 AM link 1 link 2 ... Data Structures and Algorithms link n 9
    •  Every tree node: object – useful information  children – pointers to its children nodes  O O O O O 11/13/13 06:38 AM Data Structures and Algorithms 10
    •    A binary tree is a tree in which no node can have more than two subtrees; the maximum outdegree for a node is two. In other words, a node can have zero, one, or two subtrees. These subtrees are designated as the left subtree and the right subtree. 11/13/13 06:38 AM Data Structures and Algorithms 11
    • 11/13/13 06:38 AM Data Structures and Algorithms 12
    • A null tree is a tree with no nodes 11/13/13 06:38 AM Data Structures and Algorithms 13
    •   The height of binary trees can be mathematically predicted Given that we need to store N nodes in a binary tree, the maximum height is H max = N A tree with a maximum height is rare. It occurs when all of the nodes in the entire tree have only one successor. Data Structures and Algorithms 14
    •  The minimum height of a binary tree is determined as follows: H min = [ log 2 N ] + 1 For instance, if there are three nodes to be stored in the binary tree (N=3) then Hmin=2. Data Structures and Algorithms 15
    •  Given a height of the binary tree, H, the minimum number of nodes in the tree is given as follows: N min = H Data Structures and Algorithms 16
    •  Given a height of the binary tree, H, the maximum number of nodes in the tree is given as follows: N max = 2 − 1 H Data Structures and Algorithms 17
    •   A complete tree has the maximum number of entries for its height. The maximum number is reached when the last level is full. A tree is considered nearly complete if it has the minimum height for its nodes and all nodes in the last level are found on the left 11/13/13 06:38 AM Data Structures and Algorithms 18
    • 11/13/13 06:38 AM Data Structures and Algorithms 19
    • 11/13/13 06:38 AM Data Structures and Algorithms 20
    • Sequential Representation A B C D [1] [2] [3] [4] [5] [6] [7] [8] [9] . [16] (1) waste space (2) insertion/deletion problem A B -C ---D -. E A B C D E F G H I A B D E 11/13/13 06:38 AM [1] [2] [3] [4] [5] [6] [7] [8] [9] H I Data Structures and Algorithms C E F G 21
    • Linked Representation typedef struct tnode *ptnode; typedef struct tnode { int data; ptnode left, right; }; data left data right left 11/13/13 06:38 AM Data Structures and Algorithms right 22
    • 11/13/13 06:38 AM Data Structures and Algorithms 23
    •  A binary tree traversal requires that each node of the tree be processed once and only once in a predetermined sequence. 11/13/13 06:38 AM Data Structures and Algorithms 24
    • 11/13/13 06:38 AM Data Structures and Algorithms 25
    • 11/13/13 06:38 AM Data Structures and Algorithms 26
    • 11/13/13 06:38 AM Data Structures and Algorithms 27
    • 11/13/13 06:38 AM Data Structures and Algorithms 28
    • 11/13/13 06:38 AM Data Structures and Algorithms 29
    • Preorder Traversal (recursive version) void preorder(tnode ptr) /* preorder tree traversal */ { if (ptr) { printf(“%d”, ptr->data); preorder(ptr->left); predorder(ptr->right); } } 11/13/13 06:38 AM Data Structures and Algorithms 30
    • 11/13/13 06:38 AM Data Structures and Algorithms 31
    • 11/13/13 06:38 AM Data Structures and Algorithms 32
    • Inorder Traversal (recursive version) void inorder(tnode ptr) /* inorder tree traversal */ { if (ptr) { inorder(ptr->left); printf(“%d”, ptr->data); indorder(ptr->right); } } 11/13/13 06:38 AM Data Structures and Algorithms 33
    • 11/13/13 06:38 AM Data Structures and Algorithms 34
    • 11/13/13 06:38 AM Data Structures and Algorithms 35
    • Postorder Traversal (recursive version) void postorder(tnode ptr) /* postorder tree traversal */ { if (ptr) { postorder(ptr->left); postdorder(ptr->right); printf(“%d”, ptr->data); } } 11/13/13 06:38 AM Data Structures and Algorithms 36
    •  Using Pre order and Post order Traversal  Using Post order and In order Traversal 11/13/13 06:38 AM Data Structures and Algorithms 37
    • 1. 2. 3. 4. Scan the preorder traversal from left to right. For each scanned node locate its position in inorder traversal. Let the scanned node be X. The nodes preceding X in inorder form its left subtree and nodes succeeding it form right subtree. Repeat step1 for each symbol in the preorder. 11/13/13 06:38 AM Data Structures and Algorithms 38
    • Preorder: ABDHECFG Inorder: DHBEAFCG A B D C E F G H 11/13/13 06:38 AM Data Structures and Algorithms 39
    • 1. 2. 3. Scan the post order traversal from right to left. For each node scanned locate its position in order traversal. Let the scanned node be X. The node preceding X in inorder, form its left subtree and nodes succeeding it form right subtree. 11/13/13 06:38 AM Data Structures and Algorithms 40
    • Postorder: HDIEBJFKLGCA Inorder: HDBIEAFJCKGL A B D H 11/13/13 06:38 AM C E I F G J Data Structures and Algorithms K L 41
    •  A variable is an expression.  If x and y are expressions, then ¬x, x∧y, x∨y are expressions.  Parentheses can be used to alter the normal order of evaluation (¬ > ∧ > ∨).  Example: x1 ∨ (x2 ∧ ¬x3) 11/13/13 06:38 AM Data Structures and Algorithms 42
    • (x1 ∧ ¬x2) ∨ (¬ x1 ∧ x3) ∨ ¬x3 ∨ ∨ ¬ ∧ ∧ X1 ¬ ¬ X2 11/13/13 06:38 AM X3 X3 X1 postorder traversal (postfix evaluation) 43 Data Structures and Algorithms
    • 11/13/13 06:38 AM Data Structures and Algorithms 44