BNode.h
/*
* BNode.h
*
* Created on: 2013年12月27日
* Author: lzh
*/
#ifndef BNODE_H_
#define BNODE_H_
#include <iostream>
namespace std {
class BNode {
private:
int value;
BNode *parent;
BNode *lchild;
BNode *rchild;
public:
void setValue(int& val);
int getValue() const;
void setParent( BNode* parent);
BNode* getParent() const;
void setLchild( BNode* lchild);
BNode* getLchild() const;
void setRchild( BNode* rchild);
BNode* getRchild() const;
bool isHasLeft() const;
bool isHasRight() const;
bool isHasParent() const;
BNode(int& val);
virtual ~BNode();
};
}
#endif
BNode.cpp
/*
* BNode.cpp
*
* Created on: 2013年12月27日
* Author: lzh
*/
#include "BNode.h"
namespace std {
BNode::BNode(int& val) {
// TODO Auto-generated constructor stub
this->value = val;
parent = lchild = rchild = NULL;
}
void BNode::setValue(int& val){
this->value = val;
}
int BNode::getValue() const{
return this->value;
}
void BNode::setParent( BNode* parent){
this->parent = parent;
}
BNode* BNode::getParent() const{
return this->parent;
}
void BNode::setLchild( BNode* lchild){
this->lchild = lchild;
}
BNode* BNode::getLchild() const{
return this->lchild;
}
void BNode::setRchild( BNode* rchild){
this->rchild = rchild;
}
BNode* BNode::getRchild() const{
return this->rchild;
}
bool BNode::isHasLeft() const{
return this->lchild != NULL;
}
bool BNode::isHasRight() const{
return this->rchild != NULL;
}
bool BNode::isHasParent() const{
return this->parent != NULL;
}
BNode::~BNode() {
// TODO Auto-generated destructor stub
}
}
BTree.h
/*
* BTree.h
*
* Created on: 2013年12月27日
* Author: lzh
*/
#ifndef BTREE_H_
#define BTREE_H_
#include "BNode.h"
namespace std {
class BTree {
private:
BNode *root;
BNode *current;
public:
void insert(int val);
BNode* insert(BNode* node,int val);
void pretravel(BNode* nd);
void suftravel(BNode* nd);
void centravel(BNode* nd);
void order();
BTree();
virtual ~BTree();
};
}
#endif
BTree.cpp
/*
* BTree.cpp
*
* Created on: 2013年12月27日
* Author: lzh
*/
#include "BTree.h"
namespace std {
BTree::BTree() {
// TODO Auto-generated constructor stub
this->current = this->root = NULL;
}
void BTree::insert(int val){
if(root == NULL) root = new BNode(val);
else
insert(root,val);
}
BNode* BTree::insert(BNode* node,int val){
if(node == NULL) node = new BNode(val);
else if(node->getValue() < val)
node->setLchild(this->insert(node->getLchild(),val));
else
node->setRchild(this->insert(node->getRchild(),val));
return node;
}
void destroy(BNode* par){
if(par->isHasLeft()){
BNode* left = par->getLchild();
destroy(left);
}
if(par->isHasRight()){
BNode* right = par->getRchild();
destroy(right);
}
delete par;
}
void travel(BNode *bn){
if(bn!=NULL)
cout<<"value:"<<bn->getValue()<<endl;
}
void BTree::pretravel(BNode* nd){
travel(nd);
if(nd->isHasLeft()){
this->current = this->current->getLchild();
pretravel(current);
}
this->current = nd;
if(nd->isHasRight()){
this->current = this->current->getRchild();
pretravel(current);
}
}
void BTree::suftravel(BNode* nd){
suftravel(root->getRchild());
travel(nd);
suftravel(root->getLchild());
}
void BTree::centravel(BNode* nd){
travel(nd);
centravel(root->getLchild());
centravel(root->getRchild());
}
void BTree::order(){
this->current = root;
pretravel(current);
}
BTree::~BTree() {
// TODO Auto-generated destructor stub
destroy(root);
cout<<"二叉树已被销毁"<<endl;
}
}