# C Program For Pre-order traversal in a Tree.

## Pre-order traversal:

Pre-order traverse need to traverse root, then left, then right. Array, Linked List, Queues, Stacks, etc. are linear traversal where have only one logical way to traverse them. Though trees can be traversed in different ways. That Traversals:

1.  In-order (Left, Root, Right)
2. Preorder (Root, Left, Right)
3. Postorder (Left, Right, Root)

For Pre-order traverse need to traverse root, then left, then right.

tree

Following figure Pre–order traverse  2, 7, 9.

• Input tree, root.
• Step 1: visit root node.
• Step 2: Traverse left sub tree like call Pre-order (tree, root. left).
• Step 3: Traverse right sub tree like call Pre-order (tree, root. right).

Now, Traverse the following tree by following program.

tree

Traverse : 2,7,1,6,5,10,9,8,3,4.

Recent Articles on Binary Tree in the blog   Topic :

1. Introduction Of Binary Tree
2. Traversals such as

## C Implemetation Process:

• 1st need to create node struct where data, *left , *right declared;
• 2nd need to create new node function which take an item and return new node, where firstly need i) memory allocation same as datatype size.

If failed to memory allocation show error or assign data into new node,if left or right child no value then put them as NULL.

• 3rd  to add child left and right need two function both take two input one for node pointer another for left or right child assigned.
• 4th create create_tree() function by create_node ,add_left_child() and right_child() function. This function return root.
• 5th Now use pre_order() recursion function, root  print first, then if left child not null call pre_order() recursively for left node. Again if right child not null  call pre_order()recursively for right node.
• 6th main() function receive root data or value will print.

C Program For Pre-order traversal in a Tree.

#include <stdio.h>

/*create a data type node */

typedef struct node Node;

struct node{

int data;

Node  *left;

Node  *right;

};

/*Create each node and corresponding child.Allocate memory for new node if new node empty. show error either keep item data variable,initialize left and right child into NULL then return new */

Node *create_node (int item) {

Node *new_node = (Node *) malloc(sizeof(Node));

if(new_node == NULL)

{

printf(“Error ! Could not create an tree\n”);

exit(1);

}

new_node -> data = item;

new_node -> left= NULL;

new_node ->right =NULL;

return (new_node);

}

/*add left node corresponding parent node */

void add_left_child( Node *node, Node *child ) {

node->left =child;

}

/*add right node corresponding parent node */

void add_right_child( Node *node, Node *child ) {

node->right =child;

}

/*create tree for node (2,7),(2,9),(7,1),(7,6),(6,5),(6,10),(9,8),(8,3),(8,4)*/

Node *create_tree() {

Node *two = create_node(2);/*create root node: 2 */

Node *seven=create_node(7);/*create root node: 7 */

Node *nine=create_node(9);/*create root node: 9 */

Node *one = create_node(1); /*create parent node 1 */

Node *six = create_node(6);/*create parent node:6 */

Node *five=create_node(5) ;/* similar process rest of all*/

Node *three=create_node(3);

Node *four=create_node(4);

return two;/* return root node*/

} /*print root node 2; */

void pre_order(Node *node){

printf(“%d\n”,node->data);

if(node->left != NULL){

pre-order(node->left);}

else if(node->right != NULL){

pre-order(node->right);}}

int main() {

Node *root =create_tree();

pre_order(root);

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

printf(” “);

return 0;

}

Data Structure more articles

1. PostOrder traverse C Program For Pre_Order traversal in a Tree.
2. In order traverse C program In_order Traversal of a Tree.
3. Post order traversal Post-order traverse of a Tree with C program.

## Frequency Word for IELTS Listening

Frequency Word for IELTS Listening School a. Library  WordSentence1. Shelf 2. Librarian 3. The stacks 4. Return 5. Fine 6. Magazine 7. Copier  8. Overdue  9. Reading room  10. Reference...

## Chapter 8: Gantt chart Project Development in SDLC

