SlideShare a Scribd company logo
1 of 10
[object Object],[object Object],[object Object],[object Object],[object Object],A C D E F B Left Sub Tree Right Sub Tree Binary Tree --  A Tree is said to be a binary tree that every element in a binary tree has at the most two sub trees. ( means zero or one or two ) Types of binary trees -- Strictly binary tree -- Complete binary tree -- Extended binary tree ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Root
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],A B C D E Tree structure using  Doubly Linked List   /* a node in the tree structure */ struct node { struct node *lchild; int data ; struct node *rchild; }; -- The pointer lchild stores the address of left child node. -- The pointer rchild stores the address of right child node. -- if child is not available NULL is strored. -- a pointer variable root represents the root of the tree. Root A B - C - - - D E - -
struct node { struct node *lchild; char data; struct node *rchild; } *root=NULL; struct node *insert(char a[],int index) { struct node *temp=NULL; if(a[index]!='')  { temp = (struct node *)malloc( sizeof(struct node)); temp->lchild = insert(a,2*index+1); temp->data = a[index]; temp->rchild = insert(a,2*index+2); } return temp; } void buildtree(char a[])  { root = insert(a,0); } void inorder(struct node *r){ if(r!=NULL) { inorder(r->lchild); printf("%5c",r->data); inorder(r->rchild); } } void preorder(struct node *r){ if(r!=NULL) { printf("%5c",r->data);  preorder(r->lchild); preorder(r->rchild); } }   void postorder(struct node *r){ if(r!=NULL) { postorder(r->lchild); postorder(r->rchild); printf("%5c",r->data); } } void main() { char arr[] = { 'A','B','C','D','E','F','G','','','H','', '','','','','','','','','',''}; buildtree(arr);  printf("Pre-order Traversal : "); preorder(root); printf("In-order Traversal : "); inorder(root);  printf("In-order Traversal : "); inorder(root); }  Implementing Binary Tree Output :  Preorder Traversal : A B D E H C F G Inorder Traversal : D B H E A F C G Postorder Traversal : D H E B F G C A A B C D E F G H Tree Created :
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Binary Search Tree ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
struct node { struct node *lchild; int data; struct node *rchild; }; struct node *fnode=NULL,*parent=NULL; void insert(struct node **p, int n) { if(*p==NULL)  { *p=(struct node *)malloc(sizeof(struct node)); (*p)->lchild=NULL; (*p)->data = n; (*p)->rchild=NULL; return; } if(n < (*p)->data)  insert(&((*p)->lchild),n); else insert(&((*p)->rchild),n); } void inorder(struct node *t) { if(t!=NULL)  { inorder(t->lchild); printf(&quot;%5d&quot;,t->data); inorder(t->rchild); } } void preorder(struct node *t)  { if(t!=NULL)  { printf(&quot;%5d&quot;,t->data); Implementing Binary Search Tree preorder(t->lchild);  preorder(t->rchild); } } void postorder(struct node *t)  { if(t!=NULL)  { postorder(t->lchild);  postorder(t->rchild); printf(&quot;%5d&quot;,t->data); } } void search(struct node *r,int key){ struct node *q; ,  fnode=NULL;  q=r; while(q!=NULL)  { if(q->data == key) { fnode = q; return; } parent=q; if(q->data > key) q=q->lchild; else  q=q->rchild; }   } void delnode ( struct node **r,int key)  { struct node *succ,*fnode,*parent; if(*r==NULL)  { printf(&quot;Tree is empty&quot;); return; } parent=fnode=NULL;
search(*r,key); if(fnode==NULL) { printf(&quot;Node does not exist, no deletion&quot;); return; } if(fnode->lchild==NULL && fnode->rchild==NULL) { if(parent->lchild==fnode) parent->lchild=NULL; else parent->rchild=NULL; free(fnode); return; } if(fnode->lchild==NULL && fnode->rchild!=NULL) { if(parent->lchild==fnode) parent->lchild=fnode->rchild; else parent->rchild=fnode->rchild; free(fnode); return; } if(fnode->lchild!=NULL && fnode->rchild==NULL) { if(parent->lchild==fnode) parent->lchild=fnode->lchild; else parent->rchild=fnode->lchild; free(fnode); return; } Implementing Binary Search Tree ( continued ) if(fnode->lchild!=NULL && fnode->rchild!=NULL)  { parent = fnode; succ = fnode->lchild; while(succ->rchild!=NULL){ parent=succ; succ = succ->rchild; } fnode->data = succ->data; free(succ); succ=NULL; parent->rchild = NULL; } } void inorder(struct node *t)  { if(t!=NULL)  { inorder(t->lchild); printf(&quot;%5d&quot;,t->data); inorder(t->rchild); } } void preorder(struct node *t)  { if(t!=NULL)  { printf(&quot;%5d&quot;,t->data); preorder(t->lchild);  preorder(t->rchild); } }
void postorder(struct node *t) { if(t!=NULL)  { postorder(t->lchild);  postorder(t->rchild); printf(&quot;%5d&quot;,t->data); } } int main()  { struct node *root=NULL; int n,i,num,key; printf(&quot;Enter no of nodes in the tree : &quot;); scanf(&quot;%d&quot;,&n); for(i=0;i<n;i++)  { printf(&quot;Enter the element : &quot;); scanf(&quot;%d&quot;,&num); insert(&root,num); } printf(&quot;Preorder traversal :&quot;); preorder(root); printf(&quot;Inorder traversal :&quot;); inorder(root); printf(&quot;Postorder traversal :&quot;); postorder(root); printf(&quot;Enter the key of node to be searched : &quot;); scanf(&quot;%d&quot;,&key); search(root,key); if(fnode==NULL) printf(&quot;Node does not exist&quot;); else  printf(&quot;NOde exists&quot;); Implementing Binary Search Tree ( continued ) printf(&quot;Enter the key of node to be deleted : &quot;); scanf(&quot;%d&quot;,&key); delnode(&root,key); printf(&quot;Inorder traversal after deletion :&quot;); inorder(root); } Output : Enter no of nodes in the tree : 5 Enter the element : 18 Enter the element : 13 Enter the element : 8 Enter the element : 21 Enter the element : 20 Preorder traversal : 18  13  8  21  20 Inorder traversal : 8  13  18  20  21 Postorder traversal : 8  13  20  21  18 Enter the key of node to be searched : 13 Node exists Enter the key of node to be deleted : 20 Inorder traversal after deletion : 8  13  18  21
Arithmetic Expression Tree ,[object Object],[object Object],[object Object],[object Object],[object Object],Example: E xpression Tree for (2 * (a − 1) + (3 * b)) + * * 2 - 3 b a 1 In-Order Traversal of Expression Tree Results In-fix Expression. 2 * ( a – 1 ) + ( 3 * b ) Post-Order Traversal of Expression Tree Results Post-fix Expression. 2 a 1 – *  3 b *  + To convert In-fix Expression to Post-fix expression, First construct an expression tree using infix expression and then do tree traversal in post order.
Graphs A Tree is in fact a special type of graph. Graph is a data structure having a group of nodes connecting with cyclic paths. A Graph contains two finite sets, one is set of nodes is called vertices and other is set of edges. A Graph is defined as G = ( V, E ) where. i) V is set of elements called nodes or vertices. ii) E is set of edges, identified with a unique pair ( v1, v2 ) of nodes. Here ( v1, v2 ) pair denotes that there is an edge from node v1 to node v2. A B C D E Set of vertices = { A, B, C, D, E } Set of edges = { ( A, B ) , ( A, C ) , ( B, D ),    ( C, D ),( C, E ) , ( D, E ) }  A graph in which every edge is undirected is known as Un directed graph. The set of edges are unordered pairs. Edge ( A, B ) and ( B, A ) are treated as same edge. A C D E Un-directed Graph Directed Graph Set of vertices = { A, B, C, D, E } Set of edges = { ( A, B ) , ( A, C ) , ( B, B ), ( B, D ), ( C, A ), ( C, D ), ( C, E ) , ( E, D ) }  A graph in which every edge is directed is known as Directed graph. The set of edges are ordered pairs. Edge ( A, B ) and ( B, A ) are treated as different edges. The edge connected to same vertex ( B, B ) is called loop. Out degree  : no of edges originating at a given node. In degree  : no of edges terminating at a given node. For node C - In degree is 1 and out degree is 3 and  total degree is 4. B
Representation of Graph A B C D E ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Adjacency Matrix A  B  C  D  E  A  0  1  1  1  0 B  1  0  1  1  0 C  1  1  0  1  1 D  1  1  1  0  1 E  0  0  1  1  0 Adjacency Matrix is a bit matrix which  contains entries of only 0 and 1  The connected edge between to vertices is  represented by 1 and absence of edge is  represented by 0. Adjacency matrix of an undirected graph is  symmetric. Linked List A B C D N E B C D NULL A B E NULL D A C D NULL A B E NULL C C D NULL

