Array menampilkan nilai max, min, rata dari sejumlah siswa. Ditentukan, kemudian: a. Tampilkan data dari yang awal hingga akhir b. Hitung Jumlah nilai seluruh siswa c. Hitung rata-rata d. Nilai Maksimum e. Nilai Minimum. Cara menjalankan program java di notepad.
Given a Binary Tree, find the maximum(or minimum) element in it. For example, maximum in the following Binary Tree is 9.
In Binary Search Tree, we can find maximum by traversing right pointers until we reach the rightmost node. But in Binary Tree, we must visit every node to figure out maximum. So the idea is to traverse the given tree and for every node return maximum of 3 values.
1) Node’s data.
2) Maximum in node’s left subtree.
3) Maximum in node’s right subtree.
1) Node’s data.
2) Maximum in node’s left subtree.
3) Maximum in node’s right subtree.
Below is the implementation of above approach.
CPP
// minimum in a Binary Tree #include<iostream> class Node public : Node* left, *right; /* Constructor that allocates a new left and right pointers. */ { this ->left = NULL; } // Binary Tree { if (root NULL) // 1) Root's data 2) Max in Left Subtree int res = root->data; int rres = findMax(root->right); res = lres; res = rres; } // Driver Code { Node *root = new Node(2); root->right = new Node(5); root->left->right->left = new Node(1); root->right->right = new Node(9); << findMax(root) << endl; return 0; // rathbhupendra |
C
// C program to find maximum and minimum in a Binary Tree #include <stdlib.h> struct Node int data; }; // A utility function to create a new node { malloc ( sizeof ( struct Node)); node->left = node->right = NULL; } // Returns maximum value in a given Binary Tree { if (root NULL) // 1) Root's data 2) Max in Left Subtree int res = root->data; int rres = findMax(root->right); res = lres; res = rres; } // Driver program { struct Node *root = newNode(2); root->right = newNode(5); root->left->right->left=newNode(1); root->right->right=newNode(9); printf ( 'Maximum element is %d n' , findMax(root)); return 0; |
Java
// Binary Tree // A binary tree node int data; { left = right = null ; } class BinaryTree { static int findMax(Node node) if (node null ) int lres = findMax(node.left); res = lres; res = rres; } /* Driver program to test above functions */ { tree.root = new Node( 2 ); tree.root.right = new Node( 5 ); tree.root.left.right.left = new Node( 1 ); tree.root.right.right = new Node( 9 ); tree.findMax(tree.root)); } // This code is contributed by Kamal Rawal |
Python3
![Cara Mencari Data Min Max Program C Cara Mencari Data Min Max Program C](http://4.bp.blogspot.com/-JZYKmoNoYO8/T2hqjLFBOnI/AAAAAAAAAD4/O79rRnduIXQ/s320/Program+maxmin.jpg)
# and minimum in a Bianry Tree # A class to create a new node def __init__( self , data): self .left = self .right = None # Returns maximum value in a def findMax(root): # Base case return - 999999999999 # Return maximum of 3 values: # 3) Max in right subtree lres = findMax(root.left) if (lres > res): if (rres > res): return res # Driver Code root = newNode( 2 ) root.right = newNode( 5 ) root.left.right.left = newNode( 1 ) root.right.right = newNode( 9 ) findMax(root)) # This code is contributed by PranchalK |
C#
// Binary Tree // A binary tree node { public Node left, right; public Node( int data) this .data = data; } { public static int findMax(Node node) if (node null ) return int .MinValue; int lres = findMax(node.left); { } { } } /* Driver program to test above functions */ { tree.root = new Node(2); tree.root.right = new Node(5); tree.root.left.right.left = new Node(1); tree.root.right.right = new Node(9); Console.WriteLine( 'Maximum element is ' + BinaryTree.findMax(tree.root)); } // This code is contributed by Shrikant13 |
Output:
Similarly, we can find the minimum element in a Binary tree by comparing three values. Below is the function to find a minimum in Binary Tree.
C
int findMin( struct Node* root) // Base case return INT_MAX; // Return minimum of 3 values: // 3) Max in right subtree int lres = findMin(root->left); if (lres < res) if (rres < res) return res; |
Java
static int findMin(Node node) if (node null ) int lres = findMin(node.left); res = lres; res = rres; } |
C#
public static int findMin(Node node) if (node null ) int lres = findMin(node.left); res = lres; res = rres; } // This code is contributed by Code_Mech |
This article is contributed by Himanshu Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
Improved By : shrikanth13, PranchalKatiyar, Code_Mech, rathbhupendra, Akanksha_Rai
This is quite simple. Just traverse the node from root to left recursively until left is NULL. The node whose left is NULL is the node with minimum value.
For the above tree, we start with 20, then we move left 8, we keep on moving to left until we see NULL. Since left of 4 is NULL, 4 is the node with minimum value.
C++
//C++ program to find minimum value node in binary search Tree. using namespace std; /* A binary tree node has data, pointer to left child struct node int data; struct node* right; with the given data and NULL left and right struct node* newNode( int data) struct node* node = ( struct node*) node->data = data; node->right = NULL; return (node); inserts a new node with the given number in root pointer which the caller should then use parameters). */ { single node */ return (newNode(data)); { if (data <= node->data) else return node; } /* Given a non-empty binary search tree, tree. Note that the entire tree does not need int minValue( struct node* node) struct node* current = node; /* loop down to find the leftmost leaf */ { } } /* Driver Code*/ { root = insert(root, 4); insert(root, 1); insert(root, 6); cout << 'n Minimum value in BST is ' << minValue(root); return 0; |
C
#include<stdlib.h> /* A binary tree node has data, pointer to left child struct node int data; struct node* right; with the given data and NULL left and right struct node* newNode( int data) struct node* node = ( struct node*) node->data = data; node->right = NULL; return (node); inserts a new node with the given number in root pointer which the caller should then use parameters). */ { single node */ return (newNode(data)); { if (data <= node->data) else return node; } /* Given a non-empty binary search tree, tree. Note that the entire tree does not need int minValue( struct node* node) { while (current->left != NULL) { } } /* Driver program to test sameTree function*/ { root = insert(root, 4); insert(root, 1); insert(root, 6); printf ( 'n Minimum value in BST is %d' , minValue(root)); return 0; |
Java
// Java program to find minimum value node in Binary Search Tree // A binary tree node Node left, right; Node( int d) { left = right = null ; } class BinaryTree { static Node head; /* Given a binary search tree and a number, the correct place in the tree. Returns the new (the standard trick to avoid using reference Node insert(Node node, int data) { /* 1. If the tree is empty, return a new, if (node null ) { } else { /* 2. Otherwise, recur down the tree */ node.left = insert(node.left, data); node.right = insert(node.right, data); return node; } /* Given a non-empty binary search tree, tree. Note that the entire tree does not need int minvalue(Node node) { while (current.left != null ) { } } // Driver program to test above functions BinaryTree tree = new BinaryTree(); root = tree.insert(root, 4 ); tree.insert(root, 1 ); tree.insert(root, 6 ); System.out.println( 'Minimum value of BST is ' + tree.minvalue(root)); } // This code is contributed by Mayank Jaiswal |
Python
# Python program to find the node with minimum value in bst # A binary tree node def __init__( self , key): self .left = None inserts a new node with the given number in root pointer which the caller should then use parameters). '' # single node return (Node(data)) else : if data < = node.data: else : return node '' Given a non-empty binary search tree, tree. Note that the entire tree does not need def minValue(node): while (current.left is not None ): root = None insert(root, 2 ) insert(root, 3 ) insert(root, 5 ) print 'nMinimum value in BST is %d' % (minValue(root)) # This code is contributed by Nikhil Kumar Singh(nickzuck_007) |
C#
// C# program to find minimum value node in Binary Search Tree // A binary tree node { public int data; { left = right = null ; } public class BinaryTree inserts a new node with the given number in root pointer which the caller should then use parameters). */ { /* 1. If the tree is empty, return a new, if (node null ) return ( new Node(data)); else if (data <= node.data) node.left = insert(node.left, data); else node.right = insert(node.right, data); return node; } /* Given a non-empty binary search tree, tree. Note that the entire tree does not need public virtual int minvalue(Node node) Node current = node; /* loop down to find the leftmost leaf */ { } } // Driver program to test above functions { Node root = null ; tree.insert(root, 2); tree.insert(root, 3); tree.insert(root, 5); Console.WriteLine( 'Minimum value of BST is ' + tree.minvalue(root)); } // This code is contributed by Shrikant13 |
PHP
// PHP program to find the node with class node private $node , $left , $right ; { $left = $right = NULL; function set_left( $left ) $this ->left = $left ; function set_right( $right ) $this ->right = $right ; function get_left() return $this ->left; function get_right() return $this ->right; function get_node() return $this ->node; // in a Binary Search Tree { get the minimum value*/ { } } // code to creating a tree $lnode = new node(2); $lnode ->set_right( new node(3)); $rnode ->set_left( new node(5)); $node ->set_right( $rnode ); $minimum_value = get_minimum_value( $node ); $minimum_value ; // This code is contributed ?> |
Output:
Time Complexity: O(n) Worst case happens for left skewed trees.
Similarly we can get the maximum value by recursively traversing the right node of a binary search tree.
![Find min and max in binary search tree java Find min and max in binary search tree java](/uploads/1/2/5/7/125755641/944206042.jpg)
References:
http://cslibrary.stanford.edu/110/BinaryTrees.html
http://cslibrary.stanford.edu/110/BinaryTrees.html
Recommended Posts:
Improved By : shrikanth13, DeepikaPathak, Code_Mech, rathbhupendra