二叉查找树

Posted 刁海威

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉查找树相关的知识,希望对你有一定的参考价值。

#ifndef BINARYSEARCHTR_H
#define BINARYSEARCHTR_H

#include "stdafx.h"
#include <iostream>
/*节点类定义*/
class Node {
public:
    int data;
    Node *parent;
    Node *left;
    Node *right;
public:
    Node() : data(-1), parent(NULL), left(NULL), right(NULL) {};
    Node(int num) : data(num), parent(NULL), left(NULL), right(NULL) {};
};
/*Binary Search Tree definition*/
class Tree {
public:
    Tree(int num[], int len);
    void insertNode1(int data);
    void insertNode(int data);
    Node *searchNode(int data);
    void deleteNode(int data);
private:
    void insertNode(Node *current, int data);
    Node *searchNode(Node *current, int data);
    void deleteNode(Node *current);
private:
    Node* root;
};
Tree::Tree(int num[], int len) {
    root = new Node(num[0]);
    for (int i = 1; i < len; i++) {
        insertNode1(num[i]);
    }
}
void Tree::insertNode1(int data) {
    Node *p, *par;
    Node *newNode = new Node(data);
    p = par = root;
    while (p != NULL) {
        par = p;
        if (data > p->data)
            p = p->right;
        else if (data < p->data)
            p = p->left;
        else if (data == p->data) {
            delete newNode;
            return;
        }
    }
    newNode->parent = par;
    if (par->data > newNode->data)
        par->left = newNode;
    else
        par->right = newNode;
}
void Tree::insertNode(int data) {
    if (root != NULL) {
        insertNode(root, data);
    }
}
void Tree::insertNode(Node* current, int data) {
    if (data < current->data) {
        if (current->left == NULL) {
            current->left = new Node(data);
            current->left->parent = current;
        }
        else
            insertNode(current->left, data);
    }
    else if (data > current->data){
        if (current->right == NULL) {
            current->right = new Node(data);
            current->right->parent = current;
        }
        else
            insertNode(current->right, data);
    }
}
Node* Tree::searchNode(int data) {
    if (root != NULL) {
        return searchNode(root, data);
    }
}
Node* Tree::searchNode(Node* current, int data) {
    if (data < current->data) {
        if (current->left == NULL)
            return NULL;
        return searchNode(current->left, data);
    }
    else if (data > current->data) {
        if (current->right == NULL)
            return NULL;
        return searchNode(current->right, data);
    }
    return current;
}
void Tree::deleteNode(int data) {
    Node *current = NULL;
    current = searchNode(data);
    if (current != NULL) {
        deleteNode(current);
    }
}
void Tree::deleteNode(Node *current) {
    if (current->left != NULL)
        deleteNode(current->left);
    if (current->right != NULL)
        deleteNode(current->right);
    if (current->parent == NULL) {
        delete current;
        root = NULL;
        return;
    }
    if (current->parent->data > current->data)
        current->parent->left = NULL;
    else
        current->parent->right = NULL;
    delete current;

}
#endif

 

以上是关于二叉查找树的主要内容,如果未能解决你的问题,请参考以下文章

数据结构 动态查找与二叉排序树

二叉查找树

PHP 二叉查找树(二叉搜索树)的查找

C++-二叉搜索树的查找&插入&删除-二叉搜索树代码实现-二叉搜索树性能分析及解决方案

二叉查找树 - 删除查找

数据结构中,怎么写二叉树查找双亲的伪代码?急!