source: abuse/trunk/src/imlib/tree.cpp @ 57

Last change on this file since 57 was 56, checked in by Sam Hocevar, 14 years ago
  • Add licensing terms to most C / C++ files (Ref #5).
File size: 1.9 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *
5 *  This software was released into the Public Domain. As with most public
6 *  domain software, no warranty is made or implied by Crack dot Com or
7 *  Jonathan Clark.
8 */
9
10#include "config.h"
11
12#include <stdio.h>
13#include <stdlib.h>
14
15class btnode
16{
17protected :
18  btnode *Left, *Right;
19public :
20          btnode *left()                   { return Left; }
21          btnode *right()                  { return Right; }
22          void    set_right(btnode *right) { Right=right; }
23          void    set_left (btnode *left)  { Left=left;   }
24  virtual int     compare  (btnode *node) = 0;
25  virtual char   *name     ()             = 0;
26} ;
27
28class btree
29{
30  void reprint(btnode *n);
31protected :
32  btnode *root;
33public :
34               btree() { root=NULL; }
35  virtual void insert(btnode *node);
36  virtual void remove(btnode *node);
37  void         print() { reprint(root); }
38} ;
39
40void btree::insert(btnode *node)
41{ btnode *parent,*finder;
42  int from_left;
43  node->set_right(NULL); node->set_left(NULL);
44  if (root)
45  { finder=root;
46    while (finder)
47    { parent=finder;
48      if (finder->compare(node)>0)
49      { from_left=1; finder=finder->left(); }
50      else
51      { from_left=0; finder=finder->right(); }
52    }
53    if (from_left)
54      parent->set_left(node);
55    else
56      parent->set_right(node);
57  } else root=node;
58}
59
60void btree::remove(btnode *node)
61{
62}
63
64void btree::reprint(btnode *n)
65{
66  if (n)
67  { reprint(n->left());
68    printf("%s\n",n->name());
69    reprint(n->right());
70  }
71}
72
73class btnumber : public btnode
74{
75  int num;
76public :
77  btnumber(int x) { num=x; }
78  virtual int     compare  (btnode *node)
79    { if (num<((btnumber *)node)->num) return -1;
80      else return (num> ((btnumber *)node)->num); }
81  virtual char   *name     ()
82    { static char st[20]; sprintf(st,"%d",num); return st;}
83} ;
84
85main()
86{
87  btree bt;
88  for (int i=0;i<20;i++)
89    bt.insert((btnode *) new btnumber(random(500)));
90  bt.print();
Note: See TracBrowser for help on using the repository browser.