Example of a Binary Tree

I can't remember where I got this code sample from, but I found it to be a good one for understanding b-trees.

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// our data structure (a binary tree node)
typedef struct node{
int data;
struct node* left;
struct node* right;
} node;


// initializes a node struct
node* newNode(int data){
node *node;
node = malloc(sizeof(node));
if (node != NULL){
node->data = data;
node->left = NULL;
node->right = NULL;
}
return(node);
}


// inserts a node into the btree
node* insert(node *node, int data){
if(node == NULL) {
return (newNode(data));
}else{
if(data <= node->data){
node->left = insert(node->left, data);
}else{
node->right = insert(node->right, data);
}
}
return (node);
}

// retreives a node, by it's data
int lookup(node *node, int target) {
if(node == NULL){
return 0;
}else{
if(target == node->data){
return 1;
}else{
if (target < node->data){
return (lookup(node->left, target));
}else{
return (lookup(node->right, target));
}
}
}
}

// prints each item in the btree
void traverse(node *node){
if(node == NULL){
return;
}

printf("data: %i\n", node->data);

if(node->left){
traverse(node->left);
}

if(node->right){
traverse(node->right);
}
}


// releases the entire btree
void empty(node *node){
if(node == NULL){
return;
}

if(node->left){
empty(node->left);
}

if(node->right){
empty(node->right);
}

printf("releasing item %i\n", node->data);
free(node);
}



int main(int argc, char *argv[], char *env[]){
node *root_node;
root_node = insert(NULL, 5);
insert(root_node,7);
insert(root_node,2);
insert(root_node,1);
insert(root_node,10);

printf("3 is in the btree: %s\n", (lookup(root_node,3) == 1 ? "YES" : "NO"));
printf("2 is in the btree: %s\n", (lookup(root_node,2) == 1 ? "YES" : "NO"));


traverse(root_node);

empty(root_node);

return 0;
}



 

 

Add a Comment...

 

Comments

Cheмicаl pееlÑng is а cosмetiÑ ÑrоÑеdurе аÑmed аt rejuvеnаting аnd еlimÑnatÑng skin defeÑts, for which Ñhеmicаl substances аre usеd thаt cаusе a cоntrоllеd ÑhемÑÑаl http://chemicalpeel.in/detailed-chemical-peel-aftercare-instructions - 5/19/2017

This is a good tip particularly to those new to the blogosphere. Short but very precise information? Thanks for sharing this one. A must read article! - 4/30/2017

SalVrolan Tyskland Flynnbieh ReginaldL Tyskland JacelynSt Joeesebod Everton Hildegard AlexRunyo AC Milan JaimeTrow KristinJa Brasilien GemmaTqam - 3/08/2017

- 3/01/2017

podagra alkohol jak leczyc dne moczanowa ziolami http://seeknet.pl/ dna moczanowa co mozna jesc jadlospis przy dnie moczanowej - 2/14/2017

- 2/01/2017

ÐitÑoin Ñrаdes up 8.4%, or $70, at $903 а Ñoin as оf 12:54 p.m. ET. Тuesday's bÑd hаs the cryptoÑurrenÑy hÑghеr for a fоurÑh sÑraÑghÑ sеssion as Ñt Ñontinuеs Ñо rеbоund froм Ñhe shаrp sеll-оff Ñhаt оÑÑurred еarliеr in Jаnuаry fоllоwing nеws thaÑ China begаn аn investigаtion ÑnÑo biÑÑoin exchаngеs Ñn Bеijing and ShаnghaÑ on suspiÑÑоn оf маrÐºÐµÑ Ð¼Ð°nÑpulаtiоn, mоnеy lаundеrÑng, unаuÑhоrÑzеd fмnаnÑÑng, аnd оÑhеr - 1/27/2017