/*********************************************************** Copyright 2003 Rick Miller - Pulp Free Press This source code accompanies the text C++ For Artists and is provided for instructional purposes only. No warranty concerning the quality of the code is expressed or implied. You are free to use this code in your programs so long as this copyright notice is included in its entirety. **********************************************************/ #ifndef BTREE_H #define BTREE_H #include "node.h" #include "stack.h" #include template class BTree{ public: BTree(); ~BTree(); void Insert(T item); void List_PreOrder(); void List_InOrder(); void List_PostOrder(); private: bool r,l; Node *root; Stack *> its_stack; int node_count; void Insert(T item, Node *tree, Node *parent); void List_PreOrder(Node *tree, Node *parent); void List_InOrder(Node *tree, Node *parent); void List_PostOrder(Node *tree, Node *parent); void Delete_Tree(Node *tree); void Visit(Node *the_node); void List_PreOrder(Node *tree); void List_InOrder(Node *tree); void List_PostOrder(Node *tree); }; template BTree::BTree():node_count(0), root(NULL){} template BTree::~BTree(){ Delete_Tree(root); } template void BTree::Delete_Tree(Node *tree){ if(tree){ its_stack.Push(tree); } while(!its_stack.IsEmpty()){ if(tree->left_child) its_stack.Push(tree->left_child); if(tree->right_child) its_stack.Push(tree->right_child); tree = its_stack.Pop(); delete tree; } } template void BTree::Insert(T item){ Insert(item, root, root); } template void BTree::Insert(T item, Node *tree, Node *parent){ if(node_count == 0){ root = new Node(parent); root->item = item; root->number_of_items++; node_count++; } else if(!tree){ tree = new Node(parent); tree->item = item; tree->number_of_items++; if(r) parent->right_child = tree; else if(l) parent->left_child = tree; node_count++; } else if(item == tree->item) tree->number_of_items++; else if(item < tree->item){ r = false; l = true; Insert(item, tree->left_child, tree); } else if(item > tree->item){ r = true; l = false; Insert(item, tree->right_child, tree); } } template void BTree::Visit(Node *the_node){ cout<item; } template void BTree::List_PreOrder(){ List_PreOrder(root); } template void BTree::List_InOrder(){ List_InOrder(root); } template void BTree::List_PostOrder(){ List_PostOrder(root); } template void BTree::List_PreOrder(Node *tree){ if(tree) its_stack.Push(tree); while(!its_stack.IsEmpty()){ tree = its_stack.Pop(); Visit(tree); if(tree->right_child) its_stack.Push(tree->right_child); if(tree->left_child) its_stack.Push(tree->left_child); } } template void BTree::List_InOrder(Node *tree){ if(tree){ its_stack.Push(tree->left_child); } while(!its_stack.IsEmpty()){ tree = its_stack.Pop(); Visit(tree); if(tree->right_child) its_stack.Push(tree->right_child); if(tree) its_stack.Push(tree); } } template void BTree::List_PostOrder(Node *tree){ if(tree){ its_stack.Push(tree->left_child); } while(!its_stack.IsEmpty()){ tree = its_stack.Pop(); Visit(tree); if(tree) its_stack.Push(tree); if(tree->right_child) its_stack.Push(tree->right_child); } } #endif