peri hokiperihokiclarustologisticsrimashaop.compola taktis menang gates of olympus x1000 perihokipemain perihoki bongkar trik jepep starlight princess x1000perihoki beri panduan lengkap bermain mahjong ways 2 pgsofttips cara penghasil uang pgsoft mahjong ways 2 perihokimahjong wins 3 perihoki memberikan penggali terbesarfenomena gates of olympus buka peluang besar bagi duta76cara dapat pola emas rahasia di duta7 wild west goldtrik menang pgsoft mahjong ways 2 modal receh maxwin duta76duta76 main pgsoft mahjong ways 2 di sela istirahatscatter hitam mahjong wins 3 spin pasti jepe duta76perihoki spin mahjong ways 2 pgsoft scatter hitam maxwinsimbol mahjong wins 3 yang sering muncul di awal perihokitrik gampang menang mahjong winscuan mahjong wins 3 tanpa hentipola spin taruhan kecil mahjong winsstrategi kunci scatter hitam beruntungacor total mahjong wayskemenangan maksimal mahjong wayskemenangan pria pekanbaru mahjong waysmahjong wins3 fenomena baru sumatera baratmenangkan mahjong dengan lincahmenembus batas keberuntungan mahjong waysmenjajal keberuntungan di mahjong wayspola gratis mahjong ways1scatter hitam senjata rahasia mahjong winsteknik spinning menguntungkan untuk scatter hitamanalisis rtp tertinggi mahjong winsfenomena scatter hitam mahjong winsibu rumah tangga dan keberuntungan mahjongkeseruan main mahjong wins3menang mahjong pengguna baru di sosial mediamenghargai setiap momen di mahjong waysserunya main mahjong di warung internetspin slow motion mancing scatterstrategi jitu maxwin mahjong waysstrategi jitu menang mahjong modal kecil jackpot besaranak kampung papua menggemparkan mahjongkeajaiban scatter hitam mahjong wayskeberhasilan joni mahjong winskejaiban mahjong ways di ujung jalanketua mahjong wins3 kehadiran kemenangan dimulai dari balimahjong kehadiran yang menguntungkanmahjong menggila adat88panduan kilat raih 108 jutatips memenangkan mahjongbarista kopi menang 110 juta spin mahjongdakota76 scatter hitam mahjong wins 3gagal cpns menang mahjong wins 3 rp120 jutakisah karyawan minimarket dapat thr lebih awal mahjong winskisah pelajar sma kuasai mahjong wins 3 inspirasi sekolahkisah penjaga parkir tanah abang motor baru mahjong winskisah sukses ibu rumah tangga bandung mahjong winsmahasiswa kkn beli iphone15 mahjong wins3 dakotamantan anak band influencer mahjong scatter hitampola rtp senin gates of olympus guru matematikaidola scatter hitam gampang meledakidola sensasi jackpotidola pola scatter hitamidola hasilkan jackpot dalam waktu singkatidola mahjong wins 3 bocortukang cuci motor menemukan keberuntungan di mahjong

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Tìm phần tử nhỏ nhất của cây nhị phân trong Java
CĂN BẢN
CHUỖI
VÒNG LẶP
NUMBER
INPUT / OUTPUT
COLLECTIONS
Ví dụ danh sách liên kết đơn Java Đếm số node trong Singly Linked List trong Java Đảo ngược thứ tự của Singly Linked List trong Java Xóa node đầu tiên của Singly Linked List trong Java Xóa node ở giữa của Singly Linked List trong Java Xóa node cuối cùng của Singly Linked List trong Java Thêm node vào vị trí đầu tiên của Singly Linked List trong Java Thêm node vào vị trí cuối cùng của Singly Linked List trong Java Thêm node vào vị trí giữa của Singly Linked List trong Java Kiểm tra danh sách liên kết đơn palindrome trong Java Tìm giá trị lớn nhất của Singly Linked List trong Java Tìm giá trị nhỏ nhất của Singly Linked List trong Java Xóa phần tử trùng lặp khỏi Singly Linked List trong Java Tìm kiếm phần tử của Singly Linked List trong Java Tạo một Circular Linked List trong Java Tạo một Circular Linked List Java và đảo ngược nó Xóa node đầu tiên của Circular Linked List trong Java Xóa node cuối cùng của Circular Linked List trong Java Xóa node ở giữa của Circular Linked List trong Java Thêm node vào vị trí đầu tiên của Circular Linked List trong Java Thêm node vào vị trí cuối cùng của Circular Linked List trong Java Thêm node vào vị trí giữa của Circular Linked List trong Java Tìm giá trị lớn nhất trong Circular Linked List Java Tìm giá trị nhỏ nhất trong Circular Linked List Tìm kiếm phần tử của Circular Linked List trong Java Xóa phần tử trùng lặp khỏi Circular Linked List trong Java Sắp xếp các phần tử của Circular Linked List trong Java Chuyển đổi Binary Tree thành Double Linked List trong Java Tạo double linked list từ cây bậc ba (ternary tree) trong Java Cách chuyển đổi từ cây nhị phân thành cây nhị phân tìm kiếm trong Java Xác định các lá có cùng cấp trong Tree của Java Kiểm tra hai cây giống nhau trong Java Tìm chiều rộng tối đa của cây nhị phân trong Java Tìm phần tử lớn nhất của cây nhị phân trong Java Tìm khoảng cách lớn nhất giữa các node của cây nhị phân trong Java Tìm phần tử nhỏ nhất của cây nhị phân trong Java Tính tổng giá trị của các node của cây nhị phân trong Java Tính tổng số cây nhị phân tìm kiếm có thể được tạo ra bởi N nodes trong Java Triển khai cây nhị phân từ danh sách liên kết trong Java Tìm kiếm node của cây nhị phân tìm kiếm trong Java Cách tạo ra Mirror Tree từ Binary Tree bằng ngôn ngữ Java Xác định các lá của binary tree sử dụng preoder trong Java Xác định đường biên(boundary traversal) của cây nhị phân trong Java Xóa node của cây nhị phân tìm kiếm trong Java Duyệt cây nhị phân bằng phương pháp inOder trong Java
CALCULATE
SẮP XẾP - TÌM KIẾM
KHÁC
NÂNG CAO
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Tìm phần tử nhỏ nhất của cây nhị phân trong Java

