Comprehensive Guide to Binary Trees, Traversals, and Binary Search Trees
Introduction to Trees and Non-Linear Data Structures
Classification of Data Structures:
Linked Lists, Stacks, and Queues are classified as Linear Data Structures.
Trees are classified as Non-Linear Data Structures.
Pointer and Node Connectivity:
In a Linked List, each node contains a link that points strictly to one other node.
In a Tree, each node may point to several other nodes, providing exceptional structural flexibility.
Application Example:
A tree data structure can represent hierarchical relationships, such as a person and all of their descendants.
Node Architecture:
Each tree node contains a data element (e.g., a name) alongside one or more pointers directed toward other tree nodes.
Fundamentals and Formal Definition of Binary Trees
Pointer Constraint:
A binary tree node can possess at most pointers directed to other nodes.
Formal Definition:
A Binary Tree is a finite set of elements that is either empty or partitioned into disjoint sub-sets:
The first sub-set contains a single element known as the root of the tree.
The other two sub-sets are themselves binary trees, termed the left sub-tree and the right sub-tree of the original tree.
Sub-tree Characteristics:
A left sub-tree or a right sub-tree can be completely empty.
Every individual element within a binary tree is termed a node.
Structural Examination of an Example Binary Tree:

* The tree consists of nodes in total, with node serving as the root.
* The left sub-tree is rooted at node , and the right sub-tree is rooted at node .
* Two branches emanate directly from node : branch to on the left and branch to on the right.
* The absence of an emanating branch indicates an empty sub-tree (e.g., the right sub-tree of node is empty).
Binary Tree Terminology and Hierarchical Relationships
Parent-Child (Father-Son) Terminology:
Node is the father of node .
Node is the left son of node .
Node is the right son of node .
Leaf Nodes:
A leaf node is defined as any node that has no sons.
In the reference -node tree, the leaf nodes are: , , and .
Non-Leaf Nodes:
A non-leaf node is any node that has one or more sons.
In the reference -node tree, the non-leaf nodes are: , , and .
Ancestor Relationships:
A node is said to be an ancestor of a node if is either the father of or the father of some ancestor of .
Example: Node is an ancestor of node (and node ).
Brother (Sibling) Relationships:
Two nodes are brothers if they are the left and right sons of the same father.
Example: Nodes and are brothers; nodes and are brothers.
Non-Binary Trees and Non-Tree Structures
Structural Criteria for Non-Trees:

* Graphs containing cycles (closed circular paths) are non-trees.
* Structures where a single node has multiple incoming parent links violate tree topology.
* Structures where nodes have more than children violate the strict definition of a binary tree.
Binary Tree Metrics and Specialized Classifications
Strictly Binary Tree:
A binary tree is classified as a Strictly Binary Tree if every non-leaf node has a non-empty left sub-tree and a non-empty right sub-tree.
Example node role partition:
Leaf nodes: , , ,
Non-leaf nodes: , ,
Degree of a Node:
The degree of a node is defined as the total number of nodes connected directly to that particular node.
The degree of a leaf node is always .
Degree breakdown for example nodes:
Level of a Node:
The root node of a tree is established at level .
Level assignments for example nodes:
Level :
Level : ,
Level : ,
Level : ,
Depth of a Binary Tree:
The depth of a binary tree is defined as the maximum level attained by any leaf within the tree.
This metric is equivalent to the length of the longest path from the root node to any leaf node.
In the reference example, the maximum depth is .
Complete Binary Tree:
A complete binary tree of depth is a strictly binary tree in which all leaf nodes are situated at the exact same level .

* A complete binary tree of depth contains all leaves strictly positioned at level .
Binary Tree Traversals
Concept of Traversal:
Traversal of a binary tree entails visiting every single node in the tree exactly once.
While linear lists have an inherent order from first to last, trees possess no single natural sequence for node visitation.
Memory Representation of a Node:
A binary tree node is represented in storage with three distinct fields:
DATA: The substantive value or data element stored within the node.LST LINK: Pointer to the Left Sub-Tree.RST LINK: Pointer to the Right Sub-Tree.
Traversal Orders and Algorithmic Steps:
Pre-order Traversal ():
Shorthand:
(root node, left st, right st)Step 1: Visit the Root.
Step 2: Traverse the left sub-tree in pre-order.
Step 3: Traverse the right sub-tree in pre-order.
In-order Traversal ():
Shorthand:
(Left st, root node, right st)Step 1: Traverse the left sub-tree in pre-order.
Step 2: Visit the Root.
Step 3: Traverse the right sub-tree in pre-order.
Post-order Traversal ():
Shorthand:
(left st, right st, root node)Step 1: Traverse the left sub-tree in pre-order.
Step 2: Traverse the right sub-tree in pre-order.
Step 3: Visit the Root.
Demonstration on Reference Binary Tree:

* Tree Layout: Root is node ; left child is node ; right child is node . Node has left child and right child .
* **In-order Traversal** (Left, Root, Right):
* **Pre-order Traversal** (Root, Left, Right):
* **Post-order Traversal** (Left, Right, Root): Binary Search Trees (BST)
Core Properties:
A Binary Search Tree (BST) is a node-based binary tree data structure characterized by three fundamental properties:
The left sub-tree of a node contains exclusively nodes with values less than the parent (root) node.
The right sub-tree of a node contains exclusively nodes with values greater than the parent (root) node.
Both the left and right sub-trees must each independently satisfy the requirements of a binary search tree.
Invariant Validation Example:

* For the root node :
* The left sub-tree contains values: , , , , . None of these values are greater than or equal to .
* The right sub-tree contains values: , , . None of these values are less than .
* Sub-tree evaluations:
* At node : Left child is (); right child is ().
* At node : Left child is (); right child is ().
* At node : Left child is (); right child is ().
Step-by-Step Construction of a Binary Search Tree
Insertion Dataset:
Input sequence:
BST Construction Algorithm:
Insert the first element () as the root of the tree.
Read the next element: if it is lesser than the root node element, insert it into the left sub-tree.
Otherwise, insert it into the right sub-tree.
Detailed Step Progression:

* **Step 1**: Insert .
* is the first element and becomes the tree root.
* **Step 2**: Insert .
* Compare with root : .
* is attached as the left child of .
* **Step 3**: Insert .
* Compare with root : .
* is attached as the right child of .
* **Step 4**: Insert .
* Compare with root : (navigate right to ).
* Compare with : .
* is attached as the right child of .
* **Step 5**: Insert .
* Compare with root : (navigate left to ).
* Compare with : .
* is attached as the right child of .
* **Step 6**: Insert .
* Compare with root : (navigate right to ).
* Compare with : .
* is attached as the left child of .
* **Step 7**: Insert .
* Compare with root : (navigate left to ).
* Compare with : (navigate right to ).
* Compare with : .
* is attached as the left child of .
* **Step 8**: Insert .
* Compare with root : (navigate left to ).
* Compare with : .
* is attached as the left child of .
* **Step 9**: Insert .
* Compare with root : (navigate right to ).
* Compare with : (navigate left to ).
* Compare with : .
* is attached as the left child of .