5.2.1BinTree-模板类
#include "binnode.h"
#include "release.h"
#include <iostream>
#include <memory>
#include "Queue.h"
template < typename T> class BinTree {
protected :
int _size;
virtual int updateHeight ( BinNodePosi ( T) x) ;
void updateHeightAbove ( BinNodePosi ( T) x) ;
public :
BinNodePosi ( T) _root;
BinTree ( ) : _size ( 0 ) , _root ( nullptr ) { }
int size ( ) const { return _size; }
bool empty ( ) const { return ! _root; }
BinNodePosi ( T) root ( ) const { return _root; }
BinNodePosi ( T) insertAsRoot ( T const & e) ;
BinNodePosi ( T) insertAsLC ( BinNodePosi ( T) x, T const & e) ;
BinNodePosi ( T) insertAsRC ( BinNodePosi ( T) x, T const & e) ;
BinNodePosi ( T) attachAsLC ( BinNodePosi ( T) x, BinTree< T> * & S) ;
BinNodePosi ( T) attachAsRC ( BinNodePosi ( T) x, BinTree< T> * & S) ;
int remove ( BinNodePosi ( T) x) ;
BinTree< T> * secede ( BinNodePosi ( T) x) ;
template < typename VST> void travPre ( BinNodePosi ( T) x, VST& visit) { if ( _root) _root- > travPre ( visit) ; }
template < typename VST> void travPost ( const VST& visit) { if ( _root) _root- > travPost ( visit) ; }
template < typename VST> void travLevel ( const VST& visit) { if ( _root) _root- > travLevel ( visit) ; }
template < typename VST> void travIn ( const VST& visit) { if ( _root) _root- > travIn ( visit) ; }
void traverse ( BinNodePosi ( T) x, void ( * ) ( T& ) ) ;
} ;
5.2.2 高度更新
template < typename T>
int BinTree< T> :: updateHeight ( BinNodePosi ( T) x ) {
return x- > height = 1 + max ( stature ( x- > lc ) , stature ( x- > rc ) ) ;
}
template < typename T>
void BinTree< T> :: updateHeightAbove ( BinNodePosi ( T) x ) {
while ( x) { updateHeight ( x) ; x = x- > parent; }
}
5.2.3 节点插入
template < typename T>
BinNodePosi ( T) BinTree< T> :: insertAsRoot ( T const & e)
{
_size = 1 ; return _root = new BinNode< T> ( e) ;
}
template < typename T>
BinNodePosi ( T) BinTree< T> :: insertAsLC ( BinNodePosi ( T) x, T const & e)
{
_size++ ; x- > insertAsLC ( e) ; updateHeightAbove ( x) ; return x- > lc;
}
template < typename T>
BinNodePosi ( T) BinTree< T> :: insertAsRC ( BinNodePosi ( T) x, T const & e)
{
_size++ ; x- > insertAsRC ( e) ; updateHeightAbove ( x) ; return x- > rc;
}
5.2.4 子树接入
template < typename T>
BinNodePosi ( T) BinTree< T> :: attachAsLC ( BinNodePosi ( T) x, BinTree< T> * & S)
{
x- > lc = S- > _root;
x- > lc- > parent = x;
_size + = S- > _size;
updateHeightAbove ( x) ;
S- > _root = nullptr ;
S- > size = 0 ; dtl:: release ( S) ; S = nullptr ; return x;
}
template < typename T>
BinNodePosi ( T) BinTree< T> :: attachAsRC ( BinNodePosi ( T) x, BinTree< T> * & S)
{
x- > rc = S- > _root;
x- > rc- > parent = x;
_size + = S- > _size;
updateHeightAbove ( x) ;
S- > _root = nullptr ;
S- > size = 0 ; dtl:: release ( S) ; S = nullptr ; return x;
}
5.2.5 子树删除
template < typename T>
int BinTree< T> :: remove ( BinNodePosi ( T) x)
{
FromParentTo ( * x ) = nullptr ;
updateHeightAbove ( x- > parent ) ;
int n = removeAt ( x ) ; _size - = n;
return n;
}
template < typename T>
static int removeAt ( BinNodePosi ( T) x)
{
if ( ! x) return 0 ;
int n = 1 + removeAt ( x- > lc) + removeAt ( x- > rc) ;
dtl:: release ( x- > data) ; dtl:: release ( x) ; return n;
}
5.2.6 子树分离
template < typename T> BinTree< T> * BinTree< T> :: secede ( BinNodePosi ( T) x) {
FromParentTo ( * x) = nullptr ;
updateHeightAbove ( x- > parent) ;
BinTree< T> * S= new BinTree< T> ; S- > _root= x; x- > parent= nullptr ;
S- > _size= x- > size ( ) ; _size- = S- > _size; return S;
}
5.2.7 遍历
template < typename T> void BinTree< T> :: traverse ( BinNodePosi ( T) x, void ( * visit ) ( T& ) )
{
if ( ! x ) return ;
visit ( x- > data ) ;
traverse ( x- > lc, visit ) ;
traverse ( x- > rc, visit ) ;
}
template < typename T, typename VST> void travPre ( BinNodePosi ( T) x, VST& visit) {
if ( ! x ) return ;
visit ( x- > data ) ;
travPre ( x- > lc, visit ) ;
travPre ( x- > rc, visit ) ;
}
template < typename T, typename VST> void travPost ( BinNodePosi ( T) x, VST& visit) {
if ( ! x ) return ;
travPost ( x- > lc, visit ) ;
travPost ( x- > rc, visit ) ;
visit ( x- > data ) ;
}
template < typename T, typename VST> void travIn ( BinNodePosi ( T) x, VST& visit) {
if ( ! x ) return ;
travIn ( x- > lc, visit ) ;
visit ( x- > data ) ;
travIn ( x- > rc, visit ) ;
}
template < typename T> template < typename VST> void BinNode< T> :: travLevel ( VST& visit) {
Queue< BinNodePosi ( T) > Q;
Q. enqueue ( this ) ;
while ( ! Q. empty ( ) ) {
BinNodePosi ( T) x = Q. dequeue ( ) ; visit ( x- > data) ;
if ( HasLChild ( * x ) ) Q. enqueue ( x- > lc) ;
if ( HasRChild ( * x ) ) Q. enqueue ( x- > rc) ;
}
}
5.2.8 BinTree-模板
#include "binnode.h"
#include "release.h"
#include <iostream>
#include <memory>
#include "Queue.h"
#define max(a,b) ( a > b ? a : b)
template < typename T> class BinTree {
protected :
int _size;
virtual int updateHeight ( BinNodePosi ( T) x) ;
void updateHeightAbove ( BinNodePosi ( T) x) ;
public :
BinNodePosi ( T) _root;
BinTree ( ) : _size ( 0 ) , _root ( nullptr ) { }
int size ( ) const { return _size; }
bool empty ( ) const { return ! _root; }
BinNodePosi ( T) root ( ) const { return _root; }
BinNodePosi ( T) insertAsRoot ( T const & e) ;
BinNodePosi ( T) insertAsLC ( BinNodePosi ( T) x, T const & e) ;
BinNodePosi ( T) insertAsRC ( BinNodePosi ( T) x, T const & e) ;
BinNodePosi ( T) attachAsLC ( BinNodePosi ( T) x, BinTree< T> * & S) ;
BinNodePosi ( T) attachAsRC ( BinNodePosi ( T) x, BinTree< T> * & S) ;
int remove ( BinNodePosi ( T) x) ;
BinTree< T> * secede ( BinNodePosi ( T) x) ;
template < typename VST> void travPre ( BinNodePosi ( T) x, VST& visit) { if ( _root) _root- > travPre ( visit) ; }
template < typename VST> void travPost ( const VST& visit) { if ( _root) _root- > travPost ( visit) ; }
template < typename VST> void travLevel ( const VST& visit) { if ( _root) _root- > travLevel ( visit) ; }
template < typename VST> void travIn ( const VST& visit) { if ( _root) _root- > travIn ( visit) ; }
void traverse ( BinNodePosi ( T) x, void ( * ) ( T& ) ) ;
} ;
template < typename T> void BinTree< T> :: traverse ( BinNodePosi ( T) x, void ( * visit ) ( T& ) )
{
if ( ! x ) return ;
visit ( x- > data ) ;
traverse ( x- > lc, visit ) ;
traverse ( x- > rc, visit ) ;
}
template < typename T, typename VST> void travPre ( BinNodePosi ( T) x, VST& visit) {
if ( ! x ) return ;
visit ( x- > data ) ;
travPre ( x- > lc, visit ) ;
travPre ( x- > rc, visit ) ;
}
template < typename T, typename VST> void travPost ( BinNodePosi ( T) x, VST& visit) {
if ( ! x ) return ;
travPost ( x- > lc, visit ) ;
travPost ( x- > rc, visit ) ;
visit ( x- > data ) ;
}
template < typename T, typename VST> void travIn ( BinNodePosi ( T) x, VST& visit) {
if ( ! x ) return ;
travIn ( x- > lc, visit ) ;
visit ( x- > data ) ;
travIn ( x- > rc, visit ) ;
}
template < typename T> template < typename VST> void BinNode< T> :: travLevel ( VST& visit) {
Queue< BinNodePosi ( T) > Q;
Q. enqueue ( this ) ;
while ( ! Q. empty ( ) ) {
BinNodePosi ( T) x = Q. dequeue ( ) ; visit ( x- > data) ;
if ( HasLChild ( * x ) ) Q. enqueue ( x- > lc) ;
if ( HasRChild ( * x ) ) Q. enqueue ( x- > rc) ;
}
}
template < typename T>
int BinTree< T> :: updateHeight ( BinNodePosi ( T) x)
{
return x- > height = 1 + max ( stature ( x- > lc) , stature ( x- > rc) ) ;
}
template < typename T>
void BinTree< T> :: updateHeightAbove ( BinNodePosi ( T) x)
{
while ( x) { updateHeight ( x) ; x = x- > parent; }
}
template < typename T>
BinNodePosi ( T) BinTree< T> :: insertAsRoot ( T const & e)
{
_size = 1 ; return _root = new BinNode< T> ( e) ;
}
template < typename T>
BinNodePosi ( T) BinTree< T> :: insertAsLC ( BinNodePosi ( T) x, T const & e)
{
_size++ ; x- > insertAsLC ( e) ; updateHeightAbove ( x) ; return x- > lc;
}
template < typename T>
BinNodePosi ( T) BinTree< T> :: insertAsRC ( BinNodePosi ( T) x, T const & e)
{
_size++ ; x- > insertAsRC ( e) ; updateHeightAbove ( x) ; return x- > rc;
}
template < typename T>
BinNodePosi ( T) BinTree< T> :: attachAsLC ( BinNodePosi ( T) x, BinTree< T> * & S)
{
x- > lc = S- > _root;
x- > lc- > parent = x;
_size + = S- > _size;
updateHeightAbove ( x) ;
S- > _root = nullptr ;
S- > size = 0 ; dtl:: release ( S) ; S = nullptr ; return x;
}
template < typename T>
BinNodePosi ( T) BinTree< T> :: attachAsRC ( BinNodePosi ( T) x, BinTree< T> * & S)
{
x- > rc = S- > _root;
x- > rc- > parent = x;
_size + = S- > _size;
updateHeightAbove ( x) ;
S- > _root = nullptr ;
S- > size = 0 ; dtl:: release ( S) ; S = nullptr ; return x;
}
template < typename T>
int BinTree< T> :: remove ( BinNodePosi ( T) x)
{
FromParentTo ( * x ) = nullptr ;
updateHeightAbove ( x- > parent ) ;
int n = removeAt ( x ) ; _size - = n;
return n;
}
template < typename T>
static int removeAt ( BinNodePosi ( T) x)
{
if ( ! x) return 0 ;
int n = 1 + removeAt ( x- > lc) + removeAt ( x- > rc) ;
dtl:: release ( x- > data) ; dtl:: release ( x) ; return n;
}
template < typename T> BinTree< T> * BinTree< T> :: secede ( BinNodePosi ( T) x) {
FromParentTo ( * x) = nullptr ;
updateHeightAbove ( x- > parent) ;
BinTree< T> * S= new BinTree< T> ; S- > _root= x; x- > parent= nullptr ;
S- > _size= x- > size ( ) ; _size- = S- > _size; return S;
}
5.2.9 BIntree.h
#include "binnode.h"
#include "release.h"
#include <iostream>
#include <memory>
#include "queue.h"
template < typename T> class BinTree {
protected :
int _size;
virtual int updateHeight ( BinNodePosi ( T) x) ;
void updateHeightAbove ( BinNodePosi ( T) x) ;
public :
BinNodePosi ( T) _root;
BinTree ( ) : _size ( 0 ) , _root ( nullptr ) { }
int size ( ) const { return _size; }
bool empty ( ) const { return ! _root; }
BinNodePosi ( T) root ( ) const { return _root; }
BinNodePosi ( T) insertAsRoot ( T const & e) ;
BinNodePosi ( T) insertAsLC ( BinNodePosi ( T) x, T const & e) ;
BinNodePosi ( T) insertAsRC ( BinNodePosi ( T) x, T const & e) ;
BinNodePosi ( T) attachAsLC ( BinNodePosi ( T) x, BinTree< T> * & S) ;
BinNodePosi ( T) attachAsRC ( BinNodePosi ( T) x, BinTree< T> * & S) ;
int remove ( BinNodePosi ( T) x) ;
template < typename VST> void travPre ( BinNodePosi ( T) x, VST& visit) { if ( _root) _root- > travPre ( visit) ; }
template < typename VST> void travPost ( const VST& visit) { if ( _root) _root- > travPost ( visit) ; }
template < typename VST> void travLevel ( const VST& visit) { if ( _root) _root- > travLevel ( visit) ; }
template < typename VST> void travIn ( const VST& visit) { if ( _root) _root- > travIn ( visit) ; }
void traverse ( BinNodePosi ( T) x, void ( * ) ( T& ) ) ;
} ;
template < typename T> void BinTree< T> :: traverse ( BinNodePosi ( T) x, void ( * visit ) ( T& ) )
{
if ( ! x ) return ;
visit ( x- > data ) ;
traverse ( x- > lc, visit ) ;
traverse ( x- > rc, visit ) ;
}
template < typename T, typename VST> void travPre ( BinNodePosi ( T) x, VST& visit) {
if ( ! x ) return ;
visit ( x- > data ) ;
travPre ( x- > lc, visit ) ;
travPre ( x- > rc, visit ) ;
}
template < typename T, typename VST> void travPost ( BinNodePosi ( T) x, VST& visit) {
if ( ! x ) return ;
travPost ( x- > lc, visit ) ;
travPost ( x- > rc, visit ) ;
visit ( x- > data ) ;
}
template < typename T, typename VST> void travIn ( BinNodePosi ( T) x, VST& visit) {
if ( ! x ) return ;
travIn ( x- > lc, visit ) ;
visit ( x- > data ) ;
travIn ( x- > rc, visit ) ;
}
template < typename T> template < typename VST> void BinNode< T> :: travLevel ( VST& visit) {
Queue< BinNodePosi ( T) > Q;
Q. enqueue ( this ) ;
while ( ! Q. empty ( ) ) {
BinNodePosi ( T) x = Q. dequeue ( ) ; visit ( x- > data) ;
if ( HasLChild ( * x ) ) Q. enqueue ( x- > lc) ;
if ( HasRChild ( * x ) ) Q. enqueue ( x- > rc) ;
}
}
template < typename T>
int BinTree< T> :: updateHeight ( BinNodePosi ( T) x)
{
return x- > height = 1 + max ( stature ( x- > lc) , stature ( x- > rc) ) ;
}
template < typename T>
void BinTree< T> :: updateHeightAbove ( BinNodePosi ( T) x)
{
while ( x) { updateHeight ( x) ; x = x- > parent; }
}
template < typename T>
BinNodePosi ( T) BinTree< T> :: insertAsRoot ( T const & e)
{
_size = 1 ; return _root = new BinNode< T> ( e) ;
}
template < typename T>
BinNodePosi ( T) BinTree< T> :: insertAsLC ( BinNodePosi ( T) x, T const & e)
{
_size++ ; x- > insertAsLC ( e) ; updateHeightAbove ( x) ; return x- > lc;
}
template < typename T>
BinNodePosi ( T) BinTree< T> :: insertAsRC ( BinNodePosi ( T) x, T const & e)
{
_size++ ; x- > insertAsRC ( e) ; updateHeightAbove ( x) ; return x- > rc;
}
template < typename T>
BinNodePosi ( T) BinTree< T> :: attachAsLC ( BinNodePosi ( T) x, BinTree< T> * & S)
{
x- > lc = S- > _root;
x- > lc- > parent = x;
_size + = S- > _size;
updateHeightAbove ( x) ;
S- > _root = nullptr ;
S- > size = 0 ; dtl:: release ( S) ; S = nullptr ; return x;
}
template < typename T>
BinNodePosi ( T) BinTree< T> :: attachAsRC ( BinNodePosi ( T) x, BinTree< T> * & S)
{
x- > rc = S- > _root;
x- > rc- > parent = x;
_size + = S- > _size;
updateHeightAbove ( x) ;
S- > _root = nullptr ;
S- > size = 0 ; dtl:: release ( S) ; S = nullptr ; return x;
}
template < typename T>
int BinTree< T> :: remove ( BinNodePosi ( T) x)
{
FromParentTo ( * x ) = nullptr ;
updateHeightAbove ( x- > parent ) ;
int n = removeAt ( x ) ; _size - = n;
return n;
}
template < typename T>
static int removeAt ( BinNodePosi ( T) x)
{
if ( ! x) return 0 ;
int n = 1 + removeAt ( x- > lc) + removeAt ( x- > rc) ;
dtl:: release ( x- > data) ; dtl:: release ( x) ; return n;
}
5.2.10 BInTree.h测试
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "bintree.h"
#include "vector.h"
using namespace std;
template < typename T> void returnValue ( T& a)
{
cout << "return_value: " << a << endl;
}
int main ( int argc, char * argv[ ] )
{
BinTree< char > bt_test;
bt_test. insertAsRoot ( 'b' ) ;
bt_test. insertAsLC ( bt_test. root ( ) , 'a' ) ;
bt_test. insertAsRC ( bt_test. root ( ) , 'f' ) ;
bt_test. insertAsLC ( bt_test. root ( ) - > rc, 'd' ) ;
bt_test. insertAsRC ( bt_test. root ( ) - > rc, 'g' ) ;
bt_test. insertAsLC ( bt_test. root ( ) - > rc- > lc, 'c' ) ;
bt_test. insertAsRC ( bt_test. root ( ) - > rc- > lc, 'e' ) ;
void ( * visit) ( char & ) = & returnValue;
bt_test. traverse ( bt_test. root ( ) , visit) ;
cout << "modifed:" << endl;
bt_test. remove ( bt_test. root ( ) - > rc) ;
bt_test. traverse ( bt_test. root ( ) , visit) ;
return 0 ;
}