0% found this document useful (0 votes)
2 views

Lecture 13 Trees

The document discusses trees as a data structure, focusing on binary trees and binary search trees (BST). It outlines the properties, advantages, and disadvantages of BSTs, including their applications in maintaining sorted data and efficient operations. Additionally, it provides code examples for inserting nodes and performing in-order traversal in a binary tree.

Uploaded by

saifullahmr46
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 13 Trees

The document discusses trees as a data structure, focusing on binary trees and binary search trees (BST). It outlines the properties, advantages, and disadvantages of BSTs, including their applications in maintaining sorted data and efficient operations. Additionally, it provides code examples for inserting nodes and performing in-order traversal in a binary tree.

Uploaded by

saifullahmr46
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Trees

Ali Mobin Memon


Trees means a
root with
branches
Tree is one of
the effective
ways of
structuring
data
Binary Tree

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

• Find Smallest value 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.

• A Self-Balancing Binary Search Tree is used to implement doubly ended priority


queue

• 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.

• A BST can be used to sort a large dataset

• 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

• Ordered structure: Elements are stored in sorted order, making it easy to


find the next or previous element

• Dynamic insertion and deletion: Elements can be added or removed


efficiently

• Balanced structure: Balanced BSTs maintain a logarithmic height, ensuring


efficient operations

• 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

• Worst-case time complexity: In the worst case, BSTs can have a


linear time complexity for searching and insertion

• Memory overhead: BSTs require additional memory to store pointers


to child nodes

• Not suitable for large datasets: BSTs can become inefficient for very
large datasets

• Limited functionality: BSTs only support searching, insertion, and


deletion operations
Thank you
Next week Test – 1 Retake

You might also like