C Programming Code Examples
C > Sorting Searching Code Examples
find the Total Columns/Vertical Lines of a given Binary Search Tree
/* find the Total Columns/Vertical Lines of a given Binary Search Tree */
/*
40
/ \
20 60
/ \ \
10 30 80
\
90
(40,60,80,20,30,90,10)
(Binary search tree)
*/
#include <stdio.h>
#include <stdlib.h>
struct btnode {
int value;
int col;
struct btnode *l;
struct btnode *r;
};
typedef struct btnode bt;
bt *root,*temp;
bt *new;
int min = 0, max = 0;
bt * create_node();
void display(bt *);
void insert(bt *, bt *);
void columns(bt *);
void main()
{
int j;
while (1)
{
printf("enter the number:");
scanf("%d", &j);
if (j == 0)
{
break;
}
create_node();
new->value = j;
if (root == NULL)
{
root = new;
root->col = 0;
}
else
{
insert(new,root);
}
}
display(root);
columns(root);
printf("\n total number of columns : %d",-min + max + 1);
}
/* creates new node */
bt * create_node()
{
new=(bt *)malloc(sizeof(bt));
new->l = NULL;
new->r = NULL;
}
/* inserts the node into tree */
void insert(bt * new , bt * list)
{
if (new->value>list->value)
{
if (list->r == NULL)
{
list->r = new;
new->col = list->col + 1;
}
else
{
insert (new,list->r);
}
}
if (new->value < list->value)
{
if (list->l == NULL)
{
list->l = new;
new->col = list->col - 1;
}
else
{
insert (new,list->l);
}
}
}
/* displays the elements in the tree using inorder */
void display(bt * list)
{
if (list == NULL)
{
return;
}
display(list->l);
printf("->%d", list->value);
display(list->r);
}
/* counts the number of columns in tree */
void columns(bt * list)
{
if (list == NULL)
{
return;
}
if (list->col < min)
{
min = list->col;
}
if (list->col > max)
{
max = list->col;
}
columns(list->l);
columns(list->r);
}
The break is a keyword in C which is used to bring the program control out of the loop. The break statement is used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:
Allocate memory block. Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. The content of the newly allocated block of memory is not initialized, remaining with indeterminate values. If size is zero, the return value depends on the particular library implementation (it may or may not be a null pointer), but the returned pointer shall not be dereferenced. The "malloc" or "memory allocation" method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form. It doesn't Iniatialize memory at execution time so that it has initializes each block with the default garbage value initially.
Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,
Read formatted data from stdin. Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards. The scanf() function enables the programmer to accept formatted inputs to the application or production code. Moreover, by using this function, the users can provide dynamic input values to the application.
The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.
The sizeof() operator is commonly used in C. It determines the size of the expression or the data type specified in the number of char-sized storage units. The sizeof() operator contains a single operand which can be either an expression or a data typecast where the cast is data type enclosed within parenthesis. The data type cannot only be primitive data types such as integer or floating data types, but it can also be pointer data types and compound data types such as unions and structs.
Relational Operators are the operators used to create a relationship and compare the values of two operands. For example, there are two numbers, 5 and 15, and we can get the greatest number using the greater than operator (>) that returns 15 as the greatest or larger number to the 5. Following are the various types of relational operators in C. Equal To Operator (==) is used to compare both operands and returns 1 if both are equal or the same, and 0 represents the operands that are not equal. Not Equal To Operator (!=) is the opposite of the Equal To Operator and is represented as the (!=) operator. The Not Equal To Operator compares two operands and returns 1 if both operands are not the same; otherwise, it returns 0.
The typedef is a keyword used in C programming to provide some meaningful names to the already existing variable in the C program. It behaves similarly as we define the alias for the commands. In short, we can say that this keyword is used to redefine the name of an already existing variable.
While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to be executed multiple times depending upon a given boolean condition. It can be viewed as a repeating if statement. The while loop is mostly used in the case where the number of iterations is not known in advance. The while loop evaluates the test expression inside the parentheses (). If test expression is true, statements inside the body of while loop are executed. Then, test expression is evaluated again. The process goes on until test expression is evaluated to false. If test expression is false, the loop terminates.
C Language program print code the alternate nodes in a linked list without using recursion. This C program, using iteration, displays the alternate nodes in a linked list. A linked list is
Highest position of a set bit from left to right in a number is said to be highest order set bit of that number. Highest order set bit of any negative integers is 31. Since, highest order...
Predefined standard library functions - such as puts(), gets(), printf(), scanf()... These are the functions which already have a definition in header files (.h files like stdio.h), so we just
Demonstrate multiple loop control variables. This is a testing of converge. Copies one string into another. It copies characters to both the ends, converging at the middle...
C program code 'Concatenate 2 strings' using strcat() library function. Logic to concatenate 2 strings in C Programming. Enter first string, enter second string and Concatenate str1 str2
C program to will prompt user to input three integer numbers and based on the input, it would compare and display greatest number as output. In this program number1, number2