二叉树的部分简单实现

简单用c++实现二叉树的一部分:

  1 #include <iostream>
  2 #include<queue>
  3 
  4 #define null NULL
  5 
  6 
  7 //定义二叉树类
  8 template <typename Entry>
  9 class binary_tree
 10 {
 11     protected:
 12         //定义树结点,方便访问,不封装,全部开放
 13         struct binary_node
 14         {
 15             Entry data; //数据域
 16             binary_node* left; //指向左子树的指针域
 17             binary_node* right; //指向右子树的指针域
 18             binary_node(){} //无参数构造函数
 19             binary_node(const Entry& x):data(x),right(null),left(null){} //带参数构造函数
 20         };
 21         
 22         binary_node* root; //指向根结点的指针
 23     
 24     public:
 25         binary_tree();
 26         binary_tree(const binary_tree& bt);
 27         virtual ~binary_tree();
 28         bool empty() const;
 29         int size() const;
 30         int height() const;
 31         void clear();
 32         void preorder(binary_node*root,void(*visit)(Entry&));
 33         void inorder(binary_node*root,void(*visit)(Entry&));
 34         void postorder(binary_node*root,void(*visit)(Entry&));
 35         void level_traversal(binary_node*root,void(*visit)(Entry&))
 36         int insert(const Entry&);
 37         int remove(const Entry&);
 38         binary_tree& operator=(const binary_tree& bt);
 39         
 40 };
 41 
 42 
 43 
 44 template <typename Entry>
 45 binary_tree<Entry>::binary_tree():root(null){}
 46 
 47 template <typename Entry>
 48 bool binary_tree<Entry>::empty() const
 49 {
 50     return (root == null);
 51 }
 52 
 53 
 54 //用前序遍历来获得结点的数目
 55 template <typename Entry>
 56 int binary_tree<Entry>::size() const
 57 {
 58     stack<binary_node*> s;
 59     int size = 0;
 60     binary_node* p = root;
 61     while(p!=null || !s.empty())
 62     {
 63         while(p != null)
 64         {
 65             size++;
 66             s.push(p);
 67             p = p->left;
 68         }
 69         
 70         if(!s.empty())
 71         {
 72             p = s.top();
 73             s.pop();
 74             p = p->right;
 75         }
 76     }
 77     return size;
 78     
 79 }
 80 
 81 
 82 template <typename Entry>
 83 void binary_tree<Entry>::visit(Entry& a)
 84 {
 85     cout<<"current data is: "<<a<<endl;
 86 }
 87 
 88 //先序遍历
 89 template <typename Entry>
 90 void binary_tree<Entry>::preorder(binary_node root,void (*visit)(Entry& bt))
 91 {
 92     if(root != null)
 93     {
 94         (*visit)(root->data);
 95         preorder(root->left,visit);
 96         preorder(root->right,visit);
 97     }
 98 }
 99 
100 //中序遍历
101 template <typename Entry>
102 void binary_tree<Entry>::inorder(binary_node root,void (*visit)(Entry& bt))
103 {
104     if(root != null)
105     {
106         preorder(root->left,visit);
107         (*visit)(root->data);
108         preorder(root->right,visit);
109     }
110 }
111 
112 //后序遍历
113 template <typename Entry>
114 void binary_tree<Entry>::postorder(binary_node *root,void (*visit)(Entry& bt))
115 {
116     if(root != null)
117     {
118         preorder(root->left,visit);
119         preorder(root->right,visit);
120         (*visit)(root->data);
121     }
122 }
123 
124 //利用队列层次遍历
125 template <typename Entry>
126 void binary_tree<Entry>::level_traversal(binary_node *root,void (*visit)(Entry&))
127 {
128     queue<binary_node*> q;
129     if(root != null)
130         q.push(root);
131     while(!q.empty())
132     {
133         (*visit)(q.front()->data);
134         if(q.front()->left)
135             q.push(q.front()->left);
136         if(q.front()->right)
137             q.push(q.front()->right);
138         q.pop();
139     }
140 }

 

posted @ 2018-01-02 13:45  jeavenwong  阅读(313)  评论(0编辑  收藏  举报