More Related Content

What's hot

non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of tree
Siddhi Viradiya
 

What's hot (20)

Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Data structure tree- advance
Data structure tree- advanceData structure tree- advance
Data structure tree- advance
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Binary tree and Binary search tree
Binary tree and Binary search treeBinary tree and Binary search tree
Binary tree and Binary search tree
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of tree
 
Tree-In Data Structure
Tree-In Data StructureTree-In Data Structure
Tree-In Data Structure
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Trees
TreesTrees
Trees
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Lec19
Lec19Lec19
Lec19
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
binary search tree
binary search treebinary search tree
binary search tree
 

Viewers also liked

computer notes - Traversal of a binary tree
computer notes - Traversal of a binary treecomputer notes - Traversal of a binary tree
computer notes - Traversal of a binary tree
ecomputernotes
 
Tree data structure
Tree data structureTree data structure
Tree data structure
Dana dia
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)
Arvind Devaraj
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
Navtar Sidhu Brar
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
Aakash deep Singhal
 

Viewers also liked (10)

computer notes - Traversal of a binary tree
computer notes - Traversal of a binary treecomputer notes - Traversal of a binary tree
computer notes - Traversal of a binary tree
 
Non Linear Data Structures
Non Linear Data StructuresNon Linear Data Structures
Non Linear Data Structures
 
