在Java中查找二进制搜索树中的最小和最大元素
时间:2020-02-23 14:34:11 来源:igfitidea点击:
在本教程中,我们将看到如何在二进制搜索树中找到最小和最大元素。
找到最小元素:
最小元素只不过是二进制搜索树中最左边的节点,因此留下了左侧,直到我们获得最左边的元素。
//Get minimum element in binary search tree public static TreeNode minimumElement(TreeNode root) { if(root.left==null) return root; else { return minimumElement(root.left); } }
找到最大元素:
最大元素只不过是二进制搜索树中最右边的节点,因此遍历直到获得最右边的元素。
//Get maximum element in binary search tree public static TreeNode maximumElement(TreeNode root) { if(root.right==null) return root; else { return maximumElement(root.right); } }
完整的Java程序:
package org.igi.theitroad.binarysearchtree; public class BinarySearchTreeMinMaxMain { public static class TreeNode { int data; TreeNode left; TreeNode right; TreeNode(int data) { this.data=data; } } public static boolean search(TreeNode root,TreeNode nodeToBeSearched) { if(root==null) return false; if(root.data== nodeToBeSearched.data) { return true; } boolean result=false; if(root.data > nodeToBeSearched.data) result=search(root.left,nodeToBeSearched); else if(root.data < nodeToBeSearched.data) result= search(root.right,nodeToBeSearched); return result; } //Get minimum element in binary search tree public static TreeNode minimumElement(TreeNode root) { if(root.left==null) return root; else { return minimumElement(root.left); } } //Get maximum element in binary search tree public static TreeNode maximumElement(TreeNode root) { if(root.right==null) return root; else { return maximumElement(root.right); } } public static TreeNode insert(TreeNode root,TreeNode nodeToBeInserted) { if(root==null) { root=nodeToBeInserted; return root; } if(root.data > nodeToBeInserted.data) { if(root.left==null) root.left=nodeToBeInserted; else insert(root.left,nodeToBeInserted); } else if(root.data < nodeToBeInserted.data) if(root.right==null) root.right=nodeToBeInserted; else insert(root.right,nodeToBeInserted); return root; } public static void inOrder(TreeNode root) { if(root==null) return; inOrder(root.left); System.out.print(root.data+" "); inOrder(root.right); } public static void main(String[] args) { //Creating a binary search tree TreeNode rootNode=createBinarySearchTree(); System.out.println("Minimum element in binary search tree: "+minimumElement(rootNode).data); System.out.println("Maximum element in binary search tree: "+maximumElement(rootNode).data); } public static TreeNode createBinarySearchTree() { TreeNode rootNode =new TreeNode(40); TreeNode node20=new TreeNode(20); TreeNode node10=new TreeNode(10); TreeNode node30=new TreeNode(30); TreeNode node60=new TreeNode(60); TreeNode node50=new TreeNode(50); TreeNode node70=new TreeNode(70); TreeNode node5=new TreeNode(5); TreeNode node55=new TreeNode(55); insert(null,rootNode); insert(rootNode,node20); insert(rootNode,node10); insert(rootNode,node30); insert(rootNode,node60); insert(rootNode,node50); insert(rootNode,node70); insert(rootNode,node5); insert(rootNode,node55); return rootNode; } }
运行上面的程序时,我们将得到以下输出:
Minimum element in binary search tree: 5 Maximum element in binary search tree: 70