Constructing a binary search tree is easy, because we can go for inserting each item only by comparing it with the root and decide where to go (left or right) based on its value.
Binary search tree – BST
The binary search tree is a specific kind of binary tree, where the each item keeps greater elements on the right, while the smaller items are on the left.
However a binary tree isnРІР‚в„ўt more successful in searching than any other tree or data structure. If the items arenРІР‚в„ўt placed in a specific order we must go through the entire tree in order to find the searched item. This isnРІР‚в„ўt a great optimization, so we must put an order in it to improve the searching process.
Binary trees are especially important because they can contain ordered data in a specific manner. Building a binary tree isnРІР‚в„ўt difficult at all and itРІР‚в„ўs very similar to building a linked list.
In the binary tree each node has at most two sub-trees – left and right!
A binary tree is a tree where each item can have at most two children.
If weРІР‚в„ўre looking at the root of the tree we can assume there are two sub-trees – one left and one right. However if we isolate only one of these sub-trees we can again think of it as a tree and assume that it has one left and one right sub-trees and go recursively with this definition.
In these terms only the root has no parent, and each item can have as many children as possible. Here are some trees in form of a diagrams.
If there is no item in the tree the tree is considered empty.
Of course if the item doesnРІР‚в„ўt have children, they are NIL, then this is considered a leaf in the tree terminology. In the other hand if the item doesnРІР‚в„ўt have parent item it is considered the root.
A tree data structure. Each item points to its parent and its children. However the root’s parent it’s NIL.
The tree is a data structure where each item, except of keeping some data, keeps a reference (pointer) to its children and its parent.
Because of this natural problem of linked lists searching is slow and obviously we canРІР‚в„ўt make it better. The only way to improve searching over dynamic data structures is to use different data structure.
In terms of arrays, we could perform binary search and go directly in the middle of the array, then jump back or forward. That is because we can access array items directly using their index. However as we saw the linked lists unlike arrays canРІР‚в„ўt benefit of a direct access and we must go item by item.
Sequential search over arrays seems much like searching in linked lists and it is a basically ineffective opration!
Indeed searching in a linked list has a linear complexity and in the worst case we must go through the entire list in order to find the desired element. The worst case is when the item doesnРІР‚в„ўt belong to the list and we must check every single item of the list even the last one without success. This approach seems much like the over arrays. Of course this is bad when we talk about large data sets.
Constructing a is a fairly simple task. Linked lists are a linear structure and the items are located one after another, each pointing to its predecessor and its successor. Almost every operation is easy to code in few lines and doesnРІР‚в„ўt require advanced skills. Operations like insert, delete, etc. over linked lists are performed in a linear time. Of course on small data sets this works fine, but as the data grows these operations, especially the search operation becomes too slow.
Computer Algorithms: Binary Search Tree
Computer Algorithms: Binary Search Tree
Комментариев нет:
Отправить комментарий