二叉查找树,也称二叉排序树,二叉搜索树。
它或者是一棵空树;或者是具有下列性质的二叉树:
步骤:
步骤:
假设被删结点是*p,其双亲是*f,不失一般性,设*p是*f的左孩子,下面分三种情况讨论:
C++实现代码:
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode *parent;
TreeNode(int x) : val(x), left(NULL), right(NULL), parent(NULL) {}
};
TreeNode* minNode(TreeNode *root)
{
// if (root != NULL && root->left != NULL)
// {
// return minNode(root->left);
// }
while (root != NULL && root->left != NULL)
{
root = root->left;
}
return root;
}
TreeNode* maxNode(TreeNode *root)
{
if (root != NULL && root->right != NULL)
{
return maxNode(root->right);
}
return root;
}
TreeNode* search(TreeNode *root, int value)
{
if (root == NULL || root->val == value)
{
return root;
}
else if (root->val > value)
{
return search(root->left, value);
}
else if (root->val < value)
{
return search(root->right, value);
}
}
void insertNode(TreeNode*& root, int value)
{
if (search(root, value) != NULL)
{
return;
}
TreeNode *newnode = new TreeNode(value);
TreeNode *temp = root;
TreeNode *parentNode = NULL;
if (root == NULL)
{
root = newnode; //新插入结点成为根结点
return;
}
while (temp != NULL)
{
parentNode = temp;
if (temp->val > value)
{
temp = temp->left;
}
else
{
temp = temp->right;
}
}
if (value < parentNode->val)
{
parentNode->left = newnode;
}
else
{
parentNode->right = newnode;
}
newnode->parent = parentNode;
}
bool deleteNode(TreeNode*& root, int value)
{
TreeNode *cur = search(root, value);
TreeNode *temp;
if (cur == NULL)
{
return false; //未找到结点
}
if (cur->left != NULL && cur->right != NULL)
{
TreeNode *max_node = maxNode(cur->left);
cur->val = max_node->val;
deleteNode(max_node, max_node->val);
}
else
{
if (cur->left == NULL && cur->right == NULL) //删除叶子结点
{
temp = NULL;
}
else if (cur->left == NULL) //要删除结点的左结点为空
{
temp = cur->right;
}
else if (cur->right == NULL) //要删除结点的右结点为空
{
temp = cur->left;
}
if (cur->parent == NULL)
{
root = temp;
if (temp != NULL) temp->parent = NULL;
}
else
{
if (cur->parent->left == cur)
{
cur->parent->left = temp;
}
else
{
cur->parent->right = temp;
}
if (temp != NULL)
{
temp->parent = cur->parent;
}
}
delete cur;
}
return true;
}
void preTraverse(TreeNode *root)
{
if (root != NULL)
{
cout<<root->val<<" ";
preTraverse(root->left);
preTraverse(root->right);
}
}
void inTraverse(TreeNode *root)
{
if (root != NULL)
{
inTraverse(root->left);
cout<<root->val<<" ";
inTraverse(root->right);
}
}
void postTraverse(TreeNode *root)
{
if (root != NULL)
{
postTraverse(root->left);
postTraverse(root->right);
cout<<root->val<<" ";
}
}
int main()
{
TreeNode *root = NULL;
for (int i = 0; i < 20; i++)
{
insertNode(root, rand() % 100);
}
cout<<"先序遍历:";preTraverse(root);cout<<" END"<<endl;
cout<<"中序遍历:";inTraverse(root);cout<<" END"<<endl;
cout<<"后序遍历:";postTraverse(root);cout<<" END"<<endl;
for (int i = 0; i < 20; i++)
{
deleteNode(root, i);
}
cout<<"先序遍历:";preTraverse(root);cout<<" END"<<endl;
TreeNode *temp = minNode(root);
temp != NULL ? cout<<temp->val<<endl : cout<<"null"<<endl;
temp = maxNode(root);
temp != NULL ? cout<<temp->val<<endl : cout<<"null"<<endl;
return 0;
}
Java实现代码:
package test;
class TreeNode{
int val;
TreeNode left;
TreeNode right;
TreeNode parent;
TreeNode(int x){
this.val = x;
}
}
public class BinarySearchTree {
TreeNode root;
TreeNode maxElem(TreeNode node){
while (node != null && node.right != null){
node = node.right;
}
return node;
}
TreeNode minElem(TreeNode node){
while (node != null && node.left != null){
node = node.left;
}
return node;
}
TreeNode search(int key){
TreeNode temp = root;
while (temp != null && temp.val != key){
if (temp.val > key){
temp = temp.left;
}else{
temp = temp.right;
}
}
return temp;
}
boolean insert(int key){
if (search(key) != null){
return false;
}
TreeNode newnode = new TreeNode(key);
TreeNode temp = root;
if (temp == null){
this.root = newnode;
return true;
}
TreeNode parentNode = null;
while (temp != null){
parentNode = temp;
if (temp.val > key){
temp = temp.left;
}else{
temp = temp.right;
}
}
if (parentNode.val > key){
parentNode.left = newnode;
}else{
parentNode.right = newnode;
}
newnode.parent = parentNode;
return true;
}
boolean delete(int key){
TreeNode cur = search(key);
if (cur == null){
return false;
}
TreeNode temp = null;
if (cur.left != null && cur.right != null){
TreeNode leftMax = maxElem(cur.left);
cur.val = leftMax.val;
delete(leftMax.val);
}else if (cur.left == null && cur.right == null){
temp = null;
}else if (cur.left != null && cur.right == null){
temp = cur.left;
}else if (cur.left == null && cur.right != null){
temp = cur.right;
}
if (cur.parent == null){
this.root = temp;
if (temp != null){
temp.parent = null;
}
}
else{
if (cur.parent.left == cur){
cur.parent.left = temp;
}else{
cur.parent.right = temp;
}
if (temp != null){
temp.parent = cur.parent;
}
}
return true;
}
void preTraverse(TreeNode node){
if (node != null){
System.out.print(node.val + " ");
preTraverse(node.left);
preTraverse(node.right);
}
}
void inTraverse(TreeNode node){
if (node != null){
inTraverse(node.left);
System.out.print(node.val + " ");
inTraverse(node.right);
}
}
void postTraverse(TreeNode node){
if (node != null){
postTraverse(node.left);
postTraverse(node.right);
System.out.print(node.val + " ");
}
}
public static void main(String[] args){
BinarySearchTree bst = new BinarySearchTree();
for (int i = 0; i < 20; i++){
bst.insert((int)(Math.random() * 100));
//bst.insert(i);
}
System.out.print("前序遍历:"); bst.preTraverse(bst.root); System.out.println();
System.out.print("中序遍历:"); bst.inTraverse(bst.root); System.out.println();
System.out.print("后序遍历:"); bst.postTraverse(bst.root); System.out.println();
for (int i = 0; i < 20; i++){
bst.delete(i);
}
System.out.print("前序遍历:"); bst.preTraverse(bst.root); System.out.println();
System.out.println("Max:" + bst.maxElem(bst.root).val);
System.out.println("Min:" + bst.minElem(bst.root).val);
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/foreverling/article/details/46661937