180 likes | 311 Views
Data Structure. Chapter# 5 Tree. Course Instructor AMEER JAMAL SHAH. TREES:.
E N D
Data Structure Chapter# 5 Tree Course Instructor AMEER JAMAL SHAH
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.
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
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
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
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
* - + 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
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
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).
H D L F N B J A E I C G K M O Preorder Example (visit = print) P-L-R
Preorder of Expression Tree / * + a b - c d + e f Gives prefix form of expression.
H D L F N B J A E I C G K M O Inorder Example (visit = print) L-P-R
H D L F N B J A E I C G K M O Postorder Example (visit = print) L-R-P
Postorder of Expression Tree a b + c d - * e f + / Gives postfix form of expression.
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