-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBinaryTree.h
164 lines (142 loc) · 4.61 KB
/
BinaryTree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Binary Tree (BT) Data Structure
* This BT is designed for Leetcode solution test,
* not safe, not optimized for performance, so not
* for a general usage.
* Author: Yangyao, 2018/1117
*/
#ifndef BINARY_TREE_H_
#define BINARY_TREE_H_
#include "mystd.h"
namespace MYDS{
using namespace MYSTD;
namespace _binary_tree_helper {
/**
* BinaryTreeNode - node type of binary tree
* DataT can be specified as any type when needed.
*/
template<typename DataT>
class BinaryTreeNode{
public:
typedef DataT value_type;
typedef BinaryTreeNode * pointer_type;
BinaryTreeNode(int _val = 0, pointer_type _left = nullptr,
pointer_type _right = nullptr) :
val(_val), left(_left), right(_right){ }
~BinaryTreeNode(){ }
value_type val;
pointer_type left, right;
};
template<typename DataT> class BinaryTree;
template<typename DataT> ostream & operator<<(ostream &os, const BinaryTree<DataT> &that);
/**
* BinaryTree - binary tree, children representation.
* DataT, which is the type assigned to each node, can be
* specified as any type when needed.
* The binary tree holds an extra head node for code-clarity.
*/
template<typename DataT>
class BinaryTree{
public:
typedef BinaryTreeNode<DataT> node_type;
typedef typename node_type::value_type value_type;
typedef typename node_type::pointer_type pointer_type;
/**
* memory controllers
*/
BinaryTree() :head( new node_type ){ }
BinaryTree( pointer_type p ) :BinaryTree(){ copy(p, root()); }
BinaryTree( const vector<value_type> &v, value_type nullval );
BinaryTree( const BinaryTree & that ) :BinaryTree(that.root()){ }
BinaryTree( BinaryTree && that )noexcept :BinaryTree(){ swap( that.root(), root() ); }
~BinaryTree(){ destroy(); delete head; head = nullptr; }
BinaryTree & operator=( const BinaryTree &that );
BinaryTree & operator=( BinaryTree &&that )noexcept;
/**
* print the tree, in a sequential manner, into tream os
* If p is specified, only the subtree rooted in p is printed.
*/
friend ostream & operator<< <value_type>(ostream &os, const BinaryTree &that);
ostream &print(ostream &os, pointer_type p)const;
/**
* get the root node
*/
pointer_type & root(){ return head->right; }
const pointer_type & root()const{ return head->right; }
/**
* destroy a tree rooted in p
* If p is not specified, destroy the whole tree.
*/
void destroy(){ destroy(root()); }
void destroy( pointer_type &p );
/**
* copy a binary tree rooted in s to t
* Note that original tree t is destroyed.
*/
void copy( pointer_type s, pointer_type &t );
protected:
pointer_type head;
};
template<typename DataT>
ostream & operator<<(ostream &os, const BinaryTree<DataT> &that){ return that.print( os, that.root() ); }
template<typename DataT>
BinaryTree<DataT>::BinaryTree( const vector<value_type> &v, value_type nullval ):BinaryTree(){
if( v.empty() ) return;
root() = new node_type( v[0] );
std::queue<pointer_type> sq; sq.push( root() );
int done = 1; int n = v.size();
while( done < n ){
auto p = sq.front(); sq.pop();
if( v[done] != nullval ){
p->left = new node_type(v[done]);
sq.push(p->left);
}
done++;
if( done >= n ) break;
if( v[done] != nullval ){
p->right = new node_type(v[done]);
sq.push(p->right);
}
done++;
}
}
template<typename DataT>
BinaryTree<DataT> & BinaryTree<DataT>::operator=( const BinaryTree &that )
{ destroy(); copy(that.root(), root()); return *this; }
template<typename DataT>
BinaryTree<DataT> & BinaryTree<DataT>::operator=( BinaryTree &&that )noexcept
{ swap(that.root(), root()); return *this; }
template<typename DataT>
ostream & BinaryTree<DataT>::print(ostream &os, pointer_type p)const{
std::queue<pointer_type> sq; sq.push(p);
while( !sq.empty() ){
p = sq.front(); sq.pop();
if( !p ) os << "NULL ";
else{
os << p->val << " ";
sq.push(p->left); sq.push(p->right);
}
}
os << endl;
return os;
}
template<typename DataT>
void BinaryTree<DataT>::destroy( pointer_type &p ){
if( !p ) return;
destroy(p->left); destroy(p->right);
delete p;
p = nullptr;
}
template<typename DataT>
void BinaryTree<DataT>::copy( pointer_type s, pointer_type &t ){
destroy(t);
if( !s )return;
t = new node_type(s->val);
copy( s->left, t->left );
copy( s->right, t->right );
}
} // _binary_tree_helper
using _binary_tree_helper::BinaryTreeNode;
using _binary_tree_helper::BinaryTree;
}
#endif