Conceptual
Login

Implementing a Binary Search Tree with Dynamically Allocated Nodes in C and C++

A binary search tree is defined recursively as a binary tree in which, for every node, all keys in the left subtree are lesser or equal and all keys in the right subtree are greater, with both subtrees themselves being binary search trees. Realizing this non-linear logical structure in memory follows the linked-structure paradigm: each node is an object holding a data field plus two child references, allocated in the heap and reached only through pointers, with the tree's entire identity carried by a single pointer to the root (null denoting the empty tree). Insertion and search are naturally expressed by recursion that compares the key against the current node to select a subtree, exploiting the self-similarity of the definition; this places the topic in the data-structures branch of computer science, generalizing linked-list node-and-pointer technique from a linear to a hierarchical arrangement.