二叉排序树(Binary Sort Tree)又称二叉查找树。 它或者是一棵空树;或者是具有下列性质的二叉树:
(1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;
(2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值;
(3)左、右子树也分别为二叉排序树;
--如果有两个相等的元素, 可根据情况 自定义位置。。!
实现了, 二叉排序树的 构造(递归和 非递归)、查找、插入。
- //#include "SortedTree.h"
- #include <iostream>
- using namespace std;
- struct Node
- {
- int data;
- Node *left;
- Node *right;
- Node():data(-1),left(NULL),right(NULL){}
- Node(int num):data(num),left(NULL),right(NULL){}
- };
- class BinarySortTree
- {
- public:
- BinarySortTree(int num[], int len);
- void InsertNode_Loop(int num); //非递归插入
- void InsertNode(int num); //递归插入
- void PrintTree(); //打印树
- Node* Search(int num); //查找节点
- private:
- void InsertNode(Node *curNode, int num); //递归插入
- void PrintTree(Node *curNode); //打印树, 后根遍历二叉排序树
- Node *Search(Node *curNode, int num); //查找节点
- Node *rootNode;
- };
- BinarySortTree::BinarySortTree(int a[],int len):rootNode(NULL)
- {
- for (int i = 0; i<len; i++)
- {
- //InsertNode_Loop(a[i]); //非递归
- InsertNode(a[i]); //递归
- }
- }
- void BinarySortTree::InsertNode_Loop(int num)
- {
- //若根节点为空,则用第一个节点 作为根节点。
- if (rootNode == NULL)
- {
- rootNode = new Node(num);
- return;
- }
- Node * pRootNode = rootNode;
- Node *pNode = new Node(num);
- //待插入的节点 小于根节点则 插入到 左子树
- while(pRootNode != NULL)
- {
- if (num < pRootNode->data)
- {
- if (pRootNode->left == NULL)
- {
- pRootNode->left = pNode;
- return;
- }else
- {
- pRootNode = pRootNode->left;
- }
- }else
- {
- if (pRootNode->right == NULL)
- {
- pRootNode->right = pNode;
- return;
- }else
- {
- pRootNode = pRootNode->right;
- }
- }
- }
- return;
- }
- void BinarySortTree::InsertNode(int num)
- {
- if (rootNode == NULL)
- {
- rootNode = new Node(num);
- return;
- }
- InsertNode(rootNode, num);
- return;
- }
- void BinarySortTree::InsertNode(Node *curNode,int num)
- {
- // 不要使用注释部分的代码,因为在c\c++ 中形参 只是‘值’ 传递!
- /* if (curNode == NULL)
- {
- Node *pNode = new Node(num);
- curNode = pNode;
- return;
- }
- if (num < curNode->data)
- {
- InsertNode(curNode->left, num);
- }else
- {
- InsertNode(curNode->right, num);
- }
- */
- if (num < curNode->data)
- {
- if (curNode->left == NULL)
- {
- curNode->left = new Node(num);
- }else
- {
- InsertNode(curNode->left, num);
- }
- }else if (num > curNode->data)
- {
- if (curNode->right == NULL)
- {
- curNode->right = new Node(num);
- }else
- {
- InsertNode(curNode->right, num);
- }
- }
- return;
- }
- Node* BinarySortTree::Search(int num)
- {
- if (rootNode->data == num)
- {
- cout<<"查找成功..."<<num<<endl;
- return rootNode;
- }else if (num < rootNode->data)
- {
- return Search(rootNode->left, num);
- }else
- {
- return Search(rootNode->right, num);
- }
- return NULL;
- }
- Node* BinarySortTree::Search(Node *curNode ,int num)
- {
- if (curNode == NULL)
- {
- cout<<"查找失败..."<<num<<endl;
- return NULL;
- }else
- {
- if (curNode->data == num)
- {
- cout<<"查找成功..."<<num<<endl;
- return curNode;
- }else if (num < curNode->data)
- {
- Search(curNode->left, num);
- }else
- {
- Search(curNode->right, num);
- }
- }
- return NULL;
- }
- void BinarySortTree::PrintTree()
- {
- if(rootNode !=NULL)
- {
- cout<<"打印树。。。"<<endl;
- PrintTree(rootNode);
- }
- }
- void BinarySortTree::PrintTree(Node* curNode)
- {
- if (curNode == NULL)
- {
- return;
- }
- PrintTree(curNode->left);
- PrintTree(curNode->right);
- cout<<curNode->data<<endl;
- }
- int main()
- {
- int a[] = {5,2,1,4,0,9,7,10,100};
- BinarySortTree tree(a, sizeof(a)/sizeof(a[0]));
- tree.PrintTree();
- tree.Search(19);
- return 0;
- }
//#include "SortedTree.h"
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *left;
Node *right;
Node():data(-1),left(NULL),right(NULL){}
Node(int num):data(num),left(NULL),right(NULL){}
};
class BinarySortTree
{
public:
BinarySortTree(int num[], int len);
void InsertNode_Loop(int num); //非递归插入
void InsertNode(int num); //递归插入
void PrintTree(); //打印树
Node* Search(int num); //查找节点
private:
void InsertNode(Node *curNode, int num); //递归插入
void PrintTree(Node *curNode); //打印树, 后根遍历二叉排序树
Node *Search(Node *curNode, int num); //查找节点
Node *rootNode;
};
BinarySortTree::BinarySortTree(int a[],int len):rootNode(NULL)
{
for (int i = 0; i<len; i++)
{
//InsertNode_Loop(a[i]); //非递归
InsertNode(a[i]); //递归
}
}
void BinarySortTree::InsertNode_Loop(int num)
{
//若根节点为空,则用第一个节点 作为根节点。
if (rootNode == NULL)
{
rootNode = new Node(num);
return;
}
Node * pRootNode = rootNode;
Node *pNode = new Node(num);
//待插入的节点 小于根节点则 插入到 左子树
while(pRootNode != NULL)
{
if (num < pRootNode->data)
{
if (pRootNode->left == NULL)
{
pRootNode->left = pNode;
return;
}else
{
pRootNode = pRootNode->left;
}
}else
{
if (pRootNode->right == NULL)
{
pRootNode->right = pNode;
return;
}else
{
pRootNode = pRootNode->right;
}
}
}
return;
}
void BinarySortTree::InsertNode(int num)
{
if (rootNode == NULL)
{
rootNode = new Node(num);
return;
}
InsertNode(rootNode, num);
return;
}
void BinarySortTree::InsertNode(Node *curNode,int num)
{
// 不要使用注释部分的代码,因为在c\c++ 中形参 只是‘值’ 传递!
/* if (curNode == NULL)
{
Node *pNode = new Node(num);
curNode = pNode;
return;
}
if (num < curNode->data)
{
InsertNode(curNode->left, num);
}else
{
InsertNode(curNode->right, num);
}
*/
if (num < curNode->data)
{
if (curNode->left == NULL)
{
curNode->left = new Node(num);
}else
{
InsertNode(curNode->left, num);
}
}else if (num > curNode->data)
{
if (curNode->right == NULL)
{
curNode->right = new Node(num);
}else
{
InsertNode(curNode->right, num);
}
}
return;
}
Node* BinarySortTree::Search(int num)
{
if (rootNode->data == num)
{
cout<<"查找成功..."<<num<<endl;
return rootNode;
}else if (num < rootNode->data)
{
return Search(rootNode->left, num);
}else
{
return Search(rootNode->right, num);
}
return NULL;
}
Node* BinarySortTree::Search(Node *curNode ,int num)
{
if (curNode == NULL)
{
cout<<"查找失败..."<<num<<endl;
return NULL;
}else
{
if (curNode->data == num)
{
cout<<"查找成功..."<<num<<endl;
return curNode;
}else if (num < curNode->data)
{
Search(curNode->left, num);
}else
{
Search(curNode->right, num);
}
}
return NULL;
}
void BinarySortTree::PrintTree()
{
if(rootNode !=NULL)
{
cout<<"打印树。。。"<<endl;
PrintTree(rootNode);
}
}
void BinarySortTree::PrintTree(Node* curNode)
{
if (curNode == NULL)
{
return;
}
PrintTree(curNode->left);
PrintTree(curNode->right);
cout<<curNode->data<<endl;
}
int main()
{
int a[] = {5,2,1,4,0,9,7,10,100};
BinarySortTree tree(a, sizeof(a)/sizeof(a[0]));
tree.PrintTree();
tree.Search(19);
return 0;
}
(后根)遍历结果:
0 1 4 2 7 100 10 9 5