Binary tree Find maximum minimum value, delete any node, find node

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.