Vous êtes sur la page 1sur 68

TREE Data Structures and TREE ADT

AADS-10

Specialties of TREE structure


TREE denotes a hierarchical organization of the relationships Tree represent multipath possibilities Tree denotes a nonlinear organization unlike List Tree does not contain any cycles or loops

Pedigree Tree
GRAND FATHER
FATHER

GRAND MOTHER
GRAND FATHER

CHILD

MOTHER
GRAND MOTHER

Match Fixture
P1

Q1
P2 S1

Q2
Q3 P7 Q4 P8

FINALS

S2

Organization Chart for a simple firm


PROPRIETER

WORKERS

Organization Chart for bigger firm


PROPRIETER

SUPERVISOR

WORKERS

Organization Chart for a large firm


PROPRIETER

MANAGER

SUPERVISOR

SUPERVISOR

SUPERVISOR

WORKERS

WORKERS

WORKERS

Enterprise Organization Chart


SHARE HOLDERS BOARD OF DIRECTORS

Chief Executive Officer

Chief Information Officer

General Manager

General Manager

General Manager

DGM

DGM

DGM

DGM

DGM

DGM

AGM

AGM

Organization of NIT Calicut


MHRD

NIT Council
BOG DIRECTOR Finance Committee BWC

Senate BOAC DCC s

Purchase Committee

Class Committees

Doctoral Committees

Observations
The Pedigree tree converges in the line of inheritance of descendants The organization chart diverges in the order of inheritance downwards. But the power converges as we go up in the Organization Chart. The root/seat of power is at the top Two are Trees of two different types

TREE in Data Structures

Tree as a data structure


Arrays and Linked Lists are linear structures and it is difficult to use them to organize a hierarchical representation of objects Ordered List, Stack, and Queue reflect some hierarchy, but they are limited to only one dimension Tree overcome this limitation and provide a new data type for hierarchical organization of objects

TREE in Data Modeling & Program Design


1. Tree, in general, is used to represent the hierarchical division of relationships in data abstraction 2. Tree can be used as a data structure for holding the references to the data objects when the objects have hierarchical relationship or there exists such a protocol based information flow among them 3. Tree as an Abstract Data Type can be used in the algorithms for utilizing the hierarchical representations of the objects for the development of the solution a given problem

Rooted Tree
In the case of data structures, we use the concept of ROOTED TREE with root on the top and the nodes at subsequent levels branch out to the bottom
ROOT

LEAF

Why insisting for Rooted Tree?


Tree originate in two contexts
1. Tree from a Graph(showing the topology). This Tree is not Rooted Tree. This Tree shows the topological relations extracted from a Graph and shall be without cycles/loops 2. Rooted Tree showing the discrete hierarchical organization that stand for a governance system, information flow/decision paths, or the data structure. Root is the first point of access to the Tree

So, only the Rooted Tree is considered in the Data Structure TREE All the subtrees also shall be Rooted Trees

Defining the Tree


Tree consists of Nodes/Vortex and the arcs/links/edges An empty Tree is a TREE defined and not instantiated(not created as an object). It will have a reference to the TREE structure. We ADD Nodes to a TREE to populate the Tree structure We DELETE Nodes to modify the Tree

Properties of TREE in Data Structures


Let us take that a Tree is made of nodes and edges. Then
Nodes appear at distinct levels showing the hierarchy in the relationships Nodes can be used for representing objects of simple data type or the complex objects All nodes at a particular level all appear at the same line from left to right

Properties of Node
A Node, in general, is a child of a node called parent node that is present at the preceding level and itself shall be the parent of its child node at the next lower level/succeeding level Root of the Tree is a Node and has no parent. Descendants originate from the Root Node A reference to the TREE points to the ROOT Node There are three types of Nodes-ROOT, LEAF, and Internal Node

Properties of Nodes
Nodes at a given level are called siblings Siblings are ordered from left to right at a given level Left siblings are always to the left and right siblings are always to the right of a node. Parent node is the predecessor and the child node is the successor There will be ancestors and descendants for a node( except root and leaf)

ROOT

TREE

INTERNAL NODES

Siblings

LEAF

LEAF

This is not a proper Tree

This is a proper Tree

Properties of TREE
A Path in a Tree is the path from one node to another in the hierarchically downward order There will be Starting Node and Ending Node for a Path One edge between a pair of Nodes in the hierarchical order in the Path will have a unit weight Length of the path is equal to the number of edges or alternatively one less than the number of nodes in the Path

Find the Paths in the Tree

Properties of Path
Path from a Starting Node to an Ending Node is unique(There is no alternate path for a given Starting Node-Ending Node pair) Therefore, this path shall be the path with minimum length for the given Starting Node and Ending Node

Properties of path-contd
That means, there shall be a definite inheritance path for a Descendant from an Ancestor For a Child Node there is only one Parent Node in a Rooted Tree But for a Parent Node there could be many Child Nodes in the Rooted Tree

Height of Tree
Height of a Tree is the number of levels from the bottom most Leaf to the Root(Leaf at zero height) Depth of a Tree is the longest path from the Root to Leaf Depth of a level is the length of the path from Root to the node in that level Height of a Level is the difference between height of Tree and the Depth of the Level

Find the Height of the Tree 4

Depth of Node
Depth of a Node is the Length of the path from the Root to that Node

Left SubTree and Right SubTree

