Lecture 13 Trees
Lecture 13 Trees
01 02 03 04 05
Not to be A tree with each Binary can have It may not be Deepest nodes
confused with node having 2 random balanced are leaf nodes
BST(Binary children unsorted data • Unbalanced trees are
Search Tree) those that have
uneven height/depth
in left and right of root
node.
• Can have duplicate
values
Binary Tree
can have
duplicate
values and
even duplicate
subtrees
Binary Trees - Exercise
• Write code for finding duplicate
keys in binary tree
• Efficient Way
• Inefficient way
Binary Search
Tree
Binary Search Trees
• Type of data structure that is
sorted by nature
• Follows all rules of its
predecessor Binary Tree
• Also, Children left to node are
always smaller
• Children right to node are
always greater than the node
• Follows same for inner subtrees
class Node { // If the key is already present in the tree,
int key; // return the node
Node left, right; if (root.key == key)
return root;
public Node(int item)
{ // Otherwise, recur down the tree
key = item; if (key < root.key)
left = right = null; root.left = insert(root.left, key);
} else
root.right = insert(root.right, key);
}
// Return the (unchanged) node
class GfG { pointer
return root;
// A utility function to insert a new }
node
// with the given key // A utility function to do inorder tree
static Node insert(Node root, int key) traversal
{ static void inorder(Node root)
// If the tree is empty, return a new {
node if (root != null) {
inorder(root.left);
if (root == null) System.out.print(root.key + " ");
return new Node(key); inorder(root.right);
Properties of Binary Search
Tree:
• The left subtree of a node contains only nodes with keys less than the
node’s key.
• The right subtree of a node contains only nodes with keys greater than
the node’s key.
• The left and right subtree each must also be a binary search tree.
• There must be no duplicate nodes (BST may have duplicate values with
different handling approaches).
Important Points about BST
• A Binary Search Tree is useful for maintaining sorted stream of data. It allows search,
insert, delete, ceiling, max and min in O(h) time. Along with these, we can always
traverse the tree items in sorted order.
• With Self Balancing BSTs, we can ensure that the height of the BST is bound be Log n.
above-mentioned we achieve, the above-mentioned O(h) operations in O(Log n) time.
• When we need only search, insert and delete and do not need other operations, we
prefer Hash Table over BST as a Hash Table supports these operations in O(1) time on
average.
Applications of BST
• A Self-Balancing Binary Search Tree is used to maintain sorted stream of data.
• There are many more algorithm problems where a Self-Balancing BST is the best
suited data structure, like count smaller elements on right, Smallest Greater
Element on Right Side, etc.
• Variations of BST like B Tree and B+ Tree are used in Database indexing.
• TreeMap and TreeSet in Java and set and map in C++ are internally implemented
using self-balancing BSTs, more formally a Red-Black Tree.
Advantages of Binary Search
Tree (BST):o
• Efficient searching: O(log n) time complexity for searching with a self
balancing BST
• Doubly Ended Priority Queue: In BSTs, we can maintain both maximum and
minimum efficiently
Disadvantages of Binary
Search Tree (BST)
• Not self-balancing: Unbalanced BSTs can lead to poor performance
• Not suitable for large datasets: BSTs can become inefficient for very
large datasets