bisorttree.cpp
来自「数据结构学习中遇到的二叉树的代码」· C++ 代码 · 共 68 行
CPP
68 行
#include<iostream>
#include"bisorttree.h"
using namespace std;
BiSortTree::BiSortTree(int a[ ], int n)
{
root = NULL;
for (int i = 0; i < n; i++)
{
BiNode* s = new BiNode;
s->data = a[i];
s->lchild = NULL;
s->rchild = NULL;
root = InsertBST(root, s);
}
}
BiSortTree::~BiSortTree(void)
{
Release(root);
}
BiNode* BiSortTree::Getroot( )
{
return root;
}
BiNode* BiSortTree::InsertBST(BiNode *root, BiNode *s)
{
if(root==NULL) return s;
else{
if(s->data<root->data) root->lchild = InsertBST(root->lchild, s);
else root->rchild = InsertBST(root->rchild, s);
return root;
}
}
BiNode* BiSortTree::SearchBST(BiNode *root, int k)
{
if(root==NULL)
{
cout<<"此结点不存在!"<<endl;
return NULL;
}
else
if (root->data==k)
{
cout<<"查找"<<root->data<<"成功!"<<endl;
return root;
}
else
if (k < root->data) return SearchBST(root->lchild, k);
else return SearchBST(root->rchild, k);
}
void BiSortTree::Release(BiNode* root)
{
if (root != NULL)
{
Release(root->lchild);
Release(root->rchild);
delete root;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?