二叉樹:一個父節點只能有兩個或兩個一下孩子節點
二叉搜索樹:在二叉輸的基礎上中序遍歷是按從大到小的順序排列
Node.h:
#ifndef NODE_H
#define NODE_H
template <typename T>
class Node
{
public:
T content;
Node *left;
Node *right;
};
#endif
Tree.h:
/********************************************************************
*@Program: come true the two search tree 注:英語欠佳有些註釋用的中文
*@Author: kingduo
*@Date: 2015/5/17 First Release
**********************************************************************/
#ifndef TREE_H
#define TREE_H
#include "Node.h"
class Tree
{
public:
Tree();
~Tree();
void Insert(int content);
void Remove(int content);
int Find(int content);
int FindMax();
int FindMin();
void Print();
void Clear();
private:
Node <int> *root;
void Print(const Node<int> *node);
const int FindMax(const Node<int> *node);
const int FindMin(const Node<int> *node);
Node<int> *Find(int content, Node<int> *node);
void Remove(int content, Node<int> *&node);
void Clear(Node<int> *node);
};
#endif
Tree.cpp:
#include "Tree.h"
#include <iostream>
using namespace std;
Tree::Tree()
{
root = NULL;
}
Tree::~Tree()
{
Clear(root);
root = NULL;
}
/*the function is insert the content to tree*/
void Tree::Insert(int value)
{
Node<int> *newnode = new Node<int>;
newnode->content = value;
newnode->left = NULL;
newnode->right = NULL;
if (root == NULL)
root = newnode;
else
{
Node<int> *loopnode = root;
while (loopnode != NULL)
{
if (value == loopnode->content)
{
cout<<"The value is exist!"<<endl;
break;
} else
{
if (value < loopnode->content)
{
if (loopnode->left == NULL)
{
loopnode->left = newnode;
break;
}
loopnode = loopnode->left;
} else
{
if (loopnode->right == NULL)
{
loopnode->right = newnode;
break;
}
loopnode = loopnode->right;
}
}
}
}
}
/*********************************************************************************************************************
若用循環而不用遞歸,需要找到其前一個節點,若用遞歸需要指針的指針或指針的引用做爲參數,
以便能夠修改指針的內容,否則的話只是修改了局部變量的值。
**********************************************************************************************************************/
void Tree::Remove(int value)
{
if (root == NULL)
cout<<"The tree is empty."<<endl;
else
Remove(value, root);
}
/**********************************************************************************************************************
the function is remove the content by recursion
此處使用的是指針的引用(原理與指針的指針相似),好處在於不需要尋找前一節點,
只需要刪除當前節點的指向內容,並用其孩子節點代替,
這裏主要注意的一點是爲什麼要用指針的引用(或指針的指針):C/C++的函數裏的參數是局部變量,
即將實參的內容拷貝到一個新自動分配的變量中(在這裏是指針變量),
新創的局部變量的地址(作用域在此函數內)與其實際變量的地址不同,因此若要改變原始變量的內容,
需要得到原始變量的地址,用引用*&node等價於**node,而引用更方便表示,引用表示變量的別名,
即node是*node的別名,因此函數中直接用node變量代替*node表示了,如果用指針的指針要注意帶間接運算符*。
**********************************************************************************************************************/
void Tree::Remove(int value, Node<int> *&node)
{
if (node != NULL)
{
if (value == node->content)
{
if (node->left != NULL && node->right != NULL)
{
node->content = FindMin(node->right);
Remove(node->content, node->right);
} else
{
Node<int> * delnode = node;
node = node->left? node->left : node->right;
delete delnode;
}
} else
{
if (value < node->content)
Remove(value, node->left);
else
Remove(value, node->right);
}
} else
cout<<"No find it."<<endl;
}
/* the function is print the min number by recursion */
const int Tree::FindMax(const Node<int> *node)
{
if (node->right == NULL)
return node->content;
else
FindMax(node->right);
}
int Tree::FindMax()
{
if (root == NULL)
{
cout<<"The Tree is empty!"<<endl;
return -1;
} else
return FindMax(root);
}
/* the function is print the min unmber by recursion */
const int Tree::FindMin(const Node<int> *node)
{
if (node->left == NULL)
return node->content;
else
FindMin(node->left);
}
int Tree::FindMin()
{
if (root == NULL)
{
cout<<"The tree is empty!"<<endl;
return -1;
} else
return FindMin(root);
}
int Tree::Find(int value)
{
if (root == NULL)
{
cout<<"The tree is empty."<<endl;
return -1;
} else
{
Node<int> *node = Find(value, root);
if (node == NULL)
return -1;
else
return node->content;
}
}
/* the function is find the content in tree*/
Node<int> *Tree::Find(int value, Node<int> *node)
{
if (node == NULL)
{
cout<<"Can't find it.The node is not exist."<<endl;
return NULL;
} else
{
if (value == node->content)
return node;
else if (value < node->content)
Find(value, node->left);
else
Find(value, node->right);
}
}
/* the function is print all the tree by recursion and use infix*/
void Tree::Print(const Node<int> *node)
{
if (node == NULL)
return;
else
{
Print(node->left);
cout<<node->content<<endl;
Print(node->right);
}
}
/* print the tree by Print(Node<int> *) function */
void Tree::Print()
{
if (root == NULL)
cout<<"The tree is empty!"<<endl;
else
Print(root);
}
/* clear the tree */
void Tree::Clear(Node<int> *node)
{
if (node != NULL)
{
Clear(node->left);
Clear(node->right);
delete node;
}
}
void Tree::Clear()
{
Clear(root);
root = NULL;
}