BINARY SEARCH TREES
Technical Writing (CS 1305) Assignment
Anya Chaturvedi
20146005
August 31, 2015 1 / 29
What is a Binary Search Tree?
PROPERTY 1:
Each node can have upto two successor nodes.
August 31, 2015 2 / 29
What is a Binary Search Tree?
PROPERTY 2:
A unique path exists from the root to every node.
This is not valid binary tree!
August 31, 2015 3 / 29
Some Basic Information
The successor nodes of a node are called its children.
The predecessor node of a node is called its parent.
The first node is called the root.
A node without a child is called a leaf.
Nodes with the same parent are called siblings.
August 31, 2015 4 / 29
Basic Information(Cont.)
Nodes are organized in levels.indexed from 0.
Level(or depth) of a node is the number of edges in the path
from the root to that node.
Height of a tree (h):No. of levels (L)
Full tree:Every node has exactly two children and all the leaves
are on the same level.
Complete tree:A complete binary tree is a binary tree in which
every level, except possibly the last, is completely filled, and all
nodes are as far left as possible.
Full Tree – Complete Tree
August 31, 2015 5 / 29
Binary Search Tree Property
-
A binary search tree is a rooted binary tree, whose internal nodes
each store a key (and optionally, an associated value) and each have
two distinguished sub-trees, commonly denoted left and right. The
tree additionally satisfies thebinary search tree property, which
states that the key in each node must be greater than all keys
stored in the left sub-tree, and smaller than all keys in right
sub-tree
August 31, 2015 6 / 29
Max No.of Nodes
Max no. of nodes at a level l is 2l
.
Total no. of nodes N of a full tree with height h = 2h
− 1.
August 31, 2015 7 / 29
Height h AND It’s Importance
Since, N = 2h
− 1
=> 2h
= N + 1
=> h = log(N + 1)
Hence, h -> O(log(N))
Tree operations (e.g., insert, delete, retrieve etc.)are typically
expressed in terms of h.
So, h determines running time!
August 31, 2015 8 / 29
Search In a Binary Tree
Start at the root
Search the tree level by level, until you find the element you are
searching for or you reach a leaf.
Is this better than searching a linked list?
NO! -> O(N)
August 31, 2015 9 / 29
Search In A Binary Search Tree
1 Start at the root
2 Compare the value of the item you are searching for with the
value stored at the root
3 If the values are equal, then item found; otherwise, if it is a leaf
node, then not found
4 If it is less than the value stored at the root, then search the left
subtree
5 If it is greater than the value stored at the root, then search the
right subtree
6 Repeat steps 2-6 for the root of the subtree chosen in the
previous step 4 or 5.
August 31, 2015 10 / 29
Search in BST(Cont.)
Is this better than searching a linked list?
YES-> O(log(N))
August 31, 2015 11 / 29
Function for No. of Nodes
Recursive implementation
No. of nodes in a tree
= No. of nodes in left subtree + No. of nodes in right
subtree+1
What is the size factor?
Number of nodes in the tree we are examining
What is the base case?
The tree is empty
What is the general case?
CountNodes(Left(tree)) + CountNodes(Right(tree)) +1
August 31, 2015 12 / 29
Function RetrieveItem()
What is the size of the problem?
Number of nodes in the tree we are examining
What is the base case(s)?
1 When the key is found
2 The tree is empty (key was not found)
What is the general case?
Search in the left or right subtrees
August 31, 2015 13 / 29
RetrieveItem() (Cont.)
O(h)
August 31, 2015 14 / 29
Function InsertItem()
What is the size of the problem?
Number of nodes in the tree we are examining
What is the base case(s)?
The tree is empty
What is the general case?
Choose the left or right subtree
August 31, 2015 15 / 29
InsertItem() (Cont.)
Inserting 11 in the given BST
O(h)
August 31, 2015 16 / 29
Function DeleteItem()
First, find the item.
Then, delete it.
Binary search tree property must be preserved!!
We need to consider three different cases
1 Deleting a leaf
2 Deleting a node with only one child
3 Deleting a node with two children
August 31, 2015 17 / 29
Deleting a Leaf
August 31, 2015 18 / 29
Deleting a node with only one child
August 31, 2015 19 / 29
Deleting a node with two children
August 31, 2015 20 / 29
Deleting a node with two children(Cont.)
Find predecessor (i.e., rightmost node in the left subtree)
Replace the data of the node to be deleted with predecessor’s
data
Delete predecessor node
August 31, 2015 21 / 29
Function DeleteItem()
What is the size of the problem?
Number of nodes in the tree we are examining
What is the base case(s)?
Key to be deleted was found
What is the general case?
Choose the left or right subtree
August 31, 2015 22 / 29
Traversals
There are mainly three ways to traverse a tree:
1 Inorder Traversal
2 Postorder Traversal
3 Preorder Traversal
August 31, 2015 23 / 29
Traversal Example
August 31, 2015 24 / 29
Print the Tree
For printing the Binary Search Tree in sorted order we can use
Inorder Traversal.
Result: A D J M Q R T
August 31, 2015 25 / 29
Use Of A Binary Search Tree
In comparison to other trees using a Binary Search Tree we can
efficiently perform the following:
Search for a particular element.
Sorting of elements.
Find Maximum/Minimum.
Delete a particular element.
August 31, 2015 26 / 29
REFRENCES
Images:
www.google.com
Text:
www.wikipedia.com
www.google.com
tex.stackexchange.com
August 31, 2015 27 / 29
August 31, 2015 28 / 29
August 31, 2015 29 / 29

