- 论坛徽章:
- 0
|
本帖最后由 1210603696 于 2013-08-21 20:15 编辑
我写了一个类 这是类声明:- #ifndef _NAME_SPACE_ZOUXIAOHANG_
- #define _NAME_SPACE_ZOUXIAOHANG_
- #include <iostream>
- #include <iomanip>
- #include <queue>
- #include <assert.h>
- template <class T>
- class binary_search_tree
- {
- private:
- typedef struct node_t
- {
- T value;
- node_t *left;
- node_t *right;
- } node, *tree;
- public:
- typedef node *iterator;
- typedef const node *const_iterator;
- private:
- //******
- tree m_root;
- size_t m_num;
- //******
- binary_search_tree(const binary_search_tree<T>& t);
- binary_search_tree& operator = (const binary_search_tree<T>& t);
- void _order(const tree t, const int ctrl);//ctrl: 0:pre_order 1:in_order 2:post_order
- void _display_tree(const_iterator p, int depth, int ctrl) const;//ctrl:0=root 1=left 2=right
- void _make_empty(iterator p);
- node* _find_node(iterator p, T x);
- node* _insert_node(iterator p, T x);
- node* _delete_node(iterator p, T x);
- public:
- binary_search_tree():m_root(NULL), m_num(0){}
- ~binary_search_tree();
- iterator find_node(T x);
- iterator find_max();
- iterator find_min();
- bool insert_node(T x);
- iterator delete_node(T x);
- void make_empty();
- bool empty();
- size_t size();
- void pre_order();
- void in_order();
- void post_order();
- void level_order();
- const_iterator root();
- void display_tree() const;//ctrl:0=root 1=left 2=right
- void display_tree();//ctrl:0=root 1=left 2=right
- };
- template <class T>
- std::ostream& operator<<(std::ostream& os, const binary_search_tree<T>& t);
- #endif
复制代码 下面是实现文件:- #include "./binary_search_tree.h"
- template <class T>
- void binary_search_tree<T>::_order(const tree t, const int ctrl)//ctrl: 0:pre_order 1:in_order 2:post_order
- {
- if(0 == ctrl)
- {
- if(NULL != t)
- {
- std::cout<<t->value<<" ";
- _order(t->left, 0);
- _order(t->right, 0);
- }
- }
- else if(1 == ctrl)
- {
- if(NULL != t)
- {
- _order(t->left, 1);
- std::cout<<t->value<<" ";
- _order(t->right, 1);
- }
- }
- else if(2 == ctrl)
- {
- if(NULL != t)
- {
- _order(t->left, 2);
- _order(t->right, 2);
- std::cout<<t->value<<" ";
- }
- }
- }
- template <class T>
- void binary_search_tree<T>::_display_tree(const_iterator p, int depth, int ctrl) const//ctrl:0=root 1=left 2=right
- {
- if(NULL == p)
- return ;
- std::cout<<std::setw(depth);
- if(0 == ctrl)
- std::cout<<"root:";
- else if(1 == ctrl)
- std::cout<<"left";
- else if(2 == ctrl)
- std::cout<<"right";
- std::cout<<p->value<<std::endl;
- _display_tree(p->left, depth+6, 1);
- _display_tree(p->right, depth+6, 2);
- }
- template <class T>
- void binary_search_tree<T>::_make_empty(iterator p)
- {
- if(NULL != p)
- {
- _make_empty(p->left);
- _make_empty(p->right);
- delete p;
- }
- }
- template <class T>
- typename binary_search_tree<T>::iterator binary_search_tree<T>::_insert_node(iterator p, T x)
- {
- if(NULL == p)
- {
- p = new node;
- assert(NULL != p);
- p->value = x;
- p->left = p->right = NULL;
- ++m_num;
- }
- else if(x < p->value)
- p->left = _insert_node(p->left, x);
- else if(x > p->value)
- p->right = _insert_node(p->right, x);
- return p;
- }
- template <class T>
- typename binary_search_tree<T>::iterator binary_search_tree<T>::_delete_node(iterator p, T x)
- {
- if(NULL == p)
- return NULL;
- if(x < p->value)
- p->left = _delete_node(p->left, x);
- else if(x > p->value)
- p->right = _delete_node(p->right, x);
- else
- {
- iterator tmp = NULL;
- if(p->left && p->right)// p has two childs
- {
- tmp = m_root;
- m_root = p->right;
- p->value = find_min()->value;
- m_root = tmp;
- p->right = _delete_node(p->right, p->value);
- }
- else
- {
- tmp = p;
- if(!p->left)
- p = p->right;
- else if(!p->right)
- p = p->left;
- delete tmp;
- --m_num;
- }
- }
- return p;
- }
- template <class T>
- typename binary_search_tree<T>::iterator binary_search_tree<T>::_find_node(iterator p, T x)
- {
- if(NULL == p)
- return NULL;
- if(x < p->value)
- return _find_node(p->left, x);
- else if(x > p->value)
- return _find_node(p->right, x);
- else
- return p;
- }
- template <class T>
- binary_search_tree<T>::~binary_search_tree()
- {
- make_empty();
- }
- template <class T>
- typename binary_search_tree<T>::iterator binary_search_tree<T>::find_node(T x)
- {
- return _find_node(m_root, x);
- }
- template <class T>
- typename binary_search_tree<T>::iterator binary_search_tree<T>::find_max()
- {
- node *p = m_root;
- while(NULL != p && NULL != p->right)
- p = p->right;
- return p;
- }
- template <class T>
- typename binary_search_tree<T>::iterator binary_search_tree<T>::find_min()
- {
- node *p = m_root;
- while(NULL != p && NULL != p->left)
- p = p->left;
- return p;
- }
- template <class T>
- bool binary_search_tree<T>::insert_node(T x)
- {
- return (m_root = _insert_node(m_root, x)) == NULL ? false : true;
- }
- template <class T>
- typename binary_search_tree<T>::iterator binary_search_tree<T>::delete_node(T x)
- {
- return _delete_node(m_root, x);
- }
- template <class T>
- void binary_search_tree<T>::make_empty()
- {
- _make_empty(m_root);
- m_root = NULL;
- m_num = 0;
- }
- template <class T>
- bool binary_search_tree<T>::empty()
- {
- return m_num == 0;
- }
- template <class T>
- size_t binary_search_tree<T>::size()
- {
- return m_num;
- }
- template <class T>
- void binary_search_tree<T>::pre_order()
- {
- if(NULL != m_root)
- _order(m_root, 0);
- }
- template <class T>
- void binary_search_tree<T>::in_order()
- {
- if(NULL != m_root)
- _order(m_root, 1);
- }
- template <class T>
- void binary_search_tree<T>::post_order()
- {
- if(NULL != m_root)
- _order(m_root, 2);
- }
- template <class T>
- void binary_search_tree<T>::level_order()
- {
- if(NULL == m_root)
- return ;
- iterator p = m_root;
- std::queue<node*> q;
- q.push(p);
- while(!q.empty())
- {
- p = q.front();
- std::cout<<p->value<<" ";
- if(NULL != p->left)
- q.push(p->left);
- if(NULL != p->right)
- q.push(p->right);
- q.pop();
- }
- }
- template <class T>
- typename binary_search_tree<T>::const_iterator binary_search_tree<T>::root()
- {
- return m_root;
- }
- template <class T>
- void binary_search_tree<T>::display_tree() const
- {
- _display_tree(m_root, 2, 0);
- }
- template <class T>
- void binary_search_tree<T>::display_tree()
- {
- _display_tree(m_root, 2, 0);
- }
- template <class T>
- std::ostream& operator<<(std::ostream& os, const binary_search_tree<T>& t)
- {
- t.display_tree();
- return os;
- }
复制代码 我写了一个例程来测试我的binary_tree:- #include "binary_search_tree.h"
- int main(int argc, char const *argv[])
- {
- binary_search_tree<int> t;
- binary_search_tree<int>::iterator it;
- t.make_empty();
- t.insert_node(10);
- return 0;
- }
复制代码 我编译链接的时候codeblock给出了下面错误:
obj\Debug\main.o||In function `main'
C:\Users\zxh\Desktop\test\main.cpp|7|undefined reference to `binary_search_tree<int>::make_empty()'|
C:\Users\zxh\Desktop\test\main.cpp|8|undefined reference to `binary_search_tree<int>::insert_node(int)'|
C:\Users\zxh\Desktop\test\main.cpp|9|undefined reference to `binary_search_tree<int>::~binary_search_tree()'|
C:\Users\zxh\Desktop\test\main.cpp|9|undefined reference to `binary_search_tree<int>::~binary_search_tree()'|
||=== Build finished: 4 errors, 0 warnings ===|
我找了很久实在是不知道为什么会链接失败,我明明生成了binary_tree.o并链接进来了啊,在ubuntu下g++ binary_tree.o main.c -o main也提示同样的问题 |
|