Chapter 12 – Binary Trees and Binary Search Trees (BST)
Chapter 12: Binary Trees and Binary Search Trees (BST) Welcome to the fascinating world of Trees! Trees are one of the most fundamental data structures in computer science, and they are widely used in various applications like file systems, databases, and more. This chapter focuses on two essential types of trees: Binary Trees and Binary Search Trees (BSTs). What is a Tree? A Tree is a non-linear data structure composed of nodes. It is a hierarchical structure with a root node and sub-nodes, each connected by edges. Unlike arrays, linked lists, stacks, or queues, trees are nonlinear and allow for more complex relationships between elements. A tree follows these rules: Each node contains a value or data. The root node is the topmost node in the tree. Each node has zero or more child nodes. Each node can only have one parent (except for the root, which has no parent). What is a Binary Tree? A Binary Tree is a type of tree where each node can have at most two children, often referred to as the left child and right child. Characteristics of a Binary Tree: Node: Contains data, and two pointers (left and right). Edge: The connection between two nodes. Root: The topmost node in the tree. Leaf Node: A node with no children. Height: The number of edges from the root to the deepest leaf. Structure of a Binary Tree Here’s a simple binary tree: 10 / \ 5 15 / \ / \ 3 7 12 20 In this example: 10 is the root. 5 and 15 are children of 10. 3, 7, 12, and 20 are leaf nodes. Types of Binary Trees Full Binary Tree: Every node has either 0 or 2 children. Complete Binary Tree: All levels are completely filled, except possibly the last level, which is filled from left to right. Perfect Binary Tree: All internal nodes have two children, and all leaves are at the same level. Balanced Binary Tree: The height of the tree is balanced, meaning the difference between the height of the left and right subtrees for any node is at most 1. What is a Binary Search Tree (BST)? A Binary Search Tree (BST) is a special kind of binary tree where the nodes are arranged in a specific order. In a BST: The left subtree of a node contains only nodes with values less than the node’s value. The right subtree contains only nodes with values greater than the node’s value. Both the left and right subtrees must also be binary search trees. This property makes BSTs highly efficient for searching, insertion, and deletion operations. Example of a Binary Search Tree Consider the following tree: 15 / \ 10 25 / \ / \ 5 12 20 30 In this BST: Nodes to the left of 15 are smaller than 15 (i.e., 10, 5, 12). Nodes to the right of 15 are greater than 15 (i.e., 25, 20, 30). This ordering ensures efficient search operations. Operations on Binary Search Trees 1. Insertion To insert a new element into a BST, we start at the root and compare the new element with the current node: If it is smaller, we move to the left subtree. If it is greater, we move to the right subtree. We continue this until we find an empty spot, where we insert the new node. Example: Inserting 8 into the following BST: 10 / \ 5 15 / \ 3 7 Since 8 is greater than 5 and less than 10, we move to the right child of 5 and place it there: 10 / \ 5 15 / \ 3 7 \ 8 2. Deletion To delete a node from a BST, we must consider three cases: Case 1: The node to be deleted is a leaf (has no children). Case 2: The node to be deleted has one child. Case 3: The node to be deleted has two children. Example: Let’s delete the node 7 from the tree above. Since it has one child (8), we simply replace 7 with 8. 10 / \ 5 15 / \ 3 8 3. Search To search for a value in a BST: Start at the root. If the value is smaller than the current node, move left; if it’s greater, move right. Repeat this process until you find the value or reach a NULL node (the value does not exist in the tree). Applications of Binary Search Trees Search Operations: Due to the structured order, searching for an element in a BST is more efficient than in unsorted arrays or linked lists. In-memory databases: BSTs are commonly used in database indexing where efficient searching is crucial. Network Routing: BSTs help in storing hierarchical data, which can be efficiently searched, like network routes. Dynamic Data Structures: In scenarios like building self-balancing trees in applications, BSTs form the backbone. Systems like Emancipation Edutech Private Limited often use Binary Search Trees to manage student records efficiently by indexing them according to registration numbers. For free educational resources, visit digilearn.cloud, where you can access this book and much more! Binary Tree Traversal Methods To interact with the data in a binary tree, we need to traverse it. There are three primary traversal techniques: 1. In-order Traversal (Left, Root, Right) This traversal visits nodes in ascending order in a BST. Visit Left Subtree -> Visit Root -> Visit Right Subtree Example: For the tree: 10 / \ 5 15 / \ 3 7 The in-order traversal would be: 3 -> 5 -> 7 -> 10 -> 15. 2. Pre-order Traversal (Root, Left, Right) This traversal visits the root node first, followed by the left subtree, and then the right subtree. Visit Root -> Visit Left Subtree -> Visit Right Subtree Example: For the same tree: 10 / \ 5 15 / \ 3 7 The pre-order traversal would be: 10 -> 5 -> 3 -> 7 -> 15. 3. Post-order Traversal (Left, Right, Root) This traversal visits
Chapter 12 – Binary Trees and Binary Search Trees (BST) Read More »