Comprehensive Guide to N-ary and Binary Search Trees
Introduction to N-ary and Generic Trees
N-ary Tree Node Structure:
- A node in an N-ary tree consists of a parent reference and multiple child references (Node 1, Node 2, …, Node n).
- This structure allows each node to have more than two children, unlike a standard binary tree.
Generic (N-ary) Tree Representation:
- The top-most node is the ROOT.
- Depth (): Represents the level of a node within the tree.
- : Root level.
- : Children of the root.
- : Subsequent levels.
- Height of the Tree (): The maximum depth of any node in the tree. In the provided example, the maximum depth is , thus .
Binary Tree Implementation with C
TreeNode Class Definition:
- The class is defined as
public class TreeNode<T> where T : IComparable<T>. This ensures that the generic type can be compared, which is essential for ordering operations. - Properties:
public T Value { get; set; }: Holds the data of the node.public TreeNode<T>? Left { get; set; }: Reference to the left child.public TreeNode<T>? Right { get; set; }: Reference to the right child.public TreeNode<T>? Parent { get; set; }: Reference to the parent node.
- The class is defined as
Constructor:
public TreeNode(T value, TreeNode<T>? parent = null, TreeNode<T>? left = null, TreeNode<T>? right = null)- It initializes the
Value,Parent,Left, andRightproperties with the provided arguments.
Visual Schematic of a Binary Tree Node:
- Every node contains a central Value, a pointer to its Parent above it, and two pointers (Left and Right) branching below it.
Properties and Characterization of Binary Trees
General Binary Tree Structure:
- Includes a ROOT node.
- Every node has at most two children.
- Metrics for the example provided:
- Depth levels range from to .
- The Height () of the tree is .
Perfect Binary Tree:
- Definition: A binary tree where all leaves are at the same level and all internal nodes have exactly two children.
- Leaf Node Calculation: The number of leaves is determined by . For a tree with height , the number of leaves is .
- Total Number of Nodes ():
- For height : .
- General Formula: .
- Using the geometric series sum formula: .
- Proof through manipulation:
- Height in terms of Nodes: Since , the height can be approximated as .
Tree Traversal Methodologies
Breadth-First Traversal (Level Order):
- Visits nodes level by level starting from the root.
- Sequence Example: HDLBGJACFIKE.
- Iterative Algorithm (Queue-based):
- Create a new Queue
Q. - Enqueue the root node.
- While
Q.Count > 0:currentNode = Q.Dequeue()- Read/Process
currentNode. - Enqueue
currentNode.Left(if not null). - Enqueue
currentNode.Right(if not null).
Depth-First Traversal (DFS):
- Explores as far as possible along each branch before backtracking.
- Consists of three main types based on when the current node is processed:
- PreOrder Traversal (Root, Left, Right):
- Sequence: HDBACGFELJIK.
- Recursive Logic:
- Read
CurrentNode. PreOrder-Traversal(CurrentNode.Left).PreOrder-Traversal(CurrentNode.Right).
- Read
- Iterative Logic (Stack-based):
- Create a new Stack
S. - Push root to
S. - While
S.Count > 0:currentNode = S.pop()- Read
currentNode. - Push
currentNode.Right(so it is processed later). - Push
currentNode.Left(so it is processed next).
- Create a new Stack
- InOrder Traversal (Left, Root, Right):
- Sequence: A, B, C, D, E, F, G, H, I, J, K, L.
- Recursive Logic:
InOrder-Traversal(CurrentNode.Left).- Read
CurrentNode. InOrder-Traversal(CurrentNode.Right).
- PostOrder Traversal (Left, Right, Root):
- Sequence: ACBEFGDIKJLH.
- Recursive Logic:
PostOrder-Traversal(CurrentNode.Left).PostOrder-Traversal(CurrentNode.Right).- Read
CurrentNode.
Binary Search Trees (BST)
- Fundamental Property: For every node, values in the left subtree are smaller than the node's value, and values in the right subtree are larger than the node's value.
- Search/Insert Logic:
- Start at the ROOT.
- If the target value is less than
CurrNode.Value, move toCurrNode.Left. - If the target value is greater than
CurrNode.Value, move toCurrNode.Right. - Repeat until the node is found or a null link is reached (where the new node should be inserted).
- Insertion Example: Inserting values 50, 20, 10, 30, 40, 70, 60, 80 results in a structured BST where the root is 50, its left child is 20, and its right child is 70.
- Traversal Result: Performing an InOrder Traversal on a Binary Search Tree always yields the values in sorted ascending order.
Binary Search Tree Deletion Scenarios
Deletion from a BST involves different handling based on the number of children the node-to-delete has.
Scenario 1: Deleting a Leaf Node (Zero Children):
- Simply nullify the reference from the parent to the node being deleted.
- Examples given: Deleting values 30, 24 (left of parent) or 11, 16 (right of parent).
Scenario 2: Deleting a Node with One Child:
- The child of the node-to-delete takes the position of the deleted node in the parent's structure.
- Logic for Node with Left Child Only:
- If
Node.Left != NULLandNode.Right == NULL:- If
Nodeis its parent's left child,Node.Prev.Left = Node.Left. - If
Nodeis its parent's right child,Node.Prev.Right = Node.Left. - Update the child's parent reference:
Node.Left.Prev = Node.Prev.
- If
- Logic for Node with Right Child Only:
- If
Node.Left == NULLandNode.Right != NULL:- Update parent's reference to point to
Node.Right. - Update
Node.Right.Prev = Node.Prev.
- Update parent's reference to point to
Scenario 3: Deleting a Node with Two Children:
- Requires finding a replacement node to maintain BST properties. There are two options:
- Using the Predecessor:
- The Predecessor is the node with the largest value in the left subtree.
- Finding Predecessor: Move to
Left, then keep movingRightuntil the right reference is null. - Deletion Process:
- Assign the value of the predecessor to the node being "deleted."
- Delete the predecessor node (which is either a leaf or has one left child).
- Using the Successor:
- The Successor is the node with the smallest value in the right subtree.
- Finding Successor: Move to
Right, then keep movingLeftuntil the left reference is null. - Deletion Process:
- Assign the value of the successor to the target node.
- Recursively delete the successor node (which will have at most one right child).
Performance and Balanced Trees
- Balanced Binary Search Tree:
- A tree where the height is kept minimal.
- Complexity:
- Search, Insertion, and Deletion: or .
- represents the total number of nodes.
- represents the height of the tree.
- Maintaining a balanced tree is critical; if a tree becomes skewed (like a linked list), the complexity degrades from logarithmic to linear ().