Tìm phần tử nhỏ nhất của cây nhị phân trong Java. Đây là một dạng bài tập để giúp bạn hiểu rõ hơn về Tree trong Java.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Dưới đây chúng ta sẽ thực hiện chương trình tìm phần tử nhỏ nhất của cây nhị phân trong Java.

Tìm phần tử nhỏ nhất của cây nhị phân trong Java

Trong phần này, freetuts sẽ hướng dẫn các bạn về thuật toán cũng như cách triển khai bài toán này trong Java.

Theo yêu cầu bài toán thì chúng ta sẽ tìm ra node nhỏ nhất trong cây nhị phân đã cho. Trước tiên, chúng tôi xác định biến min sẽ chứa dữ liệu của root. Sau đó, chúng tôi duyệt qua cây con bên trái để tìm node nhỏ nhất. So sánh nó với min và lưu trữ nhỏ nhất hai trong một biến min. Sau đó, chúng tôi duyệt qua cây con bên phải để tìm node nhỏ nhất và so sánh nó với giá trị nhỏ nhất. Cuối cùng, min sẽ có giá trị nhỏ nhất.

Bài viết này được đăng tại [free tuts .net]

java program to find the largest element in a binary tree png

Với cây nhị phân trên, để tìm kiếm phần tử có giá trị nhỏ nhất hãy cùng freetuts tìm hiểu về thuật toán của chương trình.

Thuật toán tìm phần tử nhỏ nhất của cây nhị phân trong Java

Các bước của thuật toán trên như sau:

Bước 1: Khởi tạo class Node có 3 thuộc tính: data, Node left và Node right

Bước 2: Dữ liệu của node sẽ được gán là data, các node left và right là null

Bước 3:

  • Root được gán bằng null
  • smallestElement() sẽ tìm ra phần tử có giá trị nhỏ nhất.
  • Nó kiểm tra xem gốc có rỗng không, nếu có thì nghĩa là cây rỗng.
  • Nếu cây không trống, hãy xác định biến min sẽ lưu trữ dữ liệu của tạm thời.
  • Tìm ra node nhỏ nhất trong cây con bên trái bằng cách gọi đệ quy smallestElement(). Lưu trữ giá trị đó trong leftmin. So sánh giá trị của min với leftmin và lưu trữ nhỏ nhất hai thành min.
  • Tìm ra node nhỏ nhất trong cây con bên phải bằng cách gọi đệ quy smallestElement(). Lưu trữ giá trị đó trong rightmin. So sánh giá trị của min với rightmin và lưu trữ nhỏ nhất hai thành min.
  • Cuối cùng, min sẽ giữ node nhỏ nhất trong cây nhị phân.