Properties-contd
Two Trees can be joined by making one of the Root as child of the other Root One Tree can be split into two Trees if we detach one Internal Node and make it the Root of the new Tree

Joining the Trees


TREE 1 TREE 2

Splitting Trees
TREE 1 TREE 2

Tree Traversals
Tree Traversal is the journey through the Nodes starting from the Root There are three Traversals
Preorder Traversal Post order Traversal In order Traversal

Purpose for Traversal through the Tree


Traversal through the Tree is needed for
1. Finding a match for KEY is the Search operations 2. Modifying contents in the Node for the algorithm purpose 3. Collecting the data from the Nodes for some operation and so on

Pre order Traversal


Go to Root Go to left Go to left child Go to right child Progress towards right
Process the Root Process the left subtree of R in preorder Process the right subtree of R in preorder

Post Order Traversal


Go to left Go to left child Go to right child Go to root Progress towards right
Traverse the left subtree of R in postorder Traverse the right subtree of R in postorder Process the Root

In Order Traversal
Go to Left Go the left child Go to Root Go to right child Progress to right

Traverse the left subtree of R in inorder Process the Root Traverse the right subtree of R in inorder

Example
A B

F D
E H G

Solution
Preorder visit is

ABCDEFGHIJ
Postorder is

CEDBIJHGFA
Inorder is

CBDEAFIHJG

The INORDER, PREORDER and POSTORDER sequencing gives an order for the NODES of the TREE, for example for search, namely INORDER SEARCH, PREORDER SEARCH, and POSTORDER SEARCH respectively

Tree traversal operation can be applied to any Tree. But this operation is more important for Binary Trees

Consider an expression a+b We can draw a Tree with the Operator + in the Root as follows This gives an Expression Tree for this Expression

Expression Trees

Exercise
Draw the expression Tree for the following expression ax+by+cz

Solution
+

In these Expression Trees we have only maximum two child nodes for a parent node. Hence they are Binary Trees

Polish Notations
Polish notation is the symbolic way of writing arithmetic expressions This symbolism becomes useful in the Compiler Construction

Notations for arithmetic expression


There are three types of notations
1. Infix Notation 2. Prefix Notation(Polish notation) 3. Postfix Notation (suffix notation)(reverse Polish notation)

Infix Notation
This is the commonly used way for writing arithmetic expressions when the binary operators +, -,*, and / are used The expression ax+b is given in the infix notation form

Prefix Notation
Invented by the Polish mathematician Jan Lukasiewicz in the 1920s Here, the operator precedes the operands The order of operations and the operands determines the result Advantage is that the Parentheses are not needed Known as Polish Notation

Example
a+b can be written as +ab 3*(4+5) can be written as *3 + 4 5

Please Excuse My Dear Aunt Saly


Order of execution for arithmetic expressions is
Normal order of execution is from Parentheses left to right. Exponentiation Presence of parentheses changes Multiplication this and what is given in the parentheses shall be computed first Division Addition Subtraction

Postfix(suffix) or Reverse Polish Notation


Here, a+b shall be written as ab+ This was introduced by the Engineers in Hewlett Packard in 1960s when they designed the keyboard for the Calculator The expression 3*(4+5) then becomes 45+3* The expression a+b becomes ab+

Executing Postfix expressions


Following rules are repeatedly applied for evaluating the expression in the postfix form
1. Find the leftmost operator in the expression 2. Select the two operands immediately to the left of the operator found 3. Perform the indicated operation 4. Replace the operator and operands with the result Then progress to the right

Example
See the expression 4 5 + 3 * Scanning from left, + appear first. Therefore, first 4 and 5 shall be added and result 9 shall replace 4 5 + Then, we have 9 3 * So, we multiply 9 and 3 to get the result

Using STACK
Imagine the execution of the postfix expression using a Stack which can hold the operands as well as the operator symbols so as to feed to a processor in a sequential manner by popping one operand/operator at a time How shall we go about?

Examples
Infix A * (B + C) / D Postfix- A B C + * D / Prefix- / * A + B C D

SYMBOLIC CODE
Consider the Postfix Expression A B C + * D / We can convert this into some Assembly Language code as LOAD B Load B to Accumulator ADD C Add C to content in the Accumulator MULT A Multiply content in the Accumulator with A DIV D Divide content in the Accumulator with D

Expression Trees and the Polish notations


If we have the Expression Tree, can we generate the three notations by traversing the Tree in the three orders-Inorder, preorder and postorder forms?

Example
+

Notations
Infix: a+b Inorder traversal Prefix: + a b Preorder traversal Postfix : a b + Postorder traversal

Example
Infix A * (B + C) / D Postfix- A B C + * D / Prefix- / * A + B C D / *

D
+

Exercises
2X2 +4(X+Y)-25Y2 A+B+CX-D(X(C+D))

Exercise
Given a polynomial Ax4 +Bx3 +Cx2 +Dx +E It is modified as below E+x(D+x(C+x(B+Ax))) Which one could be less complex if ADD is taking less time than MULT

Summary
An expression in infix form can be converted to the Expression Tree This Tree can be converted to Postfix notation Then we can convert to Assembly Language Code AL code can be converted to Machine Code as per the Instruction Set of the Processor

TREE ADT
A TREE ADT can have following methods
root(T) parent(v) children(v) isInternal(v) isExternal(v) isRoot(v) size(T) Elements(T) Positions(v) swapElements(v,w) replaceElement(v,e)

BINARY TREE(click)

Vous aimerez peut-être aussi