Tree Traverse & BST

In this lesson we have learnt about the tree traverse and binary search tree, two new method in searching for precise data points.

Tree Traverse:
Preorder traverse
Inorder traverse
Postorder traverse
这里写图片描述

Binary Search Tree Class

The following class includes adding, removing, searching, getting and setting method for a BSTNode object.

import java.util.*;
public class BSTNode {
       private int value;
       private BSTNode left;
       private BSTNode right;

        public BSTNode(int value)
        {
            this.value = value;
            left = null;
            right = null;
        }

        public boolean add(int value) {
            if (value == this.value)
                  return false;
            else if (value <this.value) {
                  if (left == null) {
                        left = new BSTNode(value);
                        return true;
                  } else
                        return left.add(value);
            } else if (value > this.value) {
                  if (right == null) {
                        right = new BSTNode(value);
                        return true;
                  } else
                        return right.add(value);
            }
            return false;
        }

        public boolean search(int value) {
            if (value == this.value)
                  return true;
            else if (value < this.value) {
                  if (left == null)
                        return false;
                  else
                        return left.search(value);
            } else if (value > this.value) {
                  if (right == null)
                        return false;
                  else
                        return right.search(value);
            }
            return false;
        }

        public boolean remove(int value, BSTNode parent) {
            if (value < this.value) {
                  if (left != null)
                        return left.remove(value, this);
                  else
                        return false;
            } else if (value > this.value) {
                  if (right != null)
                        return right.remove(value, this);
                  else
                        return false;
            } else {
                  if (left != null && right != null) {
                        this.value = right.minValue();
                        right.remove(this.value, this);
                  } else if (parent.left == this) {
                        parent.left = (left != null) ? left : right;
                  } else if (parent.right == this) {
                        parent.right = (left != null) ? left : right;
                  }
                  return true;
            }
      }

      public int minValue() {
            if (left == null)
                  return value;
            else
                  return left.minValue();
      }

      public int getValue(){
          return this.value;
      }

      public BSTNode setLeftChild(BSTNode root){
          left = root;
          return left;
      }
      public BSTNode getLeft(){
          return left;
      }
}

Binary Search Tree Class

In this class, the methods are specific applied for different use in Binary Search Tree.

import java.util.*;
public class BinarySearchTree {
    private BSTNode root;

    public BinarySearchTree()
    {
        root = null;
    }

    public boolean add(int value){
        if(root == null){
            root = new BSTNode(value);
            return true;
        }
        else{
            return root.add(value);
        }
    }

    public boolean search(int value) {
        if (root == null)
              return false;
        else
              return root.search(value);
    }

    public boolean remove(int value) {
        if (root == null)
              return false;
        else {
              if (root.getValue() == value) {
                    BSTNode auxRoot = new BSTNode(0);
                    auxRoot.setLeftChild(root);
                    boolean result = root.remove(value, auxRoot);
                    root = auxRoot.getLeft();
                    return result;
              } else {
                    return root.remove(value, null);
              }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值