根据上一章相关知识,编写了BST的代码,最近写C++感觉就是对内存的操作,如果不理解数据在内存是怎么存放的,感觉很难理解链表和树。上代码把!!
#ifndef _TREE_H
#define _TREE_H
#include<iostream>
namespace fuTree{
struct TreeNode{
int m_value;
TreeNode *leftchild;
TreeNode *rightchild;
TreeNode(int x): m_value(x),leftchild(NULL),rightchild(NULL){};
};
class Tree{
public:
Tree();
virtual ~Tree();
public:
void virtual buildTree(const int *a,int len)=0;
//void virtual insertnode(int x,TreeNode **root)=0;
//void virtual deletenode(const TreeNode *n)=0;
};
class BST:public Tree
{
public:
BST();
~BST();
public:
void virtual buildTree(const int *a,int len);
void inserNode(int x);
//void virtual deletenode(const TreeNode *n) ;
void deleteNode(int x);
void printBstToWindow()const;
private:
void printBst(TreeNode *root)const;
void insertnode(int x, TreeNode *&root);
void deletenode(int x, TreeNode *&root);
private:
TreeNode *m_root;
};
}
#endif
#include"tree.h"
fuTree::Tree::Tree()
{}
fuTree::Tree::~Tree()
{}
fuTree::BST::BST()
{
m_root = NULL;
}
fuTree::BST::~BST()
{
delete m_root;
m_root = NULL;
}
void fuTree::BST::insertnode(int x, TreeNode *&root)
{
if (!root )
{
root = new fuTree::TreeNode(x);
}
else if (x > root->m_value)
{
insertnode(x, root->rightchild);
}
else
{
insertnode(x, root->leftchild);
}
}
void fuTree::BST::buildTree(const int *a, int len)
{
if (!len || !a)
{
std::cout << "input arry is empty" <<std:: endl;
throw(1);
}
for (int i = 0; i < len; ++i)
{
insertnode(a[i], m_root);
}
}
void::fuTree::BST::printBst(TreeNode *root) const
{
if (root)
{
printBst(root->leftchild);
std::cout << root->m_value << std::endl;
printBst(root->rightchild);
}
}
void ::fuTree::BST::printBstToWindow() const
{
printBst(m_root);
}
void ::fuTree::BST::inserNode(int x)
{
insertnode(x, m_root);
}
void ::fuTree::BST::deletenode(int x, TreeNode*&root)
{
if (!root )
{
std::cout << "x is not found in this tree" << std::endl;
}
if (x < root->m_value)
{
deletenode(x, root->leftchild);
}
else if (x >root->m_value)
{
deletenode(x, root->rightchild);
}
else
{
if (!root->leftchild &&!root->rightchild )//当前节点是叶子节点
{
root = NULL;
}
else if (!root->leftchild || !root->rightchild)//有一个孩子是NULL
{
if (root->leftchild&&!root->rightchild)
{
fuTree::TreeNode*temp = root->leftchild;
root->m_value = temp->m_value;
root->leftchild = temp->leftchild;
root->rightchild = temp->rightchild;
delete temp;
temp = NULL;
}
else if (!root->leftchild&&root->rightchild)
{
fuTree::TreeNode*temp = root->rightchild;
root->m_value = temp->m_value;
root->leftchild = temp->leftchild;
root->rightchild = temp->rightchild;
delete temp;
temp = NULL;
}
}
else//左右孩子都存在,找到 右子树中最小的节点
{
fuTree::TreeNode *temp = root->rightchild;
while (temp->leftchild)temp = temp->leftchild;
root->m_value = temp->m_value;
deletenode(temp->m_value, root->rightchild);
}
}
}
void fuTree::BST::deleteNode(int x)
{
deletenode(x, m_root);
}
#include"tree.h"
int main(int argc, char**argv)
{
int a[8];
a[0] = 5;
a[1] = 1;
a[2] = 3;
a[3] = 2;
a[4] = 4;
a[5] = 6;
a[6] = 7;
a[7] = 9;
fuTree::BST bst;
bst.buildTree(a, 8);
bst.printBstToWindow();
//bst.inserNode(4);
//bst.printBstToWindow();
bst.deleteNode(7);
bst.printBstToWindow();
}