二叉查找树

#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

 

posted @ 2018-04-07 12:02  刁海威  阅读(161)  评论(0编辑  收藏  举报