1. Binary Tree
(1) Definition
A binary tree is a tree data structure in which each parent node can have at most two children. Each node of a binary tree consists of 3 item: 1> a data children; 2> address of left child 3> address of right child
(2) Types
- Binary Tree
- Full Binary Tree
- Perfect Binary Tree
- Complete Binary Tree
- Balanced Binary Tree
- AVL Tree
(3) Binary Tree Representation
struct node{
int data;
struct node* left;
struct node* right;
node(int data) : data(data), left(nullptr), right(nullptr) {}
};
2. Binary Search Tree (BST)
Definition
Binary search tree is a data strcuture that quickly allows us to main a sorted list of numbers
- Each tree node has a maximum of two children
- It can be used to search for the presence of a number in O(log(n)) time.
Binary Tree Properties:
1. All nodes of left subtree are less than the root node
2. All nodes of right subtree are more than the root node
3. Both subtrees of each node are also BSTs i.e. they have the above two properties
Search Operation
The algorithm depends on the property of BST that if each left subtree has values below root and each right subtree has values above the root.
//Algorithm
if root == NULL
return NULL;
if number == root->data
return root->data;
if number < root->data
return search(root->left);
if number > root->data
return search(root->right);
Insert Operation
Inserting a value in the correct position is similar to searching because we try to maintain the rule that the left subtree is lesser than root and the right subtree is larger than root.
//Algorithm
if node == NULL
return createNode(data);
if (data < node->data)
node->left = insert(node->left, data)
else if(data > node->data)
node->right = insert(node->right, data);
return node;
Deletion Operation (3 cases)
3. Java Code Examples
// Binary Search Tree operations in Java
class BinarySearchTree {
class Node {
int key;
Node left, right;
public Node(int item) {
key = item;
left = right = null;
}
}
Node root;
BinarySearchTree() {
root = null;
}
void insert(int key) {
root = insertKey(root, key);
}
// Insert key in the tree
Node insertKey(Node root, int key) {
// Return a new node if the tree is empty
if (root == null) {
root = new Node(key);
return root;
}
// Traverse to the right place and insert the node
if (key < root.key)
root.left = insertKey(root.left, key);
else if (key > root.key)
root.right = insertKey(root.right, key);
return root;
}
void inorder() {
inorderRec(root);
}
// Inorder Traversal
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.print(root.key + " -> ");
inorderRec(root.right);
}
}
void deleteKey(int key) {
root = deleteRec(root, key);
}
Node deleteRec(Node root, int key) {
// Return if the tree is empty
if (root == null)
return root;
// Find the node to be deleted
if (key < root.key)
root.left = deleteRec(root.left, key);
else if (key > root.key)
root.right = deleteRec(root.right, key);
else {
// If the node is with only one child or no child
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
// If the node has two children
// Place the inorder successor in position of the node to be deleted
root.key = minValue(root.right);
// Delete the inorder successor
root.right = deleteRec(root.right, root.key);
}
return root;
}
// Find the inorder successor
int minValue(Node root) {
int minv = root.key;
while (root.left != null) {
minv = root.left.key;
root = root.left;
}
return minv;
}
// Driver Program to test above functions
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
tree.insert(8);
tree.insert(3);
tree.insert(1);
tree.insert(6);
tree.insert(7);
tree.insert(10);
tree.insert(14);
tree.insert(4);
System.out.print("Inorder traversal: ");
tree.inorder();
System.out.println("\n\nAfter deleting 10");
tree.deleteKey(10);
System.out.print("Inorder traversal: ");
tree.inorder();
}
}
4. 待补充:Visualization
5. 待补充:Binary Search Tree Complexities
6. 待补充:Binary Search Tree Application