18
Trees & Graphs WHAT IS A TREE? TREE IS A DATA STRUCTURE SIMILAR TO LINKED LIST INSTEAD OF POINTING TO ONE NODE EACH NODE CAN POINT TO A NUMBER OF POINT NON LINEAR DATA STRUCTURE WAY OF REPRESENTING HIERARCHAL NATURE OF A STRUCTURE IN A GRAPHICAL FORM

Trees data structure

Embed Size (px)

DESCRIPTION

DATA STRUCTURE

Citation preview

Page 1: Trees data structure

Trees & GraphsWHAT IS A TREE?

• TREE IS A DATA STRUCTURE SIMILAR TO LINKED LIST

• INSTEAD OF POINTING TO ONE NODE EACH NODE CAN POINT TO A NUMBER OF POINT

• NON LINEAR DATA STRUCTURE

• WAY OF REPRESENTING HIERARCHAL NATURE OF A STRUCTURE IN A GRAPHICAL FORM

Page 2: Trees data structure

Level 0

Level 1

Level 2

Level 3

Page 3: Trees data structure

Some Definition • HEIGHT OF A NODE IS THE LENGTH OF THE PATH FROM THAT NODE TO

THE DEEPEST NODE. SIMILARLY HEIGHT OF A TREE IS THE LENGTH FROM ROOT TO THE DEEPEST NODE IN THE TREE. FOR EXAMPLE FOR NODE 17 HEIGHT WOULD BE 2 AND HEIGHT OF THE TREE WOULD BE 4

• ANCESTOR -IF THERE EXISTS A PATH FROM ROOT TO THE NODE LET SAY Q AND NODE P IS EXIST IN THAT PATH THEN WE CAN CALL P WOULD BE THE ANCESTOR OF Q.50,70,12 ARE THE ANCESTOR OF 9

• SIZE OF A NODE IS THE NUMBER OF DESCENDENT IT HAS INCLUDING ITSELF. 72 HAS SIZE = 3

• SKEW TREE – EVERY NODE HAS ONLY ONE CHILD EXCEPT LEAF NODE

• BINARY TREE ---- EACH NODE HAS ZERO CHILD, ONE CHILD OR TWO CHILD

Page 4: Trees data structure

Types of binary treeSTRICT BINARY TREE

• EACH NODE HAS EXACTLY TWO OR ZERO CHILD

FULL BINARY TREE

• EACH NODE HAS EXACTLY TWO NODE OR ZERO CHILD AND LEAF NODE IS AT THE SAME LEVEL

COMPLETE BINARY TREE

• ALL LEAF NODE IS AT THE HEIGHT OF H OR H-1 AND ALSO WITHOUT ANY MISSING NODE IN THE SEQUENCE

Page 5: Trees data structure

Properties of binary tree• THE NUMBER OF NODE IN A FULL BINARY TREE ARE 2POW(H+1) –

1.

• THE NUMBER OF NODES IN A COMPLETE BINARY TREE ARE IN BETWEEN 2POW(H) TO 2POW(H+1) – 1.

Page 6: Trees data structure

Operations on binary treeBasic Operations

• Inserting an element into tree

• Deleting an element from tree

• Searching for an element

• Traversing the tree

Auxiliary operations

• Finding – size of tree, height of tree least common ancestor (LCA) for a given pair of nodes

Page 7: Trees data structure

Applications of Binary Tree• Expressions tree are used in compiler• Huffman coding trees which are used in data

compression technique• Binary search tree (BST) supports insertion

deletion of node with O(log n)• Priority queue support search and deletion of

min or max on a collection of items in a logarithmic time

Page 8: Trees data structure

Binary Tree Traversal• The process of visiting all nodes in tree sequentially in tree is called

traversal

• Each node has to be processed only once but they may be visited more than once

• In the case of linked list or array traversal can be done linearly but it is slightly difficult in the case of Tree

• 6 possibilities of traversal

FOR LEFT L – LDR,LRD

similarly for right and root D

• From above possibility we are choosing only 3 possibility, cause it really does not matter that which comes first L or R it may be L or R . Standardize them and choose any one of them and we have following traversal method

Page 9: Trees data structure

• Preorder (DLR) traversal

• Inorder (LDR) traversal

• Postorder (LRD) traversal

Suppose we have full binary tree numbered breadth wise

• Preorder (DLR) traversal– each node is processed before its children processed

• For given example 1 2 4 5 3 6 7

• Definition • Visit the root

• Traverse the left subtree in preorder

• Traverse the right subtree in preorder

Page 10: Trees data structure

• Recursive algo

void preorder (struct BinaryTreeNode *root)

{

printf(“%d”,root->data);

preorder(root->left);

preorder(root->right);

}

Page 11: Trees data structure

Non recursive algorithm

Void preorderNonRecursive (struct BinaryTreeNode *root)

{ struct stack *s=createstack();

While(1){

while(root){

printf(“%d”,root->data);

push(s,root); root=root->left;

}

if(IsEmptyStack)

break;

root=pop(s); root=root->right;}

deleteStack(s);

}

Page 12: Trees data structure

Inorder traversal nonrecursive algo

Void inorderNonRecursive (struct BinaryTreeNode *root)

{ struct stack *s=createstack();

While(1){

while(root){

push(s,root); //got left sub tree and keep on adding it to the stack

root=root->left;

}

if(IsEmptyStack)

break;

root=pop(s);

printf(“%d”,root->data);//after popping process the root element

root=root->right;}

deleteStack(s);

}

Page 13: Trees data structure

postorder traversal nonrecursive algo

Void postorderNonRecursive (struct BinaryTreeNode *root)

{ struct stack *s=createstack();

While(1){

if(root){

push(s,root); //got left sub tree and keep on adding it to the stack

root=root->left; }

Else if(IsEmptyStack(s))

{ printf(“stack is empty \n”); return; }

Else if (top(s)->right==null){

root=pop(s);

printf(“%d”,root->data);

if(root==top(s)->right){

printf(“%d”,top(s)->data);

pop(s);

}

}

if(!IsEmptyStack(s))

root=top(s); else root = null;

}} deleteStack(s); }

Page 14: Trees data structure

Finding max element in Binary Tree• One way to achieve result is find max elem in Left and Right SubTree and

compare it with root value

Int FindMax(Struct BinaryTreeNode *root){

int root_val,left,right,max=INT_MIN;

if(root!=NULL){

root_val=root->data;

left=FindMax(root->left);

right=FindMax(root->right);

}

if(left>=right) max=left; else max=right;

If(root>max) max=root_val;

}

Page 15: Trees data structure

Generic Tree (N-ary Tree)• Each node can have more than two child or no child , one child ,

two child called Generic tree . Tree of a general kind

Possible solution for these purpose is shown bellow

struct TreeNode{

int data;

Struct TreeNode *firstchild;

Struct TreeNode *secondchild;

Struct TreeNode *threechild;

Struct TreeNode *fourchild;

Struct TreeNode *fivechild; }

Page 16: Trees data structure

Problem has raised due to above structure

• For general node there may exist one child , two child , three child , so it could be a wastage of memory by allocating extra child Since we are not using all the pointer in all the cases. We do not know the number of children for each node

To solve the problem

Since our objective is to reach all the nodes of tree a possible solution is:

• At each node there should be a link between all the children of same parent (sibling)

• Also , remove all the link from parent to their child except first child

Struct TreeNode{

Int data;

Struct TreeNode *firstchild;

Struct TreeNode *nextsibling;

}

Page 17: Trees data structure

Threaded Binary Tree traversal (stackless,queueless traversal)

Page 18: Trees data structure