Tree BinaryTree BST P1
Tree BinaryTree BST P1
leaves
Branches
root
Tree Data Structure
• Tree
• Collection of nodes linked to each other (similar to linked
lists)
• Each node can have 0 or more children (successor nodes)
• Each node (except the root) has exactly one parent
(predecessor node)
• This means that there is exactly one path to go from the root to
any other node
Root node
Interior nodes
Leaf nodes
Tree Data Structure
• Terminology
• Level number of ancestors (parent or parent’s
parent or …) up to the root (distance from the
root)
• Height number of levels
• Some books use height = highest level in the tree
Level 0
Height = 3 Level 1
Level 2
Binary Tree
• Characteristics
• Tree with 0 – 2 children per node
• No child
• One left child
• One right child
• One left child and one right child
Binary Tree
Binary Tree
• Characteristics
• Every node is the parent of two smaller trees
(subtree)
• Left subtree
• Right subtree
Left subtree
Right subtree
Binary Tree
Binary Tree
• Full tree
• A binary tree is full if each node is either a leaf or possesses exactly
two child nodes.
• Complete tree
• A binary tree is complete if all levels (except possibly the last) are
completely full, and the last level has all its nodes to the left side.
Binary Tree
• Full tree
• A binary tree is full if each node is either a leaf or possesses exactly
two child nodes.
• Complete tree
• A binary tree is complete if all levels (except possibly the last) are
completely full, and the last level has all its nodes to the left side.
Binary Tree
• Full tree
• A binary tree is full if each node is either a leaf or possesses exactly
two child nodes.
• Complete tree
• A binary tree is complete if all levels (except possibly the last) are
completely full, and the last level has all its nodes to the left side.
Full and
complete
Binary Tree
• How many nodes can there be in one level of a binary
tree?
• Maximum number of nodes in level =
0
Level 0 ≤2
1
Level 1 ≤2
2
Level 2 ≤2
Binary Tree
• How many nodes can there be in in a binary tree with
height ?
• We have the levels
• Maximum number of nodes
0
Level 0 ≤2
1
Level 1 ≤2
2
Level 2 ≤2
Binary Tree
• What is the height of a binary tree with nodes?
• We have seen that
• So,
• Minimum height is or simply
• Maximum height is
0
Level 0 ≤2
1
Level 1 ≤2
2
Level 2 ≤2
Binary Tree
• What is the height of a binary tree with nodes?
• For example, if we have 10 nodes
• Minimum height =
• Maximum height =
Binary Tree
• What is the height of a binary tree with nodes?
• For example, if we have 10 nodes
• Minimum height =
• Maximum height =
Binary Tree
• What is the height of a binary tree with nodes?
• For example, if we have 10 nodes
• Minimum height =
• Maximum height =
2 12
Binary Search Trees
• Examples
5
10 10
2 45
5 30 5 45
30
2 25 45 2 25 30
10
25
Binary Search Trees
• Examples
5
10 10
2 45
5 30 5 45
30
2 25 45 2 25 30
10
25
Binary search Not a binary
trees search tree
Binary Search Tree Specification
Structure: The placement of each element in the binary tree must satisfy
the binary search property: The value of the key of an element is
greater than the value of the key of any element in its left
subtree, and less than the value of the key of any element in its
right subtree.
Operations:
MakeEmpty
Function Initializes tree to empty state.
Postcondition Tree exists and is empty.
Boolean IsEmpty
Function Determines whether tree is empty.
Postcondition Returns true if tree is empty and false otherwise.
Boolean IsFull
Function Determines whether tree is full.
Postcondition Returns true if tree is full and false otherwise.
Binary Search Tree Specification
int LengthIs
Function Determines the number of elements in tree.
Postcondition Returns the number of elements in tree.
RetrieveItem(ItemType& item, Boolean& found)
Function Retrieves item whose key matches item's key (if present).
Precondition Key member of item is initialized.
Postcondition If there is an element someItem whose key matches item's key,
then found = true and item is a copy of someItem; otherwise,
found = false and item is unchanged. Tree is unchanged.
InsertItem(ItemType item)
Function Adds item to tree.
Precondition Tree is not full. item is not in tree.
Postcondition item is in tree. Binary search property is maintained.
Binary Search Tree Specification
DeleteItem(ItemType item)
Function Deletes the element whose key matches item's key.
Precondition Key member of item is initialized. One and only one element in
tree has a key matching item's key.
Postcondition No element in tree has a key matching item's key.
Print()
Function Prints the values in the tree in ascending key order.
Postcondition Items in the tree are printed in ascending key order.
Implementing the Nodes in Binary Search Tree
3 9
4 7 12
6 8 20
binarysearchtree.h
#ifndef BINARYSEARCHTREE_H_INCLUDED
#define BINARYSEARCHTREE_H_INCLUDED
class TreeType
{
public:
TreeType();
~TreeType();
void MakeEmpty();
bool IsEmpty();
// bool IsFull();
int LengthIs();
void RetrieveItem(ItemType& item, bool& found);
void InsertItem(ItemType item);
void DeleteItem(ItemType item);
void ResetTree(OrderType order);
void GetNextItem(ItemType& item, OrderType order, bool& finished);
void Print();
private:
TreeNode* root;
};
#endif // BINARYSEARCHTREE_H_INCLUDED
binarysearchtree.cpp
#include "binarysearchtree.h" bool TreeType::IsFull()
{
TreeType::TreeType() TreeNode* location;
{ try
{
root = NULL; location = new TreeNode;
} delete location;
return false;
bool TreeType::IsEmpty() }
{ catch(std::bad_alloc exception)
return root == NULL; {
} return true;
}
}
binarysearchtree.cpp
#include "binarysearchtree.h" bool TreeType::IsFull()
#include <new> {
TreeNode* location;
TreeType::TreeType() try
{
{ location = new TreeNode;
}
root = NULL; O(1) delete location;
return false;
bool TreeType::IsEmpty()
} O(1)
catch(std::bad_alloc exception)
{ {
}
return root == NULL; O(1) }
return true;
}
Function InsertItem
TreeNode *root
Function InsertItem
Insert 5
TreeNode *root
Function InsertItem
Insert 5
TreeNode *root
5
Function InsertItem
Insert 9
TreeNode *root
5
Function InsertItem
Insert 9
TreeNode *root
9
Function InsertItem
Insert 7
TreeNode *root
9
Function InsertItem
Insert 7
TreeNode *root
7
Function InsertItem
Insert 3
TreeNode *root
7
Function InsertItem
Insert 3
TreeNode *root
3 9
7
Function InsertItem
Insert 8
TreeNode *root
3 9
7
Function InsertItem
Insert 8
TreeNode *root
3 9
8
Function InsertItem
Insert 12
TreeNode *root
3 9
8
Function InsertItem
Insert 12
TreeNode *root
3 9
7 12
8
Function InsertItem
Insert 6
TreeNode *root
3 9
7 12
8
Function InsertItem
Insert 6
TreeNode *root
3 9
7 12
6 8
Function InsertItem
Insert 4
TreeNode *root
3 9
7 12
6 8
Function InsertItem
Insert 4
TreeNode *root
3 9
4 7 12
6 8
Function InsertItem
Insert 20
TreeNode *root
3 9
4 7 12
6 8
Function InsertItem
Insert 20
TreeNode *root
3 9
4 7 12
6 8 20
Function InsertItem
void Insert(TreeNode &tree, ItemType item)
{
if (tree == NULL)//Base case:Insertion place found.
{
tree = new TreeNode;
tree->right = NULL;
tree->left = NULL:
tree->info = item;
}
else if (item < tree->info)
Insert(tree->left, item);//General case 1:Insert in left subtree.
else
Insert(tree->right, item);//General case 2:Insert in right subtree.
}
Insert(root,5)
Function InsertItem
root
InsertItem(5)
5
Function InsertItem
root
5
Function InsertItem
root
InsertItem(9)
5
Function InsertItem
root
InsertItem(9)
Insert(root,9) 5
Function InsertItem
root
InsertItem(9)
Insert(root->right,9)
Function InsertItem
root
InsertItem(9)
9
Function InsertItem
root
InsertItem(9)
9
Function InsertItem
root
9
Function InsertItem
root
InsertItem(7)
9
Function InsertItem
root
InsertItem(7)
Insert(root,7) 5
9
Function InsertItem
root
InsertItem(7)
9
Function InsertItem
root
InsertItem(7)
Insert(root->right,7) 9
Function InsertItem
root
InsertItem(7)
7
Function InsertItem
root
InsertItem(7)
7
Function InsertItem
root
InsertItem(7)
7
Function InsertItem
root
7
Function InsertItem
root
InsertItem(3)
7
Function InsertItem
root
InsertItem(3)
Insert(root,3) 5
7
Function InsertItem
root
InsertItem(3)
7
Function InsertItem
root
InsertItem(3)
Insert(root->left,3) 9
7
Function InsertItem
root
InsertItem(3)
7
Function InsertItem
root
InsertItem(3)
7
Function InsertItem
root
InsertItem(3)
3 9
7
Function InsertItem
root
InsertItem(3)
3 9
7
Function InsertItem
root
3 9
7
Function InsertItem
Impact of order of insertion on tree height
Function InsertItem
Impact of order of insertion on tree height
Function InsertItem
Impact of order of insertion on tree height
Function InsertItem
void Insert(TreeNode *&tree, ItemType item)
{
if (tree == NULL)//Base case:Insertion place found.
{
tree = new TreeNode;
tree->right = NULL;
tree->left = NULL:
tree->info = item; Worst case: O(N)
}
else if (item < tree->info)
Best case: O(logN)
Insert(tree->left, item);//General case 1:Insert in left subtree.
else
Insert(tree->right, item);//General case 2:Insert in right subtree.
}