In conjunction with the previous article, we continue to delve into the operations of binary trees. Get maximum minimum value
Finding the maximum minimum value in a binary tree is not a rare matter. Because of the unique structure of the binary tree, the leftmost leaf node has the lowest value. The rightmost leaf node has the largest value.
Gets the minimum
function getmin (node) {
var current = node;
while (!) ( Current.left = = null)) {current
= Current.left;
}
return current;
}
function Getmax (node) {
var current = node;
while (!) ( Current.right = = null)) {current
= Current.right;
}
return current;
}
Delete any node.
The question to be distinguished is whether the deleted node has a leaf node.
Then, recursively, remove any nodes.
Delete a node
function RemoveNode (node,deletenode) {
if (node = = null) {
return null;
}
if (Node.data = = Deletenode) {
if (Node.left = = NULL && Node.right = = null) {//If it is a leaf node
node.data = null;
return null;
} else if (node.right = = null) {//If only left dial hand node is
return node.left;
} else if (node.left = = = null) {//If only the right child node is
return node.right;
} else{
var tempnode = bst.getmin (node.right);
Node.data = Tempnode.data;
Delete Right minimum value
node.right = RemoveNode (node.right,tempnode.data);
return node;
}
} else if (Node.data < Deletenode) {
node.right = RemoveNode (node.right,deletenode);
Return node;//current node no problem, right node may be replaced
}else{
node.left = RemoveNode (node.left,deletenode);
Return node;//current node no problem, left node may be replaced
}
}
Find any node
Find and Find2 prove that recursion and iteration, in some cases, can be transformed into one another.
Finds the given value if the return value is found, if not found returns null
//with recursive implementation
function find (node,value) {
if (node! = null) {
Console.log ( Node.data);
if (Node.data = = value) {
return node.data;
}
return find (Node.left,value) | | Find (Node.right,value);
return find (Node.right,value);
}
}
Finds the given value if a return value is found, if not found returns NULL
//implemented with iteration
function Find2 (value) {
var current = This.root;
while (current = null) {
console.log (current.data)
if (Current.data = = value) {
return current.data;
} else if (value < Current.data) {current
= Current.left;
} else{current
= Current.right;
}
}
return null;
}