Tree Traversals (In-order, Pre-order and Post-order)
Tree Traversals (In-order, Pre-order and Post-order)Tree Traversals (In-order, Pre-order and Post-order)
Tree Traversals (In-order, Pre-order and Post-order)
 
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
TYPES DATA STRUCTURES( LINEAR AND NON LINEAR)....
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Tree data structure
Tree data structureTree data structure
Tree data structure
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 

Similar to Unit8 C

TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
asimshahzad8611
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
Niraj Agarwal
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13
ecomputernotes
 

Similar to Unit8 C (20)

Data Structures
Data StructuresData Structures
Data Structures
 
Trees
TreesTrees
Trees
 
Admissions in india 2015
Admissions in india 2015Admissions in india 2015
Admissions in india 2015
 
Trees in data structrures
Trees in data structruresTrees in data structrures
Trees in data structrures
 
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Mca admission in india
Mca admission in indiaMca admission in india
Mca admission in india
 
Tree
TreeTree
Tree
 
Binary tree and operations
Binary tree and operations Binary tree and operations
Binary tree and operations
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13
 
Data Structure
Data StructureData Structure
Data Structure
 
Trees unit 3
Trees unit 3Trees unit 3
Trees unit 3
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
Tree
TreeTree
Tree
 
Threaded binary tree
Threaded binary treeThreaded binary tree
Threaded binary tree
 

More from arnold 7490 (20)

Les14
Les14Les14
Les14
 
Les13
Les13Les13
Les13
 
Les11
Les11Les11
Les11
 
Les10
Les10Les10
Les10
 
Les09
Les09Les09
Les09
 
Les07
Les07Les07
Les07
 
Les06
Les06Les06
Les06
 
Les05
Les05Les05
Les05
 
Les04
Les04Les04
Les04
 
Les03
Les03Les03
Les03
 
Les02
Les02Les02
Les02
 
Les01
Les01Les01
Les01
 
Les12
Les12Les12
Les12
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
 
Unit 5 Java
Unit 5 JavaUnit 5 Java
Unit 5 Java
 
Unit 4 Java
Unit 4 JavaUnit 4 Java
Unit 4 Java
 
