Sử dụng cây tìm kiếm nhị phân để viết ứng dụng quản lí tài khoản ngân hàng. Mỗi một tài khoản gồm mã tài khoản (duy nhất) và số dư tài khoản. Ứng dụng cho phép thêm tài khoản, sửa số dư tài khoản, tìm kiếm tài khoản theo mã tài khoản.

Vận dụng

1. Sử dụng cây tìm kiếm nhị phân để viết ứng dụng quản lí tài khoản ngân hàng. Mỗi một tài khoản gồm mã tài khoản (duy nhất) và số dư tài khoản. Ứng dụng cho phép thêm tài khoản, sửa số dư tài khoản, tìm kiếm tài khoản theo mã tài khoản.


Hướng dẫn gợi ý về cách sử dụng cây tìm kiếm nhị phân để viết ứng dụng quản lí tài khoản ngân hàng thông qua một chương trình mẫu sau: 

class Account:

    def __init__(self, account_id, balance):

        self.account_id = account_id

        self.balance = balance

        self.left = None

        self.right = None

class BankAccountManager:

    def __init__(self):

        self.root = None

    def insert(self, root, account_id, balance):

        if root is None:

            return Account(account_id, balance)

        if account_id < root.account_id:

            root.left = self.insert(root.left, account_id, balance)

        elif account_id > root.account_id:

            root.right = self.insert(root.right, account_id, balance)

        return root

    def add_account(self, account_id, balance):

        self.root = self.insert(self.root, account_id, balance)

    def search(self, root, account_id):

        if root is None or root.account_id == account_id:

            return root

        if account_id < root.account_id:

            return self.search(root.left, account_id)

        return self.search(root.right, account_id)

    def find_account(self, account_id):

        return self.search(self.root, account_id)

    def update_balance(self, account_id, new_balance):

        account = self.find_account(account_id)

        if account:

            account.balance = new_balance

        else:

            print("Account not found.")

# Sử dụng ứng dụng

bank_manager = BankAccountManager()

# Thêm tài khoản

bank_manager.add_account(1001, 50000)

bank_manager.add_account(1002, 100000)

bank_manager.add_account(1003, 75000)

# Tìm kiếm tài khoản

account = bank_manager.find_account(1002)

if account:

    print(f"Account found - Account ID: {account.account_id}, Balance: {account.balance}")

else:

    print("Account not found.")

# Cập nhật số dư tài khoản

bank_manager.update_balance(1001, 60000)

# Kiểm tra lại số dư sau khi cập nhật

account = bank_manager.find_account(1001)

if account:

    print(f"Updated balance - Account ID: {account.account_id}, Balance: {account.balance}")

else:

    print("Account not found.")

Chú thích trong chương trình này:

  • Lớp Account được sử dụng để đại diện cho mỗi tài khoản, với hai thuộc tính là account_id (mã tài khoản) và balance (số dư tài khoản).
  • Lớp BankAccountManager chứa các phương thức để thêm tài khoản, tìm kiếm tài khoản và cập nhật số dư tài khoản. Mỗi tài khoản được lưu trữ trong một cây tìm kiếm nhị phân, với account_id làm khóa.

Bình luận

Giải bài tập những môn khác