Lecture 7 - Tree & BST PDF
Document Details
Uploaded by Deleted User
Dr. Mahmoud Alnamoly
Tags
Summary
This document is a lecture on trees and binary search trees, covering various aspects such as definition, different tree types (general tree, binary tree, full binary tree, complete binary tree, perfect binary tree), terminologies (nodes, root, edge, path, subtree, level, depth, inorder, preorder, postorder, height, degree) as well as operations (traversal, insertion, searching, deletion), and advantages and disadvantages of using BST over other data structures.
Full Transcript
07 Tree Dr. Mahmoud Alnamoly Tree ? A tree is a nonlinear hierarchical data structure that consists of nodes connected by edges. Dr. Mahmoud Alnamoly Types of Tree Data Structure Dr. Mahmoud Alnamoly ✓General Tree ✓Binary Tree ✓Binary Search Tree (...
07 Tree Dr. Mahmoud Alnamoly Tree ? A tree is a nonlinear hierarchical data structure that consists of nodes connected by edges. Dr. Mahmoud Alnamoly Types of Tree Data Structure Dr. Mahmoud Alnamoly ✓General Tree ✓Binary Tree ✓Binary Search Tree (BST) ✓AVL Tree ✓Red-Black Tree ✓Heap ✓Prefix Tree ✓B-Tree ✓Segment Tree ✓Suffix Tree ✓…… Dr. Mahmoud Alnamoly General Tree Dr. Mahmoud Alnamoly General trees are tree data structures where the root node has minimum 0 or maximum ‘n’ subtrees. The General trees have no constraint placed on their hierarchy. General trees which have 3 sub-trees per node are called ternary trees. Dr. Mahmoud Alnamoly Binary Tree Dr. Mahmoud Alnamoly Binary Tree: Definition Binary Tree is a non-linear and hierarchical data structure (general tree), where each node has at most two children referred to as the left child and the right child. The topmost node in a binary tree is called the root, and the bottom-most nodes are called leaves. Dr. Mahmoud Alnamoly Dr. Mahmoud Alnamoly Binary Tree: Variations Based on the number of children, binary trees are divided into three types: Full Binary Tree A full binary tree is a binary tree type where every node has either 0 or 2 child nodes. Dr. Mahmoud Alnamoly Dr. Mahmoud Alnamoly Binary Tree: Variations Based on the number of children, binary trees are divided into three types: Complete binary tree is a binary tree that satisfies two properties. - First, every level, except possibly the last, is completely filled. However, root and internal nodes in a complete binary tree can either have 0, 1 or 2 child nodes. - Second, all nodes appear as far left as possible. Dr. Mahmoud Alnamoly Dr. Mahmoud Alnamoly Binary Tree: Variations Based on the number of children, binary trees are divided into three types: Perfect Binary Tree A perfect binary tree is a binary tree type where all the leaf nodes are on the same level and every node except leaf nodes have 2 children. Dr. Mahmoud Alnamoly Note:- A Perfect Binary Tree whose rightmost leaves (perhaps all) on the last level have been removed is called Complete Binary Dr. MahmoudTree. Alnamoly Dr. Mahmoud Alnamoly Binary Tree: Terminologies Levels= 4 Height of tree= 4+1 = 5 Depth of tree= 4 Dr. Mahmoud Alnamoly Binary Tree: Terminologies ✓ Nodes: The fundamental part of a binary tree, where each node contains data and link to two child nodes. ✓ Root: The topmost node in a tree is known as the root node. It has no parent and serves as the starting point for all nodes in the tree. ✓ Parent Node: A node that has one or more child nodes. In a binary tree, each node can have at most two children. ✓ Child Node: A node that is a descendant of another node (its parent). ✓ External nodes (terminal nodes- Leaf Node): A node that does not have any children or both children are null. ✓ Internal Node: A node that has at least one child. This includes Dr.all nodes Mahmoud except the root and the leaf nodes. Alnamoly Binary Tree: Terminologies ✓ Edge: A connection between two nodes. It represents the relationship between a parent and a child node. ✓ Path: A sequence of consecutive edges. ✓ Subtree: The Subtree is a portion of the tree that includes a node and all its descendants ✓ Level: The level of a node is defined by its depth. The root node is at level 0, its children are at level 1, and so on. The maximum number of nodes at any level L is 2^L. ✓ Depth of a Node: The number of edges (length of the path) from a specific node (N) to the root node (R). The depth of the root node is zero. The maximum number of nodes in a binary tree of depth d is (2^d)-1, where d ≥1. ✓ Height of a Binary Tree: The total number of nodes on the path from the root node to the deepest leaf node in the tree. A tree Dr.with only Mahmoud a root node has a height of 1. Alnamoly Binary Tree: Terminologies Degree of a node:- It is equal to the number of children that a node has. The degree of a leaf node is zero. For example, in the tree, degree of node 4 is 2, degree of node 5 is zero and degree of node 7 is 1. In-degree/out-degree of a node:- In-degree is the number of edges arriving at a node. The root node is the only node that has an in-degree equal to zero. Similarly, out-degree of a node is the number of edges leaving that Dr. node. Mahmoud Alnamoly Dr. Mahmoud Alnamoly The height of binary tree with N nodes is at least Log2(N+1) and at most N Example:- Nodes=5 Min_H= log(6)=2.58~=3 Max_H=N=5 Dr. Mahmoud Alnamoly The number of nodes on binary tree with height H is at least H and at most 2H – 1 Example:- Height=3 Min_Nodes= H =3 Max_Nodes=2H – 1=7 Dr. Mahmoud Alnamoly Binary Tree: Representation Each node in a Binary Tree has three parts: Data Pointer to the left child Pointer to the right child // Node Structure struct Node { int data; Node* left; Node* right; Node(int value) { data = value; left = right = nullptr; } }; Dr. Mahmoud Alnamoly Binary Tree: Representation Each node in a Binary Tree has three parts: Data Pointer to the left child Pointer to the right child // BinaryTree class class BinaryTree { private: Node* root; public: // Constructor BinaryTree() { root = nullptr; } Dr. Mahmoud Alnamoly Binary Tree: Operations ✓ 1. Traverse Traversal in Binary Tree involves visiting all the nodes of the binary tree. Tree Traversal algorithms can be classified broadly into two categories, BFS and DFS: Breadth-First Search (BFS) algorithms: BFS explores all nodes at the present depth before moving on to nodes at the next depth level. It is typically implemented using a queue. BFS in a binary tree is commonly referred to as Level Order Traversal. Dr. Mahmoud Alnamoly Binary Tree: Operations ✓ 1. Traverse Traversal in Binary Tree involves visiting all the nodes of the binary tree. Tree Traversal algorithms can be classified broadly into two categories, DFS and BFS: Depth-First Search (DFS) algorithms: DFS explores as far down a branch as possible before backtracking. It is implemented using recursion. The main traversal methods in DFS for binary trees are: Preorder-Inorder-Postorder Dr. Mahmoud Alnamoly ✓ Traversal: Depth First Search Algorithm (Pre-order) // Helper function for preorder traversal void preorder(Node* node) { if (node == nullptr) { return; } cout data left); preorder(node->right); } // Preorder traversal of the tree void preorder() { preorder(root); cout left == nullptr) { temp->left = new Node(key); break; } else { q.push(temp->left); } // If right child is empty, insert the new node here if (temp->right == nullptr) { temp->right = new Node(key); break; } else { q.push(temp->right); } } return root; Dr. } Mahmoud Alnamoly Binary Tree: Operations ✓ 3. Searching We typically use any traversal method to search. The most common methods are depth-first search (DFS) and breadth-first search (BFS). In DFS, we start from the root and explore the depth nodes first. In BFS, we explore all the nodes at the present depth level before moving on to the nodes at the next level. We continue this process until we either find the node with the desired value or reach the end of the tree. If the tree is empty or the value isn’t found after exploring all possibilities, we conclude that the value does not exist in the Dr. tree. Alnamoly Mahmoud // Function to search for a value in the binary tree using DFS bool searchDFS(Node* root, int value) { // Base case: If the tree is empty or we've reached a leaf node if (root == nullptr) return false; // If the node's data is equal to the value we are searching for if (root->data == value) return true; // Recursively search in the left and right subtrees bool left_res = searchDFS(root->left, value); bool right_res = searchDFS(root->right, value); return left_res || right_res; } int value = 6; if (searchDFS(root, value)) cout left = new Node(30); // Base Cases: root is null or key root->right = new Node(70); // is present at root root->left->left = new Node(20); if (root == NULL || root->key == key) root->left->right = new Node(40); return root; root->right->left = new Node(60); root->right->right = new Node(80); // Key is greater than root's key if (root->key < key) (search(root, 19) != NULL) ? cout right, key); "Found\n" : cout