#include <windows.h> // 用来计时 GetTickCount函数
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include <stdlib.h>
using namespace std;
//typedef int int; // 数据类型定义
// 节点定义
struct node
{
int data;
node* left;
node* right;
node* parent;
}; // 指针类型 *pNode
typedef struct node* pNode; // 指针类型节点
typedef struct node Node; // 普通节点
pNode root = NULL;
void inorder(pNode x);// 中序遍历
pNode search1(pNode x, int k); // 查找给定关键词所在节点
pNode minimum(pNode x); // 返回最小关键字节点
pNode maximum(pNode x);// 返回最大关键字节点
pNode successor(pNode x);// 返回给定节点x的后继
pNode precursor(pNode x);// 返回给定节点x的前驱
void insert(pNode T, int data);// 插入一个元素x
void transplant(pNode T, pNode u, pNode v);
void deleteNode(pNode T, pNode z); // 删除一个节点z
void destroy1(pNode T); // 摧毁整个二叉搜索树
// 中序遍历
void inorder(pNode x)
{
if (x != NULL)
{
inorder(x->left);
cout << x->data << " ";
inorder(x->right);
}
else
cout << " ";
}
// 查找给定关键词所在节点
pNode search1(pNode x, int k)
{
if (x == NULL || k == x->data)
return x;
if (k < x->data)
return search1(x->left, k);
else
return search1(x->right, k);
}
// 第二版本不用递归,用循环
pNode search2(pNode x, int k)
{
while (x != NULL && k != x->data)
{
if (k < x->data)
x = x->left;
else
x = x->right;
}
return x;
}
// 返回最小关键字节点
pNode minimum(pNode x)
{
while (x->left != NULL)
{
x = x->left;
}
return x;
}
// 返回最大关键字节点
pNode maximum(pNode x)
{
while (x->right != NULL)
{
x = x->right;
}
return x;
}
// 返回给定节点x的后继
pNode successor(pNode x)
{
if (x->right != NULL)
return minimum(x->right);
pNode y = x->parent;
while (y != NULL && x == y->right)
{
x = y;
y = y->parent;
}
return y;
}
// 返回给定节点x的前驱
pNode precursor(pNode x)
{
if (x->left != NULL)
return maximum(x->left);
pNode y = x->parent;
while (y != NULL && x == y->left)
{
x = y;
y = y->parent;
}
return y;
}
// 插入一个元素x
void insert1(pNode Root, int data)
{
pNode y = NULL;
pNode x = Root; // 临时传递节点的地址
Node* z = new Node;
// 初始化z // 注意全局变量和参数的名称不要一样。全局变量遇到同名的局部变
z->data = data; // 量,全局变量在这个函数中暂时消失!只要出了这个函数就会再现
z->left = NULL; // 一样容易搞混了。
z->right = NULL;
z->parent = NULL;
while (x != NULL)
{
y = x;
if (z->data < x->data)
x = x->left;
else
x = x->right;
}
z->parent = y;
if (y == NULL)
root = z; // 空树直接赋值z
else if (z->data < y->data)
y->left = z;
else
y->right = z;
}
//非递归方法插入节点
void insert2(pNode q, int x)
{
pNode p = (pNode)malloc(4);
p->data = x;
p->left = NULL;
p->right = NULL;
if (q == NULL){
root = p;
return;
}
while (q->left != p && q->right != p)
{
if (x < q->data)
{
if (q->left)
{
q = q->left;
}
else
{
q->left = p;
}
}
else
{
if (q->right)
{
q = q->right;
}
else
{
q->right = p;
}
}
}
return;
}
//递归方法插入节点
pNode insert3(pNode Root, int x)
{
pNode p = (pNode)malloc(4);
p->data = x;
p->left = NULL;
p->right = NULL;
if (Root == NULL)
{
Root = p;
}
else if (x < Root->data)
{
Root->left = insert3(Root->left, x);
}
else
{
Root->right = insert3(Root->right, x);
}
return Root;
}
// 删除一个节点z
void transplant(pNode T, pNode u, pNode v)
{
if (u->parent == NULL)
T = v;
else if (u == u->parent->left)
u->parent->left = v;
else
u->parent->right = v;
if (v != NULL)
v->parent = u->parent;
}
void deleteNode(pNode T, pNode z)
{
pNode y = NULL;
if (z->left == NULL)
transplant(T, z, z->right);
else if (z->right == NULL)
transplant(T, z, z->left);
else
y = minimum(z->right);
if (y->parent != z)
{
transplant(T, y, y->right);
y->right = z->right;
y->right->parent = y;
}
transplant(T, z, y);
y->left = z->left;
y->left->parent = y;
delete z;
}
// 摧毁整个二叉搜索树
void destroy1(pNode T)
{
if (T != NULL)
{
destroy1(T->left);
destroy1(T->right);
delete (T);
}
T = NULL;
}
void destroy2(pNode T)
{
if (T == NULL)
return;
destroy2(T->left);
destroy2(T->right);
free(T);
T = NULL;
}
int main()
{
srand((unsigned)time(0));
const int LENGTH = 10;
// 初始化操作,为树赋值
for (int i = 0; i < LENGTH; i++)
{
int key = rand() / 10;
cout << key << " ";
insert2(root, key);
}
cout << endl;
// 中序遍历树
inorder(root); //排序
cout << endl;
insert2(root, 34);// 插入34
inorder(root);//排序
cout << endl;
cout << (*maximum(root)).data << endl; //返回最大
cout << (*minimum(root)).data << endl; //返回最小
destroy2(root);//摧毁
/**************************/
for (int i = 0; i < 10; i++)
{
cout << endl;
}
system("pause");
return 0;
}
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include <stdlib.h>
using namespace std;
//typedef int int; // 数据类型定义
// 节点定义
struct node
{
int data;
node* left;
node* right;
node* parent;
}; // 指针类型 *pNode
typedef struct node* pNode; // 指针类型节点
typedef struct node Node; // 普通节点
pNode root = NULL;
void inorder(pNode x);// 中序遍历
pNode search1(pNode x, int k); // 查找给定关键词所在节点
pNode minimum(pNode x); // 返回最小关键字节点
pNode maximum(pNode x);// 返回最大关键字节点
pNode successor(pNode x);// 返回给定节点x的后继
pNode precursor(pNode x);// 返回给定节点x的前驱
void insert(pNode T, int data);// 插入一个元素x
void transplant(pNode T, pNode u, pNode v);
void deleteNode(pNode T, pNode z); // 删除一个节点z
void destroy1(pNode T); // 摧毁整个二叉搜索树
// 中序遍历
void inorder(pNode x)
{
if (x != NULL)
{
inorder(x->left);
cout << x->data << " ";
inorder(x->right);
}
else
cout << " ";
}
// 查找给定关键词所在节点
pNode search1(pNode x, int k)
{
if (x == NULL || k == x->data)
return x;
if (k < x->data)
return search1(x->left, k);
else
return search1(x->right, k);
}
// 第二版本不用递归,用循环
pNode search2(pNode x, int k)
{
while (x != NULL && k != x->data)
{
if (k < x->data)
x = x->left;
else
x = x->right;
}
return x;
}
// 返回最小关键字节点
pNode minimum(pNode x)
{
while (x->left != NULL)
{
x = x->left;
}
return x;
}
// 返回最大关键字节点
pNode maximum(pNode x)
{
while (x->right != NULL)
{
x = x->right;
}
return x;
}
// 返回给定节点x的后继
pNode successor(pNode x)
{
if (x->right != NULL)
return minimum(x->right);
pNode y = x->parent;
while (y != NULL && x == y->right)
{
x = y;
y = y->parent;
}
return y;
}
// 返回给定节点x的前驱
pNode precursor(pNode x)
{
if (x->left != NULL)
return maximum(x->left);
pNode y = x->parent;
while (y != NULL && x == y->left)
{
x = y;
y = y->parent;
}
return y;
}
// 插入一个元素x
void insert1(pNode Root, int data)
{
pNode y = NULL;
pNode x = Root; // 临时传递节点的地址
Node* z = new Node;
// 初始化z // 注意全局变量和参数的名称不要一样。全局变量遇到同名的局部变
z->data = data; // 量,全局变量在这个函数中暂时消失!只要出了这个函数就会再现
z->left = NULL; // 一样容易搞混了。
z->right = NULL;
z->parent = NULL;
while (x != NULL)
{
y = x;
if (z->data < x->data)
x = x->left;
else
x = x->right;
}
z->parent = y;
if (y == NULL)
root = z; // 空树直接赋值z
else if (z->data < y->data)
y->left = z;
else
y->right = z;
}
//非递归方法插入节点
void insert2(pNode q, int x)
{
pNode p = (pNode)malloc(4);
p->data = x;
p->left = NULL;
p->right = NULL;
if (q == NULL){
root = p;
return;
}
while (q->left != p && q->right != p)
{
if (x < q->data)
{
if (q->left)
{
q = q->left;
}
else
{
q->left = p;
}
}
else
{
if (q->right)
{
q = q->right;
}
else
{
q->right = p;
}
}
}
return;
}
//递归方法插入节点
pNode insert3(pNode Root, int x)
{
pNode p = (pNode)malloc(4);
p->data = x;
p->left = NULL;
p->right = NULL;
if (Root == NULL)
{
Root = p;
}
else if (x < Root->data)
{
Root->left = insert3(Root->left, x);
}
else
{
Root->right = insert3(Root->right, x);
}
return Root;
}
// 删除一个节点z
void transplant(pNode T, pNode u, pNode v)
{
if (u->parent == NULL)
T = v;
else if (u == u->parent->left)
u->parent->left = v;
else
u->parent->right = v;
if (v != NULL)
v->parent = u->parent;
}
void deleteNode(pNode T, pNode z)
{
pNode y = NULL;
if (z->left == NULL)
transplant(T, z, z->right);
else if (z->right == NULL)
transplant(T, z, z->left);
else
y = minimum(z->right);
if (y->parent != z)
{
transplant(T, y, y->right);
y->right = z->right;
y->right->parent = y;
}
transplant(T, z, y);
y->left = z->left;
y->left->parent = y;
delete z;
}
// 摧毁整个二叉搜索树
void destroy1(pNode T)
{
if (T != NULL)
{
destroy1(T->left);
destroy1(T->right);
delete (T);
}
T = NULL;
}
void destroy2(pNode T)
{
if (T == NULL)
return;
destroy2(T->left);
destroy2(T->right);
free(T);
T = NULL;
}
int main()
{
srand((unsigned)time(0));
const int LENGTH = 10;
// 初始化操作,为树赋值
for (int i = 0; i < LENGTH; i++)
{
int key = rand() / 10;
cout << key << " ";
insert2(root, key);
}
cout << endl;
// 中序遍历树
inorder(root); //排序
cout << endl;
insert2(root, 34);// 插入34
inorder(root);//排序
cout << endl;
cout << (*maximum(root)).data << endl; //返回最大
cout << (*minimum(root)).data << endl; //返回最小
destroy2(root);//摧毁
/**************************/
for (int i = 0; i < 10; i++)
{
cout << endl;
}
system("pause");
return 0;
}