/*********************************************************************** * binarytree.h * * Header file for a simple inplementation of a binary tree * * Written by Paul Bonamy - 24 January 2010 * Rewritten in pure C - 30 March 2010 ************************************************************************/ #ifndef BINARYTREE_H #define BINARYTREE_H /*********************************************************************** * C doesn't understand classes, so we'll use a struct for the node * * We normally need to refer to a struct specifically as * 'struct Node', rather than just as 'Node'. The typedef allows us * to refer to a Node as a 'BINTREE', which requires rather less typing. ***********************************************************************/ typedef struct Node { // note that we aren't explicitly declaring these public // struct members are always automatically public int value; // value stored in the tree struct Node * left; // left and right children of the node struct Node * right; // 0 of there is no child as yet // note that there's no longer a constructor. We'll have // to set up left = right = 0 on our own from here on out. } BINTREE; /*********************************************************************** * The BinaryTree class used to live here. We can't use classes in C, * so we'll have global functions and a pointer in main, instead. * * Note, however, that I'm leaving function prototypes here * * Passing around a pointer to root seems a little ugly, but it means * we can have as many binary trees as we want, and can do fun things * like passing in a pointer to a node in the middle of the tree ***********************************************************************/ void bintree_insert(BINTREE** root, int v); // insert v into tree rooted at root void bintree_empty(BINTREE** root); // delete contents of tree rooted at root void bintree_print(BINTREE* root); // print contents of tree rooted at root #endif