如何在二叉搜索树Java中添加Delete方法
我正在创建一个二叉搜索树delete node方法,到目前为止,我有一个查找max和min的方法,还有insert方法 你能给我一个大概的算法吗 这是我目前的工作如何在二叉搜索树Java中添加Delete方法,java,data-structures,binary-search-tree,nodes,Java,Data Structures,Binary Search Tree,Nodes,我正在创建一个二叉搜索树delete node方法,到目前为止,我有一个查找max和min的方法,还有insert方法 你能给我一个大概的算法吗 这是我目前的工作 public class BST { private Node root; //first node top of the tree public void insert(int key){ Node newNode = new Node (key); if(root
public class BST {
private Node root; //first node top of the tree
public void insert(int key){
Node newNode = new Node (key);
if(root == null){ //check node, no tree yet
root = newNode; //give the tree a node
}
else {
Node current = root; //assigned root as current
Node parent;
while(true) {
parent = current;
if (key < current.key) {
current = current.leftChild;
if(current == null) {
parent.leftChild = newNode;
return;
}
} else {
current = current.rightChild;
if(current == null) {
parent.rightChild = newNode;
return;}
}
}
}
}
public Node findMin(){
Node current = root; //assigned root as current
Node last = null;
while(current != null) {
last = current;
current = current.leftChild;
}
return last;
}
public Node findMax() {
Node current = root;
Node last = null;
while(current != null) {
last = current;
current = current.rightChild;
}
return last;
}
}
public class Node {
int key;
Node leftChild, rightChild;
public Node(int key){
super();
this.key = key;
}
}
public class BSTApp {
public static void main (String args[]){
BST tree = new BST();
tree.insert(10);
tree.insert(15);
tree.insert(100);
tree.insert(88);
tree.insert(2);
System.out.println(tree.findMin().key); //Search for Min in BST
System.out.println(tree.findMax().key); //Search for Min in BST
}
}
公共类BST{
私有节点根;//树顶部的第一个节点
公共无效插入(int键){
Node newNode=新节点(键);
如果(root==null){//检查节点,还没有树
root=newNode;//给树一个节点
}
否则{
节点当前=根;//将根分配为当前
节点父节点;
while(true){
父项=当前;
如果(键<当前键){
current=current.leftChild;
如果(当前==null){
parent.leftChild=newNode;
返回;
}
}否则{
current=current.rightChild;
如果(当前==null){
parent.rightChild=newNode;
返回;}
}
}
}
}
公共节点findMin(){
节点当前=根;//将根分配为当前
节点last=null;
while(当前!=null){
last=当前值;
current=current.leftChild;
}
最后返回;
}
公共节点findMax(){
节点电流=根;
节点last=null;
while(当前!=null){
last=当前值;
current=current.rightChild;
}
最后返回;
}
}
公共类节点{
int键;
节点leftChild,righchild;
公共节点(int键){
超级();
this.key=key;
}
}
公共类应用程序{
公共静态void main(字符串参数[]){
BST树=新的BST();
树。插入(10);
插入(15);
树。插入(100);
树.插入(88);
树。插入(2);
System.out.println(tree.findMin().key);//在BST中搜索Min
System.out.println(tree.findMax().key);//在BST中搜索Min
}
}
我正在努力推进我的学习,以便在数据结构和算法方面取得进步。我希望你们能帮助我。静态void delete(Node root,int key)
static void delete(Node root, int key)
{
if (root == null)
return;
if (root.left == null &&
root.right == null)
{
if (root.key == key)
return;
else
return;
}
Queue<Node> q = new LinkedList<Node>();
q.add(root);
Node temp = null, keyNode = null;
// Do level order traversal until
// we find key and last node.
while (!q.isEmpty())
{
temp = q.peek();
q.remove();
if (temp.key == key)
keyNode = temp;
if (temp.left != null)
q.add(temp.left);
if (temp.right != null)
q.add(temp.right);
}
if (keyNode != null)
{
int x = temp.key;
deleteDeepest(root, temp);
keyNode.key = x;
}
}
{
if(root==null)
返回;
如果(root.left==null&&
root.right==null)
{
if(root.key==key)
返回;
其他的
返回;
}
队列q=新的LinkedList();
q、 添加(根);
Node temp=null,keyNode=null;
//执行水平顺序遍历,直到
//我们找到键和最后一个节点。
而(!q.isEmpty())
{
温度=q.peek();
q、 删除();
如果(临时键==键)
keyNode=temp;
如果(左侧温度!=null)
q、 添加(左侧温度);
如果(临时正确!=null)
q、 添加(右侧温度);
}
if(keyNode!=null)
{
int x=临时键;
温度(根、温度);
keyNode.key=x;
}
}
公共节点deleteNode(节点n,int键){
如果(n==null){
返回n;
}
如果(键n.key){
n、 rightChild=deleteNode(n.rightChild,键);
}否则{
if(n.leftChild==null){
返回n.rightChild;
}else if(n.rightChild==null){
返回n.leftChild;
}
n、 键=最小值(n.rightChild);
n、 rightChild=deleteNode(n.rightChild,n.key);
}
返回n;
}
公共void deleteNode(int键){
deleteNode(根,键);
}
公共整数最小值(节点){
if(node.leftChild==null){
返回node.key;
}
返回最小值(node.leftChild);
}
public Node deleteNode(Node n, int key) {
if (n == null) {
return n;
}
if (key < n.key) {
n.leftChild = deleteNode(n.leftChild, key);
} else if (key > n.key) {
n.rightChild = deleteNode(n.rightChild, key);
} else {
if (n.leftChild == null) {
return n.rightChild;
} else if (n.rightChild == null) {
return n.leftChild;
}
n.key = minimum(n.rightChild);
n.rightChild = deleteNode(n.rightChild, n.key);
}
return n;
}
public void deleteNode(int key) {
deleteNode(root, key);
}
public int minimum(Node node) {
if (node.leftChild == null) {
return node.key;
}
return minimum(node.leftChild);
}