每天刷个算法题20160520:二叉排序树

Posted 小飞_Xiaofei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每天刷个算法题20160520:二叉排序树相关的知识,希望对你有一定的参考价值。

版权所有。所有权利保留。

欢迎转载,转载时请注明出处:

http://blog.csdn.net/xiaofei_it/article/details/51502682


为了防止思维僵化,每天刷个算法题。已经刷了几天了,现在发点代码。

我已经建了一个开源项目,每天的题目都在里面:

https://github.com/Xiaofei-it/Algorithms

绝大部分算法都是我自己写的,没有参考网上通用代码。读者可能会觉得有的代码晦涩难懂,因为那是我自己的理解。

最近几天都是在写一些原来的东西,大多数是非递归。以后准备刷点DP、贪心之类的题。

下面是二叉排序树:

/**
 *
 * Copyright 2016 Xiaofei
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package xiaofei.algorithm;

/**
 * Created by Xiaofei on 16/5/20.
 */
public class BinarySearchTree {
    private BinaryTreeNode root;
    public BinarySearchTree() {
        root = null;
    }

    public void insert(int data) {
        if (root == null) {
            root = new BinaryTreeNode();
            root.data = data;
        } else {
            BinaryTreeNode p = root, q = null;
            while (p != null) {
                q = p;
                if (p.data < data) {
                    p = p.right;
                } else if (p.data > data){
                    p = p.left;
                } else {
                    return;
                }
            }
            p = new BinaryTreeNode();
            p.data = data;
            if (q.data < data) {
                q.right = p;
            } else {
                q.left = p;
            }
        }
    }

    public boolean contains(int data) {
        if (root == null) {
            return false;
        }
        BinaryTreeNode p = root;
        while (p != null) {
            if (p.data < data) {
                p = p.right;
            } else if (p.data > data){
                p = p.left;
            } else {
                return true;
            }
        }
        return false;
    }

    public void delete(int data) {
        if (root == null) {
            return;
        }
        BinaryTreeNode p = root, q = null;
        while (p != null) {
            if (p.data < data) {
                q = p;
                p = p.right;
            } else if (p.data > data){
                q = p;
                p = p.left;
            } else {
                break;
            }
        }
        //element not exist
        if (p == null) {
            return;
        }
        //element is root
        if (p == root) {
            if (p.left == null && p.right == null) {
                root = null;
            } else if (p.left == null) {
                root = p.right;
            } else if (p.right == null) {
                root = p.left;
            } else {
                BinaryTreeNode p1 = p.left, q1 = null;
                while (p1.right != null) {
                    q1 = p1;
                    p1 = p1.right;
                }
                if (q1 != null) {
                    q1.right = p.left;
                }
                p1.left = p.left;
                p1.right = p.right;
                root = p1;
            }
            return;
        }
        if (p.left == null && p.right == null) {
            if (q.data < data) {
                q.right = null;
            } else {
                q.left = null;
            }
        } else if (p.left == null) {
            if (q.data < data) {
                q.right = p.right;
            } else {
                q.left = p.right;
            }
        } else if (p.right == null) {
            if (q.data < data) {
                q.right = p.left;
            } else {
                q.left = p.right;
            }
        } else {
            BinaryTreeNode p1 = p.left, q1 = null;
            while (p1.right != null) {
                q1 = p1;
                p1 = p1.right;
            }
            if (q1 != null) {
                q1.right = p1.left;
            }
            p1.left = p.left;
            p1.right = p.right;
            if (q.data < data) {
                q.right = p1;
            } else {
                q.left = p1;
            }

        }

    }

    public void output() {
        if (root != null) {
            InOrderTraversal.traversalCorecursively(root);
        }
    }
}


以上是关于每天刷个算法题20160520:二叉排序树的主要内容,如果未能解决你的问题,请参考以下文章

每天刷个算法题20160520:二叉排序树

每天刷个算法题20160521:二叉树高度(递归与非递归)

每天刷个算法题20160518:非递归二叉树遍历

每天刷个算法题20160518:非递归二叉树遍历

每天刷个算法题20160521:二叉树高度(递归与非递归)

每天刷个算法题20160521:二叉树高度(递归与非递归)