Unit 3 Java
Unit 3 JavaUnit 3 Java
Unit 3 Java
 
Unit 2 Java
Unit 2 JavaUnit 2 Java
Unit 2 Java
 
Unit 1 Java
Unit 1 JavaUnit 1 Java
Unit 1 Java
 

Recently uploaded

2024 May - Clearbit Integration with Hubspot - Greenville HUG.pptx
2024 May - Clearbit Integration with Hubspot  - Greenville HUG.pptx2024 May - Clearbit Integration with Hubspot  - Greenville HUG.pptx
2024 May - Clearbit Integration with Hubspot - Greenville HUG.pptx
Boundify
 
obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...
obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...
obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...
yulianti213969
 
Shots fired Budget Presentation.pdf12312
Shots fired Budget Presentation.pdf12312Shots fired Budget Presentation.pdf12312
Shots fired Budget Presentation.pdf12312
LR1709MUSIC
 

Recently uploaded (20)

Arti Languages Pre Seed Teaser Deck 2024.pdf
Arti Languages Pre Seed Teaser Deck 2024.pdfArti Languages Pre Seed Teaser Deck 2024.pdf
Arti Languages Pre Seed Teaser Deck 2024.pdf
 
Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...
Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...
Understanding Financial Accounting 3rd Canadian Edition by Christopher D. Bur...
 
Thompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptx
Thompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptxThompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptx
Thompson_Taylor_MBBS_PB1_2024-03 (1)- Project & Portfolio 2.pptx
 
2024 May - Clearbit Integration with Hubspot - Greenville HUG.pptx
2024 May - Clearbit Integration with Hubspot  - Greenville HUG.pptx2024 May - Clearbit Integration with Hubspot  - Greenville HUG.pptx
2024 May - Clearbit Integration with Hubspot - Greenville HUG.pptx
 
Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...
Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...
Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...
 
UJJAIN CALL GIRL ❤ 8272964427❤ CALL GIRLS IN UJJAIN ESCORTS SERVICE PROVIDE
UJJAIN CALL GIRL ❤ 8272964427❤ CALL GIRLS IN UJJAIN ESCORTS SERVICE PROVIDEUJJAIN CALL GIRL ❤ 8272964427❤ CALL GIRLS IN UJJAIN ESCORTS SERVICE PROVIDE
UJJAIN CALL GIRL ❤ 8272964427❤ CALL GIRLS IN UJJAIN ESCORTS SERVICE PROVIDE
 
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 MonthsSEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
 
A DAY IN THE LIFE OF A SALESPERSON .pptx
A DAY IN THE LIFE OF A SALESPERSON .pptxA DAY IN THE LIFE OF A SALESPERSON .pptx
A DAY IN THE LIFE OF A SALESPERSON .pptx
 
Top Quality adbb 5cl-a-d-b Best precursor raw material
Top Quality adbb 5cl-a-d-b Best precursor raw materialTop Quality adbb 5cl-a-d-b Best precursor raw material
Top Quality adbb 5cl-a-d-b Best precursor raw material
 
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
 
Marel Q1 2024 Investor Presentation from May 8, 2024
Marel Q1 2024 Investor Presentation from May 8, 2024Marel Q1 2024 Investor Presentation from May 8, 2024
Marel Q1 2024 Investor Presentation from May 8, 2024
 
PHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation Final
 
Falcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business GrowthFalcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business Growth
 
Falcon Invoice Discounting: Aviate Your Cash Flow Challenges
Falcon Invoice Discounting: Aviate Your Cash Flow ChallengesFalcon Invoice Discounting: Aviate Your Cash Flow Challenges
Falcon Invoice Discounting: Aviate Your Cash Flow Challenges
 
obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...
obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...
obat aborsi bandung wa 081336238223 jual obat aborsi cytotec asli di bandung9...
 
Buy gmail accounts.pdf buy Old Gmail Accounts
Buy gmail accounts.pdf buy Old Gmail AccountsBuy gmail accounts.pdf buy Old Gmail Accounts
Buy gmail accounts.pdf buy Old Gmail Accounts
 
