Linux C++ 二叉查找树

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;
    }






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值