1 / 18

Data Structure

Data Structure. Chapter# 5 Tree. Course Instructor AMEER JAMAL SHAH. TREES:.

Download Presentation

Data Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structure Chapter# 5 Tree Course Instructor AMEER JAMAL SHAH

  2. TREES: • TREES:- Tree is a nonlinear data structure. This data structure is mainly used to represent data containing a hierarchical relationship between elements., e.g., records of family trees and tables of contents etc. Tree is divided into • General Trees. • Binary Trees. • General Trees:- It can be defined as a node having finite number of subnodes is known as a general tree.

  3. Tree Image

  4. Level 0 A B C 1 D E F 2 G 3 Level of TREES • The tree shown in the fig will be access as : ABCDEF •   A Level First • B C Level Second • D E F Level Third

  5. Binary Tree:- • A Tree which has almost two branches is called Binary Tree. A binary tree can be easily maintained in the computer, although it seems to be very restricted. • A Binary Tree “T” has a finite set of elements, called nodes such that: • T is empty (called the null tree or empty tree), or • T contains a distinguished node “R” called the root of the tree, and the remaining nodes of the tree form an ordered pair of disjoined binary tree T1 and T2. R T1 T2 T3 T4 T5 T6

  6. Binary Tree • The node having no preceding node is called the root node. • while the nodes having no succeeding nodes are known as terminal nodes of the tree. • Each node has two successors • one called the left child • one called the right child • left child and/or right child may be empty A binary tree is either - empty or - consists of a root and two binary trees, one called the left subtree and one called the right subtree

  7. Why a binary tree is so useful? • For storing a collection of values that has a binary hierarchical organization • arithmetic expressions • boolean logic expressions • Data structure for a binary search tree • General tree can be stored using a binary tree data structure

  8. * - + 6 1 5 2 An expression tree • An arithmetic expression consists of an operator and two operands (either of which may be an arithmetic expression) • some simplifications • only binary operators (+, *, /, -) • only single digit, non-negative integer operands operands are stored in leaf nodes operators are stored in branch nodes

  9. Traversing a binary tree • What does it mean to traverse a container? • "visit" each item once in some order • Many operations on a collection of items involve traversing the container in which they are stored • displaying all items • making a copy of a container • Linear collections (usually) traversed from first to last • last to first is an alternative traversal order

  10. Binary Tree Traversal Methods • Preorder • The Parent of the subtree is processed first before going into the left then right subtree (Parent, left, right). • Inorder • After the complete processing of the left subtree the Parent is processed followed by the processing of the complete right subtree (left, Parent, right). • Postorder • The root is processed only after the complete processing of the left and right subtree (left, right, Parent).

  11. H D L F N B J A E I C G K M O Preorder Example (visit = print) P-L-R

  12. Preorder of Expression Tree / * + a b - c d + e f Gives prefix form of expression.

  13. H D L F N B J A E I C G K M O Inorder Example (visit = print) L-P-R

  14. Inorder by Projection

  15. H D L F N B J A E I C G K M O Postorder Example (visit = print) L-R-P

  16. Postorder of Expression Tree a b + c d - * e f + / Gives postfix form of expression.

  17. Traversing an expression tree preorder (1st touch) * + 1 3 * - 4 1 2 * + * postorder (last touch) 1 3 + 4 1 - 2 * * 2 1 3 - inorder (2nd touch) 1 + 3 * 4 - 1 * 2 4 1

  18. The End

More Related