Progress Report - Oracle's OCI Analyst Summit 2024
Progress Report - Oracle's OCI Analyst Summit 2024Progress Report - Oracle's OCI Analyst Summit 2024
Progress Report - Oracle's OCI Analyst Summit 2024
 
Shots fired Budget Presentation.pdf12312
Shots fired Budget Presentation.pdf12312Shots fired Budget Presentation.pdf12312
Shots fired Budget Presentation.pdf12312
 
Managerial Accounting 5th Edition by Stacey Whitecotton test bank.docx
Managerial Accounting 5th Edition by Stacey Whitecotton test bank.docxManagerial Accounting 5th Edition by Stacey Whitecotton test bank.docx
Managerial Accounting 5th Edition by Stacey Whitecotton test bank.docx
 
10 Influential Leaders Defining the Future of Digital Banking in 2024.pdf
10 Influential Leaders Defining the Future of Digital Banking in 2024.pdf10 Influential Leaders Defining the Future of Digital Banking in 2024.pdf
10 Influential Leaders Defining the Future of Digital Banking in 2024.pdf
 

Unit8 C

  • 1.
  • 2.
  • 3. struct node { struct node *lchild; char data; struct node *rchild; } *root=NULL; struct node *insert(char a[],int index) { struct node *temp=NULL; if(a[index]!='') { temp = (struct node *)malloc( sizeof(struct node)); temp->lchild = insert(a,2*index+1); temp->data = a[index]; temp->rchild = insert(a,2*index+2); } return temp; } void buildtree(char a[]) { root = insert(a,0); } void inorder(struct node *r){ if(r!=NULL) { inorder(r->lchild); printf(&quot;%5c&quot;,r->data); inorder(r->rchild); } } void preorder(struct node *r){ if(r!=NULL) { printf(&quot;%5c&quot;,r->data); preorder(r->lchild); preorder(r->rchild); } } void postorder(struct node *r){ if(r!=NULL) { postorder(r->lchild); postorder(r->rchild); printf(&quot;%5c&quot;,r->data); } } void main() { char arr[] = { 'A','B','C','D','E','F','G','','','H','', '','','','','','','','','',''}; buildtree(arr); printf(&quot;Pre-order Traversal : &quot;); preorder(root); printf(&quot;In-order Traversal : &quot;); inorder(root); printf(&quot;In-order Traversal : &quot;); inorder(root); } Implementing Binary Tree Output : Preorder Traversal : A B D E H C F G Inorder Traversal : D B H E A F C G Postorder Traversal : D H E B F G C A A B C D E F G H Tree Created :
  • 4.
  • 5. struct node { struct node *lchild; int data; struct node *rchild; }; struct node *fnode=NULL,*parent=NULL; void insert(struct node **p, int n) { if(*p==NULL) { *p=(struct node *)malloc(sizeof(struct node)); (*p)->lchild=NULL; (*p)->data = n; (*p)->rchild=NULL; return; } if(n < (*p)->data) insert(&((*p)->lchild),n); else insert(&((*p)->rchild),n); } void inorder(struct node *t) { if(t!=NULL) { inorder(t->lchild); printf(&quot;%5d&quot;,t->data); inorder(t->rchild); } } void preorder(struct node *t) { if(t!=NULL) { printf(&quot;%5d&quot;,t->data); Implementing Binary Search Tree preorder(t->lchild); preorder(t->rchild); } } void postorder(struct node *t) { if(t!=NULL) { postorder(t->lchild); postorder(t->rchild); printf(&quot;%5d&quot;,t->data); } } void search(struct node *r,int key){ struct node *q; , fnode=NULL; q=r; while(q!=NULL) { if(q->data == key) { fnode = q; return; } parent=q; if(q->data > key) q=q->lchild; else q=q->rchild; } } void delnode ( struct node **r,int key) { struct node *succ,*fnode,*parent; if(*r==NULL) { printf(&quot;Tree is empty&quot;); return; } parent=fnode=NULL;
  • 6. search(*r,key); if(fnode==NULL) { printf(&quot;Node does not exist, no deletion&quot;); return; } if(fnode->lchild==NULL && fnode->rchild==NULL) { if(parent->lchild==fnode) parent->lchild=NULL; else parent->rchild=NULL; free(fnode); return; } if(fnode->lchild==NULL && fnode->rchild!=NULL) { if(parent->lchild==fnode) parent->lchild=fnode->rchild; else parent->rchild=fnode->rchild; free(fnode); return; } if(fnode->lchild!=NULL && fnode->rchild==NULL) { if(parent->lchild==fnode) parent->lchild=fnode->lchild; else parent->rchild=fnode->lchild; free(fnode); return; } Implementing Binary Search Tree ( continued ) if(fnode->lchild!=NULL && fnode->rchild!=NULL) { parent = fnode; succ = fnode->lchild; while(succ->rchild!=NULL){ parent=succ; succ = succ->rchild; } fnode->data = succ->data; free(succ); succ=NULL; parent->rchild = NULL; } } void inorder(struct node *t) { if(t!=NULL) { inorder(t->lchild); printf(&quot;%5d&quot;,t->data); inorder(t->rchild); } } void preorder(struct node *t) { if(t!=NULL) { printf(&quot;%5d&quot;,t->data); preorder(t->lchild); preorder(t->rchild); } }
  • 7. void postorder(struct node *t) { if(t!=NULL) { postorder(t->lchild); postorder(t->rchild); printf(&quot;%5d&quot;,t->data); } } int main() { struct node *root=NULL; int n,i,num,key; printf(&quot;Enter no of nodes in the tree : &quot;); scanf(&quot;%d&quot;,&n); for(i=0;i<n;i++) { printf(&quot;Enter the element : &quot;); scanf(&quot;%d&quot;,&num); insert(&root,num); } printf(&quot;Preorder traversal :&quot;); preorder(root); printf(&quot;Inorder traversal :&quot;); inorder(root); printf(&quot;Postorder traversal :&quot;); postorder(root); printf(&quot;Enter the key of node to be searched : &quot;); scanf(&quot;%d&quot;,&key); search(root,key); if(fnode==NULL) printf(&quot;Node does not exist&quot;); else printf(&quot;NOde exists&quot;); Implementing Binary Search Tree ( continued ) printf(&quot;Enter the key of node to be deleted : &quot;); scanf(&quot;%d&quot;,&key); delnode(&root,key); printf(&quot;Inorder traversal after deletion :&quot;); inorder(root); } Output : Enter no of nodes in the tree : 5 Enter the element : 18 Enter the element : 13 Enter the element : 8 Enter the element : 21 Enter the element : 20 Preorder traversal : 18 13 8 21 20 Inorder traversal : 8 13 18 20 21 Postorder traversal : 8 13 20 21 18 Enter the key of node to be searched : 13 Node exists Enter the key of node to be deleted : 20 Inorder traversal after deletion : 8 13 18 21
  • 8.
  • 9. Graphs A Tree is in fact a special type of graph. Graph is a data structure having a group of nodes connecting with cyclic paths. A Graph contains two finite sets, one is set of nodes is called vertices and other is set of edges. A Graph is defined as G = ( V, E ) where. i) V is set of elements called nodes or vertices. ii) E is set of edges, identified with a unique pair ( v1, v2 ) of nodes. Here ( v1, v2 ) pair denotes that there is an edge from node v1 to node v2. A B C D E Set of vertices = { A, B, C, D, E } Set of edges = { ( A, B ) , ( A, C ) , ( B, D ), ( C, D ),( C, E ) , ( D, E ) } A graph in which every edge is undirected is known as Un directed graph. The set of edges are unordered pairs. Edge ( A, B ) and ( B, A ) are treated as same edge. A C D E Un-directed Graph Directed Graph Set of vertices = { A, B, C, D, E } Set of edges = { ( A, B ) , ( A, C ) , ( B, B ), ( B, D ), ( C, A ), ( C, D ), ( C, E ) , ( E, D ) } A graph in which every edge is directed is known as Directed graph. The set of edges are ordered pairs. Edge ( A, B ) and ( B, A ) are treated as different edges. The edge connected to same vertex ( B, B ) is called loop. Out degree : no of edges originating at a given node. In degree : no of edges terminating at a given node. For node C - In degree is 1 and out degree is 3 and total degree is 4. B
  • 10.