Java 差分键的二叉搜索树和
我试图将一个名为Java 差分键的二叉搜索树和,java,recursion,data-structures,binary-tree,binary-search-tree,Java,Recursion,Data Structures,Binary Tree,Binary Search Tree,我试图将一个名为sigma()的方法实现到一个二进制搜索树类中。此方法的任务是返回BST中差分键的总和。给出的定义I如下: 定义1。二叉树中节点的差分键,其 元素是整数如果节点是 root or是节点中的元素与其 父母亲空节点的差分为0。请参见图1以了解一个示例 差分二叉树的图示。总数 T的微分键为9,∑i=1n∆(i) ,在哪里∆(i) 表示 节点i和n的差分键,树的大小 该方法应返回树西格玛(T)值的总和。在这种情况下,sigma(T)将返回10-4-2+3+5-3=9。我理解所有这些背后
sigma()
的方法实现到一个二进制搜索树类中。此方法的任务是返回BST中差分键的总和。给出的定义I如下:
定义1。二叉树中节点的差分键,其
元素是整数如果节点是
root or是节点中的元素与其
父母亲空节点的差分为0。请参见图1以了解一个示例
差分二叉树的图示。总数
T的微分键为9,∑i=1n∆(i) ,在哪里∆(i) 表示
节点i和n的差分键,树的大小
该方法应返回树西格玛(T)值的总和。在这种情况下,sigma(T)将返回10-4-2+3+5-3=9。我理解所有这些背后的概念,可以轻松地在纸上完成,但将其实现到我的代码中是我遇到的问题。我需要编写一个包装器方法和一个递归的辅助方法来定义sigma()
这是我迄今为止编写的bTree类(sigma()
位于底部):
package-bstreedemo;
导入java.util.function.function;
/**
*二叉搜索树
*功能需要JDK 1.8
*@param树数据类型
*@自2016年9月12日起
*@见BSTreeAPI
*/
公共类BSTree实现BSTreeAPI
{
/**
*这棵树的根
*/
私有节点根;
/**
*此树中的节点数
*/
私有整数大小;
/**
*树的节点存储数据项和引用
*到左侧和右侧的子节点。
*/
私有类节点
{
/**
*此节点中的数据
*/
公共电子数据;
/**
*对根在此节点上的左子树的引用。
*/
公共节点左;
/**
*对根在此节点上的右子树的引用
*/
公共节点权;
}
/**
*构造一棵空树
*/
公共BSTree()
{
root=null;
尺寸=0;
}
@凌驾
公共布尔值为空()
{
返回大小==0;
}
@凌驾
公共空白插入(E项)
{
Node newNode=新节点();
newNode.data=项目;
如果(大小==0)
{
根=新节点;
大小++;
}
其他的
{
节点tmp=根;
while(true)
{
int d=tmp.data.compareTo(项目);
如果(d==0)
{/*密钥已存在。(更新)*/
tmp.data=项目;
返回;
}
否则,如果(d>0)
{
if(tmp.left==null)
{/*如果密钥小于tmp*/
tmp.left=newNode;
大小++;
返回;
}
其他的
{/*继续搜索插入点*/
tmp=tmp.left;
}
}
其他的
{
if(tmp.right==null)
{/*如果密钥大于tmp*/
tmp.right=newNode;
大小++;
返回;
}
其他的
{/*继续搜索插入点*/
tmp=tmp.right;
}
}
}
}
}
@凌驾
公共布尔整数树(E项)
{
返回搜索(项目)!=null;
}
@凌驾
删除公共作废(E项)
{
节点nodeptr=搜索(项);
if(nodeptr!=null)
{
移除(nodeptr);
大小--;
}
}
@凌驾
OrderTraverse中的公共void(函数func)
{
inorderTraverse(根,函数);
}
@凌驾
公共E检索(E密钥)引发BSTreeException
{
如果(大小==0)
抛出新的BSTreeException(“在检索()时应为非空树”);
节点nodeptr=搜索(键);
if(nodeptr==null)
抛出新的BSTreeException(“retrieve()上需要存在的键”);
返回nodeptr.data;
}
@凌驾
公共整数大小()
{
返回大小;
}
/**
*inorderTraver方法的递归辅助方法
*@param node对节点对象的引用
*@param func应用于每个
*按顺序遍历树时的节点。
*/
OrderTraverse中的专用void(节点,函数func)
{
如果(节点!=null)
{
inorderTraverse(node.left,func);
函数应用(节点数据);
inorderTraverse(node.right,func);
}
}
/**
*支持删除方法的辅助方法
*@param node此树中节点对象的引用
*/
私有无效删除(节点)
{
E数据;
节点父节点,替换;
父节点=findParent(节点);
if(node.left!=null)
{
if(node.right!=null)
{
替换=node.right;
while(replacement.left!=null)
替换=替换。左;
数据=替换。数据;
拆除(更换);
node.data=数据;
返回;
}
其他的
替换=node.left;
}
其他的
{
if(node.right!=null)
替换=node.right;
其他的
替换=空;
}
如果(父项==null)
根=替换;
else if(parent.left==节点)
parent.left=替换;
其他的
parent.right=替换;
}
/**
*支持搜索方法的辅助方法
*@param key一个数据键
*@返回对其数据具有指定键的节点对象的引用。
*/
专用节点搜索(E密钥)
{
节点电流=根;
package bstreedemo;
import java.util.function.Function;
/**
* A binary search tree <br>
* Requires JDK 1.8 for Function
* @param <E> the tree data type
* @since 12/09/2016
* @see BSTreeAPI
*/
public class BSTree<E extends Comparable<E>> implements BSTreeAPI<E>
{
/**
* the root of this tree
*/
private Node root;
/**
* the number of nodes in this tree
*/
private int size;
/**
* A node of a tree stores a data item and references
* to the child nodes to the left and to the right.
*/
private class Node
{
/**
* the data in this node
*/
public E data;
/**
* A reference to the left subtree rooted at this node.
*/
public Node left;
/**
* A reference to the right subtree rooted at this node
*/
public Node right;
}
/**
* Constructs an empty tree
*/
public BSTree()
{
root = null;
size = 0;
}
@Override
public boolean isEmpty()
{
return size == 0;
}
@Override
public void insert(E item)
{
Node newNode = new Node();
newNode.data = item;
if (size == 0)
{
root = newNode;
size++;
}
else
{
Node tmp = root;
while (true)
{
int d = tmp.data.compareTo(item);
if (d == 0)
{ /* Key already exists. (update) */
tmp.data = item;
return;
}
else if (d>0)
{
if (tmp.left == null)
{ /* If the key is less than tmp */
tmp.left = newNode;
size++;
return;
}
else
{ /* continue searching for insertion pt. */
tmp = tmp.left;
}
}
else
{
if (tmp.right == null)
{/* If the key is greater than tmp */
tmp.right = newNode;
size++;
return;
}
else
{ /* continue searching for insertion point*/
tmp = tmp.right;
}
}
}
}
}
@Override
public boolean inTree(E item)
{
return search(item) != null;
}
@Override
public void remove(E item)
{
Node nodeptr = search(item);
if (nodeptr != null)
{
remove(nodeptr);
size--;
}
}
@Override
public void inorderTraverse(Function func)
{
inorderTraverse(root,func);
}
@Override
public E retrieve(E key) throws BSTreeException
{
if (size == 0)
throw new BSTreeException("Non-empty tree expected on retrieve().");
Node nodeptr = search(key);
if (nodeptr == null)
throw new BSTreeException("Existent key expected on retrieve().");
return nodeptr.data;
}
@Override
public int size()
{
return size;
}
/**
* A recursive auxiliary method for the inorderTraver method that
* @param node a reference to a Node object
* @param func a function that is applied to the data in each
* node as the tree is traversed in order.
*/
private void inorderTraverse(Node node, Function func)
{
if (node != null)
{
inorderTraverse(node.left,func);
func.apply(node.data);
inorderTraverse(node.right,func);
}
}
/**
* An auxiliary method that support the remove method
* @param node a reference to a Node object in this tree
*/
private void remove(Node node)
{
E theData;
Node parent, replacement;
parent = findParent(node);
if (node.left != null)
{
if (node.right != null)
{
replacement = node.right;
while (replacement.left != null)
replacement = replacement.left;
theData = replacement.data;
remove(replacement);
node.data = theData;
return;
}
else
replacement = node.left;
}
else
{
if (node.right != null)
replacement = node.right;
else
replacement = null;
}
if (parent==null)
root = replacement;
else if (parent.left == node)
parent.left = replacement;
else
parent.right = replacement;
}
/**
* An auxiliary method that supports the search method
* @param key a data key
* @return a reference to the Node object whose data has the specified key.
*/
private Node search(E key)
{
Node current = root;
while (current != null)
{
int d = current.data.compareTo(key);
if (d == 0)
return current;
else if (d > 0)
current = current.left;
else
current = current.right;
}
return null;
}
/**
* An auxiliary method that gives a Node reference to the parent node of
* the specified node
* @param node a reference to a Node object
* @return a reference to the parent node of the specified node
*/
private Node findParent(Node node)
{
Node tmp = root;
if (tmp == node)
return null;
while(true)
{
assert tmp.data.compareTo(node.data) != 0;
if (tmp.data.compareTo(node.data)>0)
{
/* this assert is not needed but just
in case there is a bug */
assert tmp.left != null;
if (tmp.left == node)
return tmp;
tmp = tmp.left;
}
else
{
assert tmp.right != null;
if (tmp.right == node)
return tmp;
tmp = tmp.right;
}
}
}
/********************* Method Begins Here **********************/
/**
* A wrapper method for a method that computes the
* sum of the differential keys of this binary search tree.
* @return the sum of the differential keys of this tree.
*/
@Override
public int sigma()
{
if (size == 0)
return 0;
if (root.data.getClass() != Integer.class)
throw new IllegalArgumentException("Keys must be integers");
return (Integer)root.data + sigma(root);
}
/**
* An auxiliary method that recursively computes the sum of
* differential keys in the subtrees of the tree rooted at
* the specified key.
* @param subtreeRoot the root of a subtree of this tree
* @return the sum of the differential keys of the left and
* right subtrees
*/
private int sigma(Node subtreeRoot)
{
if(subtreeRoot == null)
return 0;
if(subtreeRoot.left != null)
{
if(subtreeRoot.right != null)
{
return (Integer)subtreeRoot.data + sigma(subtreeRoot.left) + sigma(subtreeRoot.right);
}
else
return (Integer)subtreeRoot.data + sigma(subtreeRoot.left);
}
if(subtreeRoot.right != null)
return sigma(subtreeRoot.right) - (Integer)subtreeRoot.data;
return (Integer)subtreeRoot.data;
}
/********************* Method Ends Here **********************/
/**
* Determines whether this binary tree is perfect
* @return true if the binary tree is perfect, otherwise false
*/
@Override
public boolean isPerfect()
{
}
/**
* A wrapper method that computes the height of this tree
* @return the height of this tree
*/
@Override
public int height()
{
return height(root);
}
/**
* An auxiliary method that recursively computes
* the height of the subtree rooted at the specified node.
* @param node a root of a subtree
* @return the height of this tree
*/
private int height(Node node)
{
if(node == null)
return 0;
return 1 + Math.max(height(node.left), height(node.right));
}
/**
* Determines whether this binary tree is complete.
* @return true if this binary tree is complete, otherwise false
*/
@Override
public boolean isComplete()
{
}
/**
* An auxiliary method that recursively determines whether
* the index of the subtree rooted at the specified node is
* less than the size of this tree.
* @param node a root of a subtree
* @param index the index of this node
* @return
*/
private boolean isComplete(Node node, int index)
{
}
}
int sigma_aux (node root, int parent_val) {
if !node
return 0
else
root_val = root->data
return root_val - parent_val +
sigma_aux(root->left , root_val) +
sigma_aux(root->right, root_val)
return (Integer) subtreeRoot.data - (Integer) root.data +
sigma(subtreeRoot.left) + sigma(subtreeRoot.right);
private int sigma(Node subtreeRoot) {
int tot = 0;
if (subtreeRoot == null) {
return 0;
}
if (findParent(subtreeRoot) == null) {
tot = sigma(subtreeRoot.left) + sigma(subtreeRoot.right);
} else{
tot = (Integer) subtreeRoot.data - (Integer) findParent(subtreeRoot).data
+ sigma(subtreeRoot.left) + sigma(subtreeRoot.right);
}
return tot;
}