TREES AND HIERARCHICAL STRUCTURES
TREES AND HIERARCHICAL STRUCTURES
Binary Trees and Binary Search Trees
Binary Tree
Binary Tree
It allows data to be organized hierarchically, making it valuable in scenarios where data relationships exist. A binary tree can have up to two child nodes per parent, providing flexibility in structuring data.
Every node acts either like a parent node or a child node. The topmost node of this tree is the root node. Every parent node may have only two children. They are commonly called as left child/child node and the right child/child node.
A node in the binary tree may have three fields:
· Data Element - functions as a storage for the data value that the node needs to store.
· Pointer to the Left Child - holds the address (or the reference to) the left child node.
· Pointer to the Right Child - holds the address (or the reference to) the right child node.
The end nodes or leaf nodes have no children, and a NULL pointer points to the left and right children of a leaf node.
In context, binary trees can be used in databases, such as organizing files into folders. The node, in this case the folder, represents a record, and this setup makes it easier to find specific records. It is like having a well-organized filing system for the information.
For example:
In the example, each node has either zero, one, or two children. Therefore, this tree is a binary tree.