Cùng freetuts triển khai chương trình dưới đây để có thể hiểu hơn về yêu cầu bài toán.

Chương trình tìm kiếm phần tử nhỏ nhất của cây nhị phân trong Java

Dưới đây là chương trình mà freetuts đã viết, mời các bạn tham khảo:

/**
 * Học lập trình Java miễn phí tại freetuts.net
 *
 * @author freetuts
 */
public class smallestNode {

    public static class Node {

        int data;
        Node left;
        Node right;

        public Node(int data) {

            this.data = data;
            this.left = null;
            this.right = null;
        }
    }

    public Node root;

    public smallestNode() {
        root = null;
    }

    public int smallestElement(Node temp) {

        if (root == null) {
            System.out.println("Tree is empty");
            return 0;
        } else {
            int leftmin, rightmin;

            int min = temp.data;

            if (temp.left != null) {
                leftmin = smallestElement(temp.left);

                min = Math.min(min, leftmin);
            }

            if (temp.right != null) {
                rightmin = smallestElement(temp.right);

                min = Math.min(min, rightmin);
            }
            return min;
        }
    }

    public static void main(String[] args) {

        smallestNode bt = new smallestNode();

        bt.root = new Node(15);
        bt.root.left = new Node(20);
        bt.root.right = new Node(35);
        bt.root.left.left = new Node(74);
        bt.root.right.left = new Node(55);
        bt.root.right.right = new Node(6);

        System.out.println("Chương trình được đăng tại freetuts.net");

        System.out.println("Phần tử có giá trị nhỏ nhất của cây nhị phân đã cho là: " + bt.smallestElement(bt.root));
    }
}

Kết quả:

result find min jpg

Như vậy chúng ta đã thực hiện xong chương trình tìm giá trị độ rộng nhỏ nhất của cây nhị phân trong Java. Chúc các bạn thực hiện thành công!!!

Cùng chuyên mục:

Hướng dẫn Upload file với Spring Boot và jQuery Ajax trong Java

Hướng dẫn Upload file với Spring Boot và jQuery Ajax trong Java

Hướng dẫn download file với Spring Boot trong Java

Hướng dẫn download file với Spring Boot trong Java

Hướng dẫn Upload file với Spring Boot trong Java

Hướng dẫn Upload file với Spring Boot trong Java

Hướng dẫn CRUD với Spring Boot, REST và AngularJS trong Java

Hướng dẫn CRUD với Spring Boot, REST và AngularJS trong Java

Cách sử dụng Spring  Scheduled trong Spring Boot

Cách sử dụng Spring Scheduled trong Spring Boot

Cách dùng Groovy trong Spring Boot Java

Cách dùng Groovy trong Spring Boot Java

Cách dùng Spring Boot và Mustache trong Java

Cách dùng Spring Boot và Mustache trong Java

Cách dùng Spring Boot và MongoDB trong Java

Cách dùng Spring Boot và MongoDB trong Java

Cách tạo Restful Client bằng RestTemplate trong Spring Boot

Cách tạo Restful Client bằng RestTemplate trong Spring Boot

Hướng dẫn sử dụng Interceptor trong Spring Boot

Hướng dẫn sử dụng Interceptor trong Spring Boot

Sử dụng Twitter Bootstrap trong Spring Boot

Sử dụng Twitter Bootstrap trong Spring Boot

Tạo trang web đa ngôn ngữ với Spring Boot trong Java

Tạo trang web đa ngôn ngữ với Spring Boot trong Java

Tạo ứng dụng Chat với Spring Boot và Websocket

Tạo ứng dụng Chat với Spring Boot và Websocket

Sử dụng JUnit để tạo unit test trong Spring Boot

Sử dụng JUnit để tạo unit test trong Spring Boot

Cách triển khai Spring Boot trên Tomcat Server

Cách triển khai Spring Boot trên Tomcat Server

Cách test RESTful API trong Spring Boot

Cách test RESTful API trong Spring Boot

Cách dùng Spring Security trong Spring Boot để xác thực và phân quyền

Cách dùng Spring Security trong Spring Boot để xác thực và phân quyền

Duyệt cây nhị phân bằng phương pháp inOder trong Java

Duyệt cây nhị phân bằng phương pháp inOder trong Java

Xóa node của cây nhị phân tìm kiếm trong Java

Xóa node của cây nhị phân tìm kiếm trong Java

Bảo mật Spring Boot RESTful Service sử dụng Basic Authentication trong Java

Bảo mật Spring Boot RESTful Service sử dụng Basic Authentication trong Java

Top