Binary Search Tree

  • 1.
    BINARY SEARCH TREES TechnicalWriting (CS 1305) Assignment Anya Chaturvedi 20146005 August 31, 2015 1 / 29
  • 2.
    What is aBinary Search Tree? PROPERTY 1: Each node can have upto two successor nodes. August 31, 2015 2 / 29
  • 3.
    What is aBinary Search Tree? PROPERTY 2: A unique path exists from the root to every node. This is not valid binary tree! August 31, 2015 3 / 29
  • 4.
    Some Basic Information Thesuccessor nodes of a node are called its children. The predecessor node of a node is called its parent. The first node is called the root. A node without a child is called a leaf. Nodes with the same parent are called siblings. August 31, 2015 4 / 29
  • 5.
    Basic Information(Cont.) Nodes areorganized in levels.indexed from 0. Level(or depth) of a node is the number of edges in the path from the root to that node. Height of a tree (h):No. of levels (L) Full tree:Every node has exactly two children and all the leaves are on the same level. Complete tree:A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. Full Tree – Complete Tree August 31, 2015 5 / 29
  • 6.
    Binary Search TreeProperty - A binary search tree is a rooted binary tree, whose internal nodes each store a key (and optionally, an associated value) and each have two distinguished sub-trees, commonly denoted left and right. The tree additionally satisfies thebinary search tree property, which states that the key in each node must be greater than all keys stored in the left sub-tree, and smaller than all keys in right sub-tree August 31, 2015 6 / 29
  • 7.
    Max No.of Nodes Maxno. of nodes at a level l is 2l . Total no. of nodes N of a full tree with height h = 2h − 1. August 31, 2015 7 / 29
  • 8.
    Height h ANDIt’s Importance Since, N = 2h − 1 => 2h = N + 1 => h = log(N + 1) Hence, h -> O(log(N)) Tree operations (e.g., insert, delete, retrieve etc.)are typically expressed in terms of h. So, h determines running time! August 31, 2015 8 / 29
  • 9.
    Search In aBinary Tree Start at the root Search the tree level by level, until you find the element you are searching for or you reach a leaf. Is this better than searching a linked list? NO! -> O(N) August 31, 2015 9 / 29
  • 10.
    Search In ABinary Search Tree 1 Start at the root 2 Compare the value of the item you are searching for with the value stored at the root 3 If the values are equal, then item found; otherwise, if it is a leaf node, then not found 4 If it is less than the value stored at the root, then search the left subtree 5 If it is greater than the value stored at the root, then search the right subtree 6 Repeat steps 2-6 for the root of the subtree chosen in the previous step 4 or 5. August 31, 2015 10 / 29
  • 11.
    Search in BST(Cont.) Isthis better than searching a linked list? YES-> O(log(N)) August 31, 2015 11 / 29
  • 12.
    Function for No.of Nodes Recursive implementation No. of nodes in a tree = No. of nodes in left subtree + No. of nodes in right subtree+1 What is the size factor? Number of nodes in the tree we are examining What is the base case? The tree is empty What is the general case? CountNodes(Left(tree)) + CountNodes(Right(tree)) +1 August 31, 2015 12 / 29
  • 13.
    Function RetrieveItem() What isthe size of the problem? Number of nodes in the tree we are examining What is the base case(s)? 1 When the key is found 2 The tree is empty (key was not found) What is the general case? Search in the left or right subtrees August 31, 2015 13 / 29
  • 14.
  • 15.
    Function InsertItem() What isthe size of the problem? Number of nodes in the tree we are examining What is the base case(s)? The tree is empty What is the general case? Choose the left or right subtree August 31, 2015 15 / 29
  • 16.
    InsertItem() (Cont.) Inserting 11in the given BST O(h) August 31, 2015 16 / 29
  • 17.
    Function DeleteItem() First, findthe item. Then, delete it. Binary search tree property must be preserved!! We need to consider three different cases 1 Deleting a leaf 2 Deleting a node with only one child 3 Deleting a node with two children August 31, 2015 17 / 29
  • 18.
    Deleting a Leaf August31, 2015 18 / 29
  • 19.
    Deleting a nodewith only one child August 31, 2015 19 / 29
  • 20.
    Deleting a nodewith two children August 31, 2015 20 / 29
  • 21.
    Deleting a nodewith two children(Cont.) Find predecessor (i.e., rightmost node in the left subtree) Replace the data of the node to be deleted with predecessor’s data Delete predecessor node August 31, 2015 21 / 29
  • 22.
    Function DeleteItem() What isthe size of the problem? Number of nodes in the tree we are examining What is the base case(s)? Key to be deleted was found What is the general case? Choose the left or right subtree August 31, 2015 22 / 29
  • 23.
    Traversals There are mainlythree ways to traverse a tree: 1 Inorder Traversal 2 Postorder Traversal 3 Preorder Traversal August 31, 2015 23 / 29
  • 24.
  • 25.
    Print the Tree Forprinting the Binary Search Tree in sorted order we can use Inorder Traversal. Result: A D J M Q R T August 31, 2015 25 / 29
  • 26.
    Use Of ABinary Search Tree In comparison to other trees using a Binary Search Tree we can efficiently perform the following: Search for a particular element. Sorting of elements. Find Maximum/Minimum. Delete a particular element. August 31, 2015 26 / 29
  • 27.
  • 28.
  • 29.