Académique Documents
Professionnel Documents
Culture Documents
AADS-10
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
WORKERS
SUPERVISOR
WORKERS
MANAGER
SUPERVISOR
SUPERVISOR
SUPERVISOR
WORKERS
WORKERS
WORKERS
General Manager
General Manager
General Manager
DGM
DGM
DGM
DGM
DGM
DGM
AGM
AGM
NIT Council
BOG DIRECTOR Finance Committee BWC
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
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
So, only the Rooted Tree is considered in the Data Structure TREE All the subtrees also shall be Rooted Trees
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
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
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
Depth of Node
Depth of a Node is the Length of the path from the Root to that Node
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
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
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
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
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
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)