Loading...

DESIGN & ANALYSIS OF ALGORITHMS  

>

LEARNING OUTCOME 7

Tree Data Structure

A tree is a non-linear data structure that consists of nodes connected by edges. It resembles an inverted tree, with a root node at the top and branches extending downwards. Each node can have zero or more child nodes.

Properties of a Tree Data Structure:

Different Types of Trees

Here are some common types of trees in computer science:

General Trees

Binary Trees

Special Trees

Binary Tree Operations

A binary tree is a fundamental data structure in computer science. It consists of nodes, where each node has at most two children: a left child and a right child.

What is a Binary Tree?

A binary tree is a hierarchical data structure where each node has at most two children: a left child and a right child. The topmost node is called the root. Nodes without children are called leaves.

Key Concepts

Binary Tree Operations

Here's a detailed explanation of common binary tree operations:

1. Insertion

The goal is to add a new node containing a specific value while maintaining the binary tree's structure (often a Binary Search Tree, where left child <= parent < right child).

  1. Find the Insertion Point:
    • Start at the root.
    • If the new value is less than the current node's value, move to the left child.
    • If the new value is greater than or equal to the current node's value, move to the right child.
    • Repeat until you reach a nullptr (an empty spot). This is where the new node will be inserted.
  2. Create the New Node:
    • Allocate memory for a new node.
    • Set the new node's data to the value being inserted.
    • Initialize the new node's left and right pointers to nullptr.
  3. Link the New Node:
    • If the new value is less than the parent's value, make the new node the parent's left child.
    • Otherwise, make the new node the parent's right child.

2. Deletion

Deleting a node involves several cases to maintain the tree's structure:

  1. Case 1: Node to be deleted is a leaf (no children):
    • Simply remove the node by setting the parent's corresponding child pointer (left or right) to nullptr.
  2. Case 2: Node to be deleted has one child:
    • Replace the node with its child. Update the parent's pointer to point to the node's child.
  3. Case 3: Node to be deleted has two children:
    • Find the inorder successor (smallest node in the right subtree) or the inorder predecessor (largest node in the left subtree).
    • Copy the value of the inorder successor/predecessor to the node being deleted.
    • Delete the inorder successor/predecessor (which will now have at most one child, so it falls into Case 1 or 2).

3. Searching

Searching for a specific value in a binary search tree is efficient:

  1. Start at the root.
  2. Compare the target value with the current node's value.
  3. If they are equal, the value is found.
  4. If the target value is less than the current node's value, move to the left child.
  5. If the target value is greater than the current node's value, move to the right child.
  6. Repeat until the value is found or you reach a nullptr (value not in the tree).

4. Tree Traversals

Tree traversals are systematic ways to visit every node in the tree exactly once.

  1. a. Inorder Traversal (Left, Root, Right): Often used for binary search trees because it visits nodes in sorted order.
    • Traverse the left subtree (recursively).
    • Visit the current node (process its data).
    • Traverse the right subtree (recursively).
  2. b. Preorder Traversal (Root, Left, Right):
    • Visit the current node (process its data).
    • Traverse the left subtree (recursively).
    • Traverse the right subtree (recursively).
  3. c. Postorder Traversal (Left, Right, Root):
    • Traverse the left subtree (recursively).
    • Traverse the right subtree (recursively).
    • Visit the current node (process its data).

Example (Conceptual C++ Code Snippet - Insertion)

		
		struct Node {
			int data;
			Node* left;
			Node* right;
		};
		
		Node* insert(Node* root, int value) {
			if (root == nullptr) {
				Node* newNode = new Node;
				newNode->data = value;
				newNode->left = newNode->right = nullptr;
				return newNode;
			}
		
			if (value < root->data) {
				root->left = insert(root->left, value);
			} else {
				root->right = insert(root->right, value);
			}
			return root;
		}
		
		
>

Binary Tree Implementation in C++(Dont stress yourself youcan skip if you want to keep learning simple)

					
		#include <iostream>
		#include <limits>
		
		struct Node {
			int data;
			Node* left;
			Node* right;
		
			Node(int val) : data(val), left(nullptr), right(nullptr) {}
		};
		
		class BinaryTree {
		private:
			Node* root;
		
			Node* findInorderSuccessor(Node* node) {
				while (node->left != nullptr) {
					node = node->left;
				}
				return node;
			}
		
			Node* deleteNodeRecursive(Node* root, int key) {
				if (root == nullptr) return root;
		
				if (key < root->data)
					root->left = deleteNodeRecursive(root->left, key);
				else if (key > root->data)
					root->right = deleteNodeRecursive(root->right, key);
				else {
					if (root->left == nullptr) {
						Node* temp = root->right;
						delete root;
						return temp;
					} else if (root->right == nullptr) {
						Node* temp = root->left;
						delete root;
						return temp;
					}
		
					Node* temp = findInorderSuccessor(root->right);
					root->data = temp->data;
					root->right = deleteNodeRecursive(root->right, temp->data);
				}
				return root;
			}
		
		public:
			BinaryTree() : root(nullptr) {}
		
			Node* insert(Node* root, int value) {
				if (root == nullptr) {
					return new Node(value);
				}
		
				if (value < root->data) {
					root->left = insert(root->left, value);
				} else {
					root->right = insert(root->right, value);
				}
				return root;
			}
		
			void insert(int value) {
				root = insert(root, value);
			}
		
			void deleteNode(int key) {
				root = deleteNodeRecursive(root, key);
			}
		
			bool search(Node* root, int key) {
				if (root == nullptr) return false;
				if (root->data == key) return true;
		
				if (key < root->data) {
					return search(root->left, key);
				} else {
					return search(root->right, key);
				}
			}
		
			bool search(int key) {
				return search(root, key);
			}
		
			void inorder(Node* root) {
				if (root != nullptr) {
					inorder(root->left);
					std::cout << root->data << " ";
					inorder(root->right);
				}
			}
		
			void inorder() {
				inorder(root);
				std::cout << std::endl;
			}
		
			void preorder(Node* root) {
				if (root != nullptr) {
					std::cout << root->data << " ";
					preorder(root->left);
					preorder(root->right);
				}
			}
		
			void preorder() {
				preorder(root);
				std::cout << std::endl;
			}
		
			void postorder(Node* root) {
				if (root != nullptr) {
					postorder(root->left);
					postorder(root->right);
					std::cout << root->data << " ";
				}
			}
		
			void postorder() {
				postorder(root);
				std::cout << std::endl;
			}
		};
		
		int main() {
			BinaryTree tree;
		
			tree.insert(50);
			tree.insert(30);
			tree.insert(20);
			tree.insert(40);
			tree.insert(70);
			tree.insert(60);
			tree.insert(80);
		
			std::cout << "Inorder traversal: ";
			tree.inorder();
			std::cout << "Preorder traversal: ";
			tree.preorder();
			std::cout << "Postorder traversal: ";
			tree.postorder();
		
			std::cout << "Search for 40: " << (tree.search(40) ? "Found" : "Not Found") << std::endl;
			std::cout << "Search for 99: " << (tree.search(99) ? "Found" : "Not Found") << std::endl;
		
			tree.deleteNode(30);
			std::cout << "Inorder traversal after deleting 30: ";
			tree.inorder();
		
			return 0;
		}
					
				

Binary Search Tree (BST)

Definition

A Binary Search Tree (BST) is a specific type of binary tree where:

This property allows for efficient searching, insertion, and deletion of elements.

Applications of Binary Search Trees

End of Outcome Quiz

1 of 20

    Quiz Score

    Percentage: 0%

    Answered Questions: 0

    Correct Answers: 0

    Faults: