Ai
1 Star 0 Fork 0

莫念.莫言/20162329zxs_2ad

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LinkedBinarySearchTree.java 2.60 KB
一键复制 编辑 原始数据 按行查看 历史
1050725105@qq.com 提交于 2017-10-25 11:12 +08:00 . 实现二叉查找树
package chapter17;
//*******************************************************************
// LinkedBinarySearchTree.java Java Foundations
//
// Implements the binary tree using a linked representation.
//*******************************************************************
import chapter16.BTNode;
import chapter16.LinkedBinaryTree;
public class LinkedBinarySearchTree<T extends Comparable<T>> extends LinkedBinaryTree<T> implements BinarySearchTree<T>
{
//-----------------------------------------------------------------
// Creates an empty binary search tree.
//-----------------------------------------------------------------
public LinkedBinarySearchTree()
{
super();
}
//-----------------------------------------------------------------
// Creates a binary search tree with the specified element at its
// root.
//-----------------------------------------------------------------
public LinkedBinarySearchTree (T element)
{
root = new BSTNode<T>(element);
}
//-----------------------------------------------------------------
// Adds the specified element to this binary search tree.
//-----------------------------------------------------------------
public void add (T item)
{
if (root == null)
root = new BSTNode<T>(item);
else
((BSTNode)root).add(item);
}
//-----------------------------------------------------------------
// Removes and returns the element matching the specified target
// from this binary search tree. Throws an ElementNotFoundException
// if the target is not found.
//-----------------------------------------------------------------
public T remove (T target) throws Exception {
BSTNode<T> node = null;
if (root != null)
node = ((BSTNode)root).find(target);
if (node == null)
throw new Exception ("Remove operation failed. "
+ "No such element in tree.");
root = ((BSTNode)root).remove(target);
return node.getElement();
}
//-----------------------------------------------------------------
// The following methods are left as programming projects.
//-----------------------------------------------------------------
public T findMin() {
BTNode<T> r = root;
while (r.getLeft() != null)
r = r.getLeft();
return r.getElement();
}
public T findMax() {
BTNode<T> r = root;
while (r.getRight() != null)
r = r.getRight();
return r.getElement();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/XuiWe/20162329zxs_2ad.git
git@gitee.com:XuiWe/20162329zxs_2ad.git
XuiWe
20162329zxs_2ad
20162329zxs_2ad
master

搜索帮助