[56] | 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 | |
---|
[2] | 12 | #include <stdio.h> |
---|
| 13 | #include <stdlib.h> |
---|
| 14 | |
---|
| 15 | class btnode |
---|
| 16 | { |
---|
| 17 | protected : |
---|
| 18 | btnode *Left, *Right; |
---|
| 19 | public : |
---|
| 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 | |
---|
| 28 | class btree |
---|
| 29 | { |
---|
| 30 | void reprint(btnode *n); |
---|
| 31 | protected : |
---|
| 32 | btnode *root; |
---|
| 33 | public : |
---|
| 34 | btree() { root=NULL; } |
---|
| 35 | virtual void insert(btnode *node); |
---|
| 36 | virtual void remove(btnode *node); |
---|
| 37 | void print() { reprint(root); } |
---|
| 38 | } ; |
---|
| 39 | |
---|
| 40 | void 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 | |
---|
| 60 | void btree::remove(btnode *node) |
---|
| 61 | { |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | void 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 | |
---|
| 73 | class btnumber : public btnode |
---|
| 74 | { |
---|
| 75 | int num; |
---|
| 76 | public : |
---|
| 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 | |
---|
| 85 | main() |
---|
| 86 | { |
---|
| 87 | btree bt; |
---|
| 88 | for (int i=0;i<20;i++) |
---|
| 89 | bt.insert((btnode *) new btnumber(random(500))); |
---|
| 90 | bt.print(); |
---|