Technical studies

Technical studies

Share

Photos from Technical studies's post 24/09/2014

Fault detection system for power transmission line project

10/09/2013

Building a binary tree in C
The following program shows how to build a binary tree in a
C program. It uses dynamic memory allocation, pointers and
recursion. A binary tree is a very useful data-structure, since
it allows efficient insertion, searching and deletion in a sorted
list. As such a tree is essentially a recursively defined
structure, recursive programming is the natural and efficient
way to handle it.
tree
empty
node left-branch right-branch
left-branch
tree
right-branch
tree


struct tree_el {
int val;
struct tree_el * right, * left;
};
typedef struct tree_el node;
void insert(node ** tree, node * item) {
if(!(*tree)) {
*tree = item;
return;
}
if(item->valval)
insert(&(*tree)->left, item);
else if(item->val>(*tree)->val)
insert(&(*tree)->right, item);
}
void printout(node * tree) {
if(tree->left) printout(tree->left);
printf("%d\n",tree->val);
if(tree->right) printout(tree->right);
}
void main() {
node * curr, * root;
int i;
root = NULL;
for(i=1;ileft = curr->right = NULL;
curr->val = rand();
insert(&root, curr);
}
printout(root);
}

Want your school to be the top-listed School/college in Coimbatore?
Click here to claim your Sponsored Listing.

Telephone

Address


Malumichampatty
Coimbatore
641032