CS212 Sep2016 08 Trees
CS212 Sep2016 08 Trees
Data Structures
and Algorithms (CS212)
Section 08 :
Trees
Eng. Mahmoud Osama Radwan
[email protected] – AAST CCIT
Section Index
Intro to Trees
Tree Terminologies
Tree Properties
Binary Tree
Binary Search Tree
Trees
unlike linked lists , stacks & queues ,
A Tree is non-linear data structure
Child Child
Trees: Some Terminologies
Parent
Child Child
Trees: Some Terminologies
Leaf:
Node without
children
Trees: Properties
The Tree is a recursive data structure:
Root
Root
Trees: Properties
The Tree is a recursive data structure:
Root
Trees: Properties
If the tree have N Nodes ,
Then there’s N-1 Links
11 Nodes
10 Links
Trees: Properties
Depth of a Node:
Length or Number of links
From Root to the Node Ex:
Depth of
Node 76 = 2
Trees: Properties
Height of a Node:
Longest Length from the
Node to a leaf Ex:
Height of
Node 72 = 2
Trees: Properties
Height of a Tree:
Longest Length from the
Root to a leaf Height of this
Tree = 3
Trees: Properties
Levels
Level 0
Level 1
Level 2
Level 3
Binary Tree
Binary Tree:
A Tree in which each
Node have
At most 2
children
Binary Tree
If we use linked list to represent it:
50
17 72
12 23 54 76
9 14 19 67
Binary Search Tree
for each node the value of all nodes in left
sub-tree is lesser or equal , and values in
right sub-tree is greater
15 15
8 12 21 25 8 12 17 25
Insert Node in a BST
How we can Insert a Node in this BST ?
15
10 20
8 12 17 25
Insert Node in a BST
First, Create a function to create a node
8 12 17 25
Insert Node in a BST
Now Create the Insert Function ,
Check if tree is empty or not , if it’s empty create
the new node and point the root to it
void insert(Node *&root, int x){
15 if(root == NULL){
root = CreateNewNode(x);
return;
10 20 }
8 12 17 25
Insert Node in a BST
Check if the new node is <= the root , then insert
it in the left sub-tree , else insert in right sub-tree
10 20
8 12 17 25
Search for a number in BST
First , Check if the root is empty or not , then
print “not found”
void search(Node *&root, int x){
if(root == NULL){
15 printf(“Not Found”);
return;
}
10 20
8 12 17 25
Search for a number in BST
If the target is equal to the root , then it’s
found
void search(Node *&root, int x){
if(root == NULL){
15 printf(“Not Found”);
return;
}
10 20 else if(x == root->data){
printf(“Found”);
return;
}
8 12 17 25
Search for a number in BST
Check if the target is < the root , then search in
the left sub-tree , else search right
void search(Node *&root, int x){
if(root == NULL){
printf(“Not Found”);
return;
15 }
else if(x == root->data){
printf(“Found”);
10 20 return;
}
else if(x < root->data){
search(root->left,x);
8 12 17 25 }
else{
search(root->right,x);
}
Search for a number in BST
void search(Node *&root, int x){
Ex: Search for 12 if(root == NULL){
printf(“Not Found”);
15 return;
}
else if(x == root->data){
10 20 printf(“Found”);
return;
}
else if(x < root->data){
8 12 17 25 search(root->left,x);
}
else{
search(root->right,x);
}
Search for a number in BST
void search(Node *&root, int x){
Ex: Search for 12 if(root == NULL){
printf(“Not Found”); NO
15 return;
}
else if(x == root->data){
10 20 printf(“Found”);
return;
}
else if(x < root->data){
8 12 17 25 search(root->left,x);
}
else{
search(root->right,x);
}
Search for a number in BST
void search(Node *&root, int x){
Ex: Search for 12 if(root == NULL){
printf(“Not Found”);
15 return;
}
else if(x == root->data){
10 20 printf(“Found”);
NO
return;
}
else if(x < root->data){
8 12 17 25 search(root->left,x);
}
else{
search(root->right,x);
}
Search for a number in BST
void search(Node *&root, int x){
Ex: Search for 12 if(root == NULL){
printf(“Not Found”);
15 return;
}
else if(x == root->data){
10 20 printf(“Found”);
return;
}
else if(x < root->data){
8 12 17 25 search(root->left,x); YES
}
else{
search(root->right,x);
}
Search for a number in BST
void search(Node *&root, int x){
Ex: Search for 12 if(root == NULL){
printf(“Not Found”);
15 return;
}
else if(x == root->data){
10 20 printf(“Found”);
return;
}
else if(x < root->data){
8 12 17 25 search(root->left,x); YES
}
else{
search(root->right,x);
}
Search for a number in BST
void search(Node *&root, int x){
Ex: Search for 12 if(root == NULL){
printf(“Not Found”);
return;
NO
15
}
else if(x == root->data){
10 20 printf(“Found”);
return;
}
else if(x < root->data){
8 12 17 25 search(root->left,x);
}
else{
search(root->right,x);
}
Search for a number in BST
void search(Node *&root, int x){
Ex: Search for 12 if(root == NULL){
printf(“Not Found”);
15 return;
}
else if(x == root->data){
10 20 printf(“Found”); NO
return;
}
else if(x < root->data){
8 12 17 25 search(root->left,x);
}
else{
search(root->right,x);
}
Search for a number in BST
void search(Node *&root, int x){
Ex: Search for 12 if(root == NULL){
printf(“Not Found”);
15 return;
}
else if(x == root->data){
10 20 printf(“Found”);
return;
}
else if(x < root->data){
8 12 17 25 search(root->left,x); NO
}
else{
search(root->right,x);
}
Search for a number in BST
void search(Node *&root, int x){
Ex: Search for 12 if(root == NULL){
printf(“Not Found”);
15 return;
}
else if(x == root->data){
10 20 printf(“Found”);
return;
}
else if(x < root->data){
8 12 17 25 search(root->left,x);
}
else{
search(root->right,x); YES
}
Search for a number in BST
void search(Node *&root, int x){
Ex: Search for 12 if(root == NULL){
printf(“Not Found”);
15 return;
}
else if(x == root->data){
10 20 printf(“Found”);
return;
}
else if(x < root->data){
8 12 17 25 search(root->left,x);
}
else{
search(root->right,x); YES
}
Search for a number in BST
void search(Node *&root, int x){
Ex: Search for 12 if(root == NULL){
printf(“Not Found”);
NO
15 return;
}
else if(x == root->data){
10 20 printf(“Found”);
return;
}
else if(x < root->data){
8 12 17 25 search(root->left,x);
}
else{
search(root->right,x);
}
Search for a number in BST
void search(Node *&root, int x){
Ex: Search for 12 if(root == NULL){
printf(“Not Found”);
15 return;
}
else if(x == root->data){
YES
10 20 printf(“Found”);
return;
}
else if(x < root->data){
8 12 17 25 search(root->left,x);
}
else{
search(root->right,x);
}
Search for a number in BST
void search(Node *root, int x){
Ex: Search for 12 if(root == NULL){
printf(“Not Found”);
15 return;
}
else if(x == root->data){
YES
10 20 printf(“Found”);
return;
}
else if(x < root->data){
8 12 17 25 search(root->left,x);
}
else{
search(root->right,x);
Found }
Search for a number in BST
Node* search(Node *&root, int x){
if(root == NULL){
return root;
}
Useful Search: else if(x == root->data){
Return a pointer return root;
to the found }
else if(x < root->data){
node , instead return search(root->left,x);
of just saying }
else{
“found” return search(root->right,x);
}
}
Count elements in the BST
How we can count nodes in a BST ?
15
10 20
8 12 17 25
Count elements in the BST
First , Check if root is empty then return 0
int count(Node *&root){
if(root == NULL){
return 0;
15 }
10 20
8 12 17 25
Count elements in the BST
Else , return 1 + count the left Subtree +
count the right
int count(Node *&root){
15 if(root == NULL){
return 0;
}
10 20 else {
return 1+count(root->left)+
count(root->right);
}
8 12 17 25 }
Count elements in the BST
int count(Node *&root){
if(root == NULL){
Ex: Count this tree }
return 0;
else {
return 1+count(root->left)+
15 count(root->right);
}
}
10 20
8 12 17 25
Count elements in the BST
int count(Node *&root){
if(root == NULL){
Ex: Count this tree }
return 0;
else {
return 1+count(root->left)+
15 count(root->right);
}
}
10 20 count(15)
8 12 17 25
Count elements in the BST
int count(Node *&root){
if(root == NULL){
Ex: Count this tree }
return 0;
else {
return 1+count(root->left)+
15 count(root->right);
}
}
10 20 count(15)
1+count(10)+count(20)
8 12 17 25
Count elements in the BST
int count(Node *&root){
if(root == NULL){
Ex: Count this tree }
return 0;
else {
return 1+count(root->left)+
15 count(root->right);
}
}
10 20 count(15)
1+count(10)+count(20)
1+ 1+count(8)+count(12) +count(20)
8 12 17 25
Count elements in the BST
int count(Node *&root){
if(root == NULL){
Ex: Count this tree }
return 0;
else {
return 1+count(root->left)+
15 count(root->right);
}
}
10 20 count(15)
1+count(10)+count(20)
1+1+count(8)+count(12)+count(20)
8 12 17 25 1+1+ 1+c(null)+c(null) +c(12)+c(20)
Count elements in the BST
int count(Node *&root){
if(root == NULL){
Ex: Count this tree }
return 0;
else {
return 1+count(root->left)+
15 count(root->right);
}
}
10 20 count(15)
1+count(10)+count(20)
1+1+count(8)+count(12)+count(20)
8 12 17 25 1+1+1+c(null)+c(null)+c(12)+c(20)
1+1+1+0+c(null)+c(12)+c(20)
Count elements in the BST
int count(Node *&root){
if(root == NULL){
Ex: Count this tree }
return 0;
else {
return 1+count(root->left)+
15 count(root->right);
}
}
10 20 count(15)
1+count(10)+count(20)
1+1+count(8)+count(12)+count(20)
8 12 17 25 1+1+1+c(null)+c(null)+c(12)+c(20)
1+1+1+0+0+c(12)+c(20)
Count elements in the BST
int count(Node *&root){
if(root == NULL){
Ex: Count this tree }
return 0;
else {
return 1+count(root->left)+
15 count(root->right);
}
}
10 20 count(15)
1+count(10)+count(20)
1+1+count(8)+count(12)+count(20)
8 12 17 25 1+1+1+c(null)+c(null)+c(12)+c(20)
1+1+1+0+0+c(12)+c(20)
1+1+1+0+0+ 1+c(null)+c(null) +c(20)
Count elements in the BST
int count(Node *&root){
if(root == NULL){
Ex: Count this tree }
return 0;
else {
return 1+count(root->left)+
15 count(root->right);
}
}
10 20 count(15)
1+count(10)+count(20)
1+1+count(8)+count(12)+count(20)
8 12 17 25 1+1+1+c(null)+c(null)+c(12)+c(20)
1+1+1+0+0+c(12)+c(20)
1+1+1+0+0+1+0+0+c(20)
Count elements in the BST
int count(Node *&root){
if(root == NULL){
Ex: Count this tree }
return 0;
else {
return 1+count(root->left)+
15 count(root->right);
}
}
10 20 count(15)
1+count(10)+count(20)
1+1+count(8)+count(12)+count(20)
8 12 17 25 1+1+1+c(null)+c(null)+c(12)+c(20)
1+1+1+0+0+c(12)+c(20)
1+1+1+0+0+1+0+0+c(20)
1+1+1+0+0+1+0+0+ 1+c(17)+c(25)
Count elements in the BST
int count(Node *&root){
if(root == NULL){
Ex: Count this tree }
return 0;
else {
return 1+count(root->left)+
15 count(root->right);
}
}
10 20 count(15)
1+count(10)+count(20)
1+1+count(8)+count(12)+count(20)
8 12 17 25 1+1+1+c(null)+c(null)+c(12)+c(20)
1+1+1+0+0+c(12)+c(20)
1+1+1+0+0+1+0+0+c(20)
1+1+1+0+0+1+0+0+1+c(17)+c(25)
1+1+1+0+0+1+0+0+1+ 1+0+0 +c(25)
Count elements in the BST
int count(Node *&root){
if(root == NULL){
Ex: Count this tree }
return 0;
else {
return 1+count(root->left)+
15 count(root->right);
}
}
10 20 count(15)
1+count(10)+count(20)
1+1+count(8)+count(12)+count(20)
8 12 17 25 1+1+1+c(null)+c(null)+c(12)+c(20)
1+1+1+0+0+c(12)+c(20)
1+1+1+0+0+1+0+0+c(20)
1+1+1+0+0+1+0+0+1+c(17)+c(25)
1+1+1+0+0+1+0+0+1+1+0+0+ 1+0+0
Count elements in the BST
int count(Node *&root){
if(root == NULL){
Ex: Count this tree }
return 0;
else {
return 1+count(root->left)+
15 count(root->right);
}
}
10 20 count(15)
1+count(10)+count(20)
1+1+count(8)+count(12)+count(20)
8 12 17 25 1+1+1+c(null)+c(null)+c(12)+c(20)
1+1+1+0+0+c(12)+c(20)
1+1+1+0+0+1+0+0+c(20)
1+1+1+0+0+1+0+0+1+c(17)+c(25)
1+1+1+0+0+1+0+0+1+1+0+0+1+0+0 = 7
Find the Min element in the BST
How we can find the Min of a BST ?
15
10 20
8 12 17 25
Find the Min element in the BST
Check if the tree is empty , then return not
found or -1 int min(Node *&root){
if(root == NULL){
return -1;
15 }
10 20
8 12 17 25
Find the Min element in the BST
Check if there is no left child , then you have found a min ,
else get the min
int min(Node *&root){
of the left subtree
if(root == NULL){
return -1;
15 }
else if(root->left == NULL){
return root->data;
10 20 }
else {
return min(root->left);
8 12 17 25 }
Find the Min element in the BST
Ex: Find the Min
int min(Node *&root){
if(root == NULL){
return -1;
15 }
else if(root->left == NULL){
return root->data;
10 20 }
else {
return min(root->left);
8 12 17 25 }
Find the Min element in the BST
Ex: Find the Min
int min(Node *&root){
if(root == NULL){
return -1;
15 }
else if(root->left == NULL){
return root->data;
10 20 }
else {
return min(root->left);
8 12 17 25 }
Find the Min element in the BST
Ex: Find the Min
int min(Node *&root){
if(root == NULL){
return -1;
15 }
else if(root->left == NULL){
return root->data;
10 20 }
else {
return min(root->left);
8 12 17 25 }
Find the Min element in the BST
Ex: Find the Min
int min(Node *&root){
if(root == NULL){
return -1;
15 }
else if(root->left == NULL){
return root->data;
10 20 }
else {
return min(root->left);
8 12 17 25 }
Return 8
Section Homework
Create functions for the following :
sum , avg , max Submit it to
[email protected]
Calculate the Depth of a Node (Pseudo-code)