码迷,mamicode.com
首页 > 其他好文 > 详细

二叉搜索树的基本操作实现

时间:2018-07-16 23:08:10      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:运行   插入   mes   删除元素   new   png   删除   code   oid   

1. 二叉搜索树结点结构定义如下:

// 二叉搜索树节点定义
struct TreeNode {
    int data;
    TreeNode* lChild;
    TreeNode* rChild;
    TreeNode(int val) : data(val), lChild(NULL), rChild(NULL) {}
};

2. 二叉搜索树的基本操作函数如下:

  • void preOrderTraverse(TreeNode* head); // 先序遍历(递归)
  • TreeNode* searchBST(TreeNode* head, int val); // 查找操作(尾递归)
  • TreeNode* iterSearchBST(TreeNode* head, int val); // 查找操作(迭代)
  • TreeNode* finMin(TreeNode* head); // 查找最小元素(尾递归)
  • TreeNode* finMax(TreeNode* head); // 查找最大元素(迭代)
  • TreeNode* insertBST(TreeNode* head, int val); // 插入元素(递归)
  • TreeNode* deleteBST(TreeNode* head, int val); // 删除元素(递归)

3. 具体代码实现如下:

#include <iostream>
#include <stack>

using namespace std;

// 二叉搜索树节点定义
struct TreeNode {
    int data;
    TreeNode* lChild;
    TreeNode* rChild;
    TreeNode(int val) : data(val), lChild(NULL), rChild(NULL) {}
};

// 先序遍历(递归)
void preOrderTraverse(TreeNode* head) {
    if (head) {
        cout << head->data << " ";
        preOrderTraverse(head->lChild);
        preOrderTraverse(head->rChild);
    }
}

// 查找操作(尾递归)
TreeNode* searchBST(TreeNode* head, int val) {
    if (head == NULL) {
        return NULL;
    }
    if (val > head->data) {
        return searchBST(head->rChild, val);
    }
    else if (val < head->data) {
        return searchBST(head->lChild, val);
    }
    else {
        return head;
    }
}

// 查找操作(迭代)
TreeNode* iterSearchBST(TreeNode* head, int val) {
    TreeNode* node = head;
    while (node != NULL) {
        if (val > node->data) {
            node = node->rChild;
        }
        else if (val < node->data) {
            node = node->lChild;
        }
        else {
            return node;
        }
    }
    return NULL;
}

// 查找最小元素(尾递归)
TreeNode* finMin(TreeNode* head) {
    if (head == NULL) {
        return NULL;
    }
    else if (head->lChild == NULL) {
        return head;
    }
    else
        return finMin(head->lChild);
}

// 查找最大元素(迭代)
TreeNode* finMax(TreeNode* head) {
    TreeNode* node = head;
    if (node != NULL) {
        while (node->rChild) {
            node = node->rChild;
        }
    }
    return node;
}

// 插入元素(递归)
TreeNode* insertBST(TreeNode* head, int val) {
    if (head == NULL) {
        head = new TreeNode(val);
    }
    else if (val < head->data) {
        head->lChild = insertBST(head->lChild, val);
    }
    else if (val > head->data) {
        head->rChild = insertBST(head->rChild, val);
    }
    return head;
}

// 删除元素(递归)
TreeNode* deleteBST(TreeNode* head, int val) {
    if (head == NULL) {
        cout << "The BST is empty. Not find." << endl;
        return NULL;
    }
    else if (val < head->data) {
        head->lChild = deleteBST(head->lChild, val);
    }
    else if (val > head->data) {
        head->rChild = deleteBST(head->rChild, val);
    }
    else if (head->lChild && head->rChild) {
        TreeNode* temp = finMin(head->rChild);
        head->data = temp->data;
        head->rChild = deleteBST(head->rChild, head->data);
    }
    else {
        TreeNode* temp = head;
        if (head->lChild == NULL) {
            head = head->rChild;
        }
        else if (head->rChild == NULL) {
            head = head->lChild;
        }
        delete(temp);
    }
    return head;
}

int main() {

    TreeNode* head = NULL;
    head = insertBST(head, 3);
    head = insertBST(head, 2);
    head = insertBST(head, 4);
    head = insertBST(head, 1);
    head = insertBST(head, 5);
    cout << "先序遍历BST元素: " << endl;
    preOrderTraverse(head);
    cout << endl;
    TreeNode* node = NULL;
    if (node = searchBST(head, 5)) {
        cout << node->data << " is in the BST." << endl;
    }
    else {
        cout << 5 << " is not exist in the BST." << endl;
    }
    if (node = iterSearchBST(head, 100)) {
        cout << node->data << " is in the BST." << endl;
    }
    else {
        cout << 100 << " is not exist in the BST." << endl;
    }
    if (node = finMin(head)) {
        cout << "The minimun element is: " << node->data << "." << endl;
    }
    if (node = finMax(head)) {
        cout << "The maximun element is: " << node->data << "." << endl;
    }
    head = deleteBST(head, 3);
    cout << "Delete " << 3 << ": ";
    preOrderTraverse(head);
    cout << endl;
    head = deleteBST(head, 2);
    cout << "Delete " << 2 << ": ";
    preOrderTraverse(head);
    cout << endl;

    system("pause");

    return 0;
}

4. 运行结果截图如下:

技术分享图片

二叉搜索树的基本操作实现

标签:运行   插入   mes   删除元素   new   png   删除   code   oid   

原文地址:https://www.cnblogs.com/yiluyisha/p/9320692.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!