Gantt chart Project DevelopmentSchedule (project management) The project scheduleis the tool that communicates what work needs to be performed, which resources of the organization will perform the work and the timeframes in which that work needs to be performed....

## Chapter 7: Feasibility Analysis in Software Develoment Life Cycle.

Feasibility AnalysisWhat is Feasibility Analysis?? An analysisand evaluation of a proposed project to determine if it (1) is technically feasible, (2) is feasible within the estimated cost, and (3) will be profitable for Organization. Feasibility analysis guides the...

## Chapter 6: Data Flow Diagram in Software Development Life Cycle.

Data Flow Diagram What is DFD? A data flow diagram (DFD) is a graphical representation of the "flow" of data through an information system, modelling its process aspects.A DFD is often used as a preliminary step to create an overview of the system, which can later be...

## Chapter 5: System request on SDLC

System Request In most organizations, project initiation begins by preparing a  system request. A  system request is a document that describes the business reasons for building a system and the value that the system is expected to provide.The project sponsor...

## Chapter 4: SDLC design Phase

SDLC design Phase DFD (Design Analysis)Architectural DesignUI DesignDatabase DesignProgram DesignArchitectural design (logical)Network designClient –server designClient designServer designCloud ComputingDatabase designER diagramRelational diagramDDL (not...

# You may find interest following article

## Frequency Word for IELTS Listening

Frequency Word for IELTS Listening School a. Library  WordSentence1. Shelf 2. Librarian 3. The stacks 4. Return 5. Fine 6. Magazine 7. Copier  8. Overdue  9. Reading room  10. Reference room  11. Periodical room  12. Study lounge  13. Catalogue  14....

## Chapter 8: Gantt chart Project Development in SDLC

Gantt chart Project DevelopmentSchedule (project management) The project scheduleis the tool that communicates what work needs to be performed, which resources of the organization will perform the work and the timeframes in which that work needs to be performed. The project scheduleshould reflect all of the work associated with delivering the project on time....

## Chapter 7: Feasibility Analysis in Software Develoment Life Cycle.

Feasibility AnalysisWhat is Feasibility Analysis?? An analysisand evaluation of a proposed project to determine if it (1) is technically feasible, (2) is feasible within the estimated cost, and (3) will be profitable for Organization. Feasibility analysis guides the organization in determining whether to proceed with the project. Feasibility analysis also identifies...

## Chapter 6: Data Flow Diagram in Software Development Life Cycle.

Data Flow Diagram What is DFD? A data flow diagram (DFD) is a graphical representation of the "flow" of data through an information system, modelling its process aspects.A DFD is often used as a preliminary step to create an overview of the system, which can later be elaborated.Show users how data moves between different processes in a system. Figure 1: DFD Symbols...

## Chapter 5: System request on SDLC

System Request In most organizations, project initiation begins by preparing a  system request. A  system request is a document that describes the business reasons for building a system and the value that the system is expected to provide.The project sponsor usually completes this form as part of a formal system project selection process within the...

## Chapter 4: SDLC design Phase

SDLC design Phase DFD (Design Analysis)Architectural DesignUI DesignDatabase DesignProgram DesignArchitectural design (logical)Network designClient –server designClient designServer designCloud ComputingDatabase designER diagramRelational diagramDDL (not now..!!)Program design (physical)Investigating the hardware/software platformPhysical DFDData storageData...

## Chapter 3: SDLC and its Life cycle Phases.

What is SDLC? The systems development life cycle (SDLC), also referred to as the application development life-cycle, is a term used in systems engineering, information systems and software engineering to describe a process for planning, creating, testing, and deploying an information system. Career Paths for System Developers Systems Development Life Cycle Building...

## Chapter 2: SDLC Key Features For SYSTEMS ANALYST.

Once upon a time, software development consisted of a programmer writing code to solve a problem or automate a procedure. Nowadays, systems are so big and complex that teams of architects, analysts, programmers, testers and users must work together to create the millions of lines of custom-written code that drive our enterprises.To manage this, a number of system...

## Chapter 1: System analysis and Design Overview.

System analysis, a method of studying a system by examining its component parts and their interactions. •It provides a framework in which judgments of the experts in different fields can be combined to determine what must be done, and what is the best way to accomplish it in light of current and future needs.  •The system analyst (usually a software engineer or...

## Chapter 4: Concept Of Sampling, Quantization And Resolutions

Concept Of Sampling, Quantization And Resolutions Conversion of analog signal to digital signal: The output of most of the image sensors is an analog signal, and we can not apply digital processing on it because we can not store it. We can not store it because it requires infinite memory to store a signal that can have infinite values. So we have to convert an...

## Chapter 3: Images and Conversions in Digital Image Process

Images And Conversions There are many type of images, and we will look in detail about different types of images, and the color distribution in them. The binary image The binary image as it name states, contain only two pixel values. 0 and 1. In our previous tutorial of bits per pixel, we have explained this in detail about the representation of pixel values to...

## Chapter 2: Concept of Pixel in Digital Image Process

Concept of Pixel Pixel Pixel is the smallest element of an image. Each pixel correspond to any one value. In an 8-bit gray scale image, the value of the pixel between 0 and 255. The value of a pixel at any point correspond to the intensity of the light photons striking at that point. Each pixel store a value proportional to the light intensity at that particular...

Vocabulary to show the sequence: You must write a summary of at least 150 words in response to a specific graph (bar, line, or pie graph), table, chart, or procedure in Writing Task 1 of the IELTS Academic test (how something works, how something is done). This job assesses your ability to choose and report the most important aspects, describe and compare data,...

## Part 5: IELTS Academic Writing Task 1 Formal and Informal expressions.

Formal and Informal expressions and words: You must write a summary of at least 150 words in response to a specific graph (bar, line, or pie graph), table, chart, or procedure on the IELTS Academic test (how something works, how something is done). Few more informal expressions with their formal versions are given below. Since IELTS is a formal test, your writing...

## Part 4: IELTS Academic Writing Task 1 For Graph Comparison Vocabulary

Vocabulary to represent comparison in graphs: Type Word(s) should be used Similar about / almost / nearly / roughly / approximately / around / just about / very nearly / Just over just above / just over / just bigger / just beyond / just across Just short just below / just beneath / just sort / just under / just a little Much more well above / well above / well...

## Part 3: IELTS Academic Writing Task 1 For Vocabulary Date month etc.

IELTS Academic Writing Task 1 For Vocabulary Date month From 1990 to 2000, Commencing from 1980, Between 1995 and 2005, After 2012. By 1995, In 1998, In February, Over the period, During the period, During 2011. In the first half of the year, For the first quarter, The last quarter of the year, During the first decade. In the 80s, In the 1980s, During the next 6...

The structure of the IELTS Academic Writing Task 1 (Report Writing): Introduction: Introduction (never copy word for word from the question) + Overview/ General trend (what the diagrams indicate at a first glance). Reporting Details: Main features in the Details + Comparison and Contrast of the data. (Do not give all the figures.) + Most striking features of the...

## Part 1: IELTS Writing Task 1 For Vocabulary

Vocabulary For Academic IELTS Writing Task 1 (part 1)  Academic IELTS Writing Task 1 question requires you to use several vocabularies to present the data given in a pie/ bar/ line/ mixed graph or to describe a process or a flow chart. Being able to use appropriate vocabularies, presenting the main trend, comparing & contrasting data and presenting thei logical...

## Chapter 1: Introduction To Digital Image Processing (DIP)

Introduction to DIP Signal processing is a discipline in electrical engineering and in mathematics that deals with analysis and processing of analog and digital signals , and deals with storing , filtering , and other operations on signals. These signals include transmission signals , sound or voice signals , image signals , and other signals etc. Out of all these...

## Part 10: DC – Network Addressing and Network Layer Routing

DC - Network Addressing Layer 3 network addressing is one of the major tasks of Network Layer. Network Addresses are always logical i.e. these are software based addresses which can be changed by appropriate configurations. A network address always points to host / node / server or it can represent a whole network. Network address is always configured on network...