Introduction
Binary Search Trees (BSTs) are fundamental data structures used for efficient searching, insertion, and deletion operations. In this blog, we will explore the concept of BSTs, understand their working principles, and discuss their effectiveness in searching nodes within large lists, particularly in the context of FiveM Lua scripting.
-
Understanding Binary Search Trees:
A Binary Search Tree is a hierarchical data structure in which each node has at most two children, commonly referred to as the left child and the right child. These nodes are organized in a manner that allows for efficient search, insertion, and removal operations. The tree follows a particular ordering property, where the value of every node in the left subtree is less than its parent, while the value of every node in the right subtree is greater than or equal to its parent. -
Working Principle and Efficiency:
The efficiency of a Binary Search Tree lies in its ability to quickly locate a specific node within a large list, even when the number of nodes exceeds 10,000. The searching process in a BST involves comparing the target value with the value at the current node and recursively traversing either the left or right subtree based on the comparison result. This binary partitioning of the tree ensures that the search space is halved at each step, resulting in a time complexity of O(log n), where n represents the number of nodes. -
Example of Efficiency Improvements:
Lets take an example of building a housing resource. You want to draw a marker when a player is near a property. In your server, their are 10,000 properties. As we know, the EndTextCommandDisplayText and DrawMarker needs to be run every frame. If we use a for loop that iterates the entire existing property table and finds the nearest property, the time complexity for the search is O(n), which means for each frame you need to search 10,000 times to find the nearest property in the worst case. However, if we use the BST, the time complexity for the search is O(log n), which means for each frame you only need to searchlog2(10000) â 13.8 = 14times in the worst case.
Implementing a Binary Search Tree
-
Building the BST:
To start, you would populate the BST with your coordinate data. Each coordinate becomes a node in the tree, where the left and right children are determined based on their respective values. The construction of the BST ensures that the left subtree of any node contains only smaller values, while the right subtree contains only larger values. -
Searching the Nearest Coordinate:
To find the nearest coordinate to the player, the BST provides an optimized approach. By comparing the playerâs current position with the root nodeâs coordinate, you can determine whether to traverse the left or right subtree. This process is repeated until the nearest coordinate is found. -
Time Complexity:
The beauty of BST lies in its time complexity for searching operations. In a balanced BST, the time complexity for searching, insertion, and deletion is O(log n), where n is the number of nodes in the tree. Even with a large number of nodes (e.g., nodeCount > 10,000), BST can quickly locate the nearest coordinate, making it a perfect fit for efficient gameplay experiences.
Example Implementation
For a hands-on experience, you can find an example implementation using BST for nearest coordinate search in this link. In this example, each node can hold both the coordinate and other data, such as a table or a value, in your original data structure (table). An additional feature has been added to find a list of coordinates within a specified range.
Note
-
Please be aware that all example code provided in this blog post has been initially generated using ChatGPT, a language model developed by OpenAI. While efforts have been made to ensure the accuracy and functionality of the code, it is essential to review and modify the examples according to your specific requirements and coding standards.
-
Remember that while BSTs are efficient for searching and retrieval operations, they might not always be the best choice for every scenario. It is crucial to evaluate the characteristics of your data and the specific requirements of your application to determine if a BST is the optimal data structure for your use case.