Binary Search Tree using dynamic memory allocation (URGENT request for example) 
Author Message
 Binary Search Tree using dynamic memory allocation (URGENT request for example)

----------------------------------------------------------------------------
-----
Does anybody have an example of a
Binary Search Tree which uses dynamic memory allocation ???

If so, could they PRETTY PLEASE (with sugar on top)
be so kind as to post it immediately ???

If so,
            Thanks kindly (in advance)
            You've truely made my day !!!

:-)
----------------------------------------------------------------------------
-----



Fri, 09 Mar 2001 03:00:00 GMT  
 Binary Search Tree using dynamic memory allocation (URGENT request for example)
"The C Programming Language", written by Kernigan and Ritchie;

Has a nice binary tree example. They provide the code and a great
description.

Look in the section under structures i.e. pointers to structures; or look
under recursive function calls. I can't  remember exactly where it is but I
believe its under one of these.

--
"The opinions expressed herein are mine and not my employer".

Mike Kleiman
Web Application Developer
Lucent Technologies
(407) 812-5307



----------------------------------------------------------------------------

Quote:
> -----
> Does anybody have an example of a
> Binary Search Tree which uses dynamic memory allocation ???

> If so, could they PRETTY PLEASE (with sugar on top)
> be so kind as to post it immediately ???

> If so,
>             Thanks kindly (in advance)
>             You've truely made my day !!!

> :-)

----------------------------------------------------------------------------
Quote:
> -----



Fri, 09 Mar 2001 03:00:00 GMT  
 Binary Search Tree using dynamic memory allocation (URGENT request for example)
Hi all !!!

Thankyou all for helping me in my time of dispair
(especially to those who gave tips and web sites).
I have now finished.

Yay!

For those who are curious (& for those who are
interested in seeing an example), the (properly
working) program's as follows.

Thanks again,

Kelly.

/*
Purpose:
To implement a BINARY SEARCH
     TREE using dynamic memory
     allocation
  ...where a node in the tree is used
  to store a person's identification
  number, first name, surname, and age.

Files:
main.c
binarytree.c
binarytree.h
binarytree.dta
*/

/*************** main.c **************/
#include <stdio.h>
#include "binarytree.h"

/* functions prototypes */
extern void initialise(struct node **);
extern int empty(struct node *);
extern struct node *insert(struct node **, struct person);
extern void print(struct node *);

main(int argc, char *argv[])
{
        FILE *file;
        struct node *tree;   /* tree is the pointer to the root node */
        struct person p;

        if(argc != 2)
        {
                printf("usage: %s <filename>\n", argv[0]);
                exit(1);
        }

        if((file = fopen(argv[1], "r")) == NULL)
        {
                printf("fopen failed on %s\n", argv[1]);
                exit(1);
        }

        initialise(&tree);
        if(empty(tree))
                printf("Binary tree is empty\n");

        while(fscanf(file, "%d%s%s%d", &p.id, p.firstname, p.surname,
&p.age) != EOF)
        {
                if(insert(&tree, p) == NULL)
                        printf("insert failed, id %d exists\n", p.id);
                print(tree);
        }
        fclose(file);

Quote:
}

/*************** binarytree.c **************/
include <stdio.h>
#include "binarytree.h"

void initialise(struct node **);
int empty(struct node *);
struct node *insert(struct node **, struct person);
void print(struct node *);
void print_tree(struct node *);

void initialise(struct node **tree)
{
 *tree = NULL;

Quote:
}

int empty(struct node *tree)
{
 if(tree == NULL)
  return(0);

Quote:
}

struct node *insert(struct node **tree, struct person data)
{
 struct node *temp, *previous, *current;

 temp = (struct node *)malloc(sizeof(struct node));

 temp->data = data;
 temp->left = NULL;
 temp->right = NULL;

 previous = NULL;
 current = *tree;

 while(current != NULL)
 {
  if(temp->data.id == current->data.id)
   return(NULL);

  previous = current;

  if(temp->data.id < current->data.id)
   current = current->left;
  else
   current = current->right;
 }

 if(previous == NULL)
  *tree = temp;
 else
 {
  if(temp->data.id < previous->data.id)
   previous->left = temp;
  else
   previous->right = temp;
 }

Quote:
}

void print(struct node *tree)
{
 print_tree(tree);

 printf("\n");

Quote:
}

void print_tree(struct node *tree)
{
 if(tree != NULL)
 {
  print_tree(tree->left);
  printf("%i ", tree->data.id);
  print_tree(tree->right);
 }

Quote:
}

/*************** binarytree.h **************/
#define FIRSTNAME_SIZE 20
#define SURNAME_SIZE 30

struct person
{
 int id;
 char firstname[FIRSTNAME_SIZE];
 char surname[SURNAME_SIZE];
 int age;

Quote:
};

struct node
{
 struct person data;
 struct node *left;
 struct node *right;

Quote:
};

/*************** binarytree.dta **************/
555 Jack Aroo 22
222 Erin Burns 21
222 Kate Butcher 18
777 Dave Nue 21
111 Mary Montana 19
888 Anne Telope 17
666 Lizz Ard 22
333 Erin Burnside 20
444 Jack Harue 20


Sat, 10 Mar 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Linked List using dynamic memory allocation (URGENT request for example)

2. Tree memory allocation, pool memory allocation

3. Proposal: alt.binaries.examples.vb4 , alt.binaries.examples.vcpp , alt.binaries.examples.java

4. K&R binary tree example using tsearch?

5. Multi-dimensional array using dynamic memory allocation

6. Multi-dimensional array using dynamic memory allocation

7. AVL tree (Binary Balanced tree) using C or C++

8. Binary Search Trees

9. Binary Search Trees

10. Binary search tree

11. binary search tree problem

12. searching for a structure in a binary tree

 

 
Powered by phpBB® Forum Software