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.
Here are some common types of trees in computer science:
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.
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.
Here's a detailed explanation of common binary tree operations:
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).
Deleting a node involves several cases to maintain the tree's structure:
Searching for a specific value in a binary search tree is efficient:
Tree traversals are systematic ways to visit every node in the tree exactly once.
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;
}
#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;
}
A Binary Search Tree (BST) is a specific type of binary tree where:
This property allows for efficient searching, insertion, and deletion of elements.
Percentage: 0%
Answered Questions: 0
Correct Answers: 0