0% found this document useful (0 votes)
29 views80 pages

CS212 Sep2016 08 Trees

The document discusses trees and binary search trees. It defines key tree terminology like root, parent, child, and leaf nodes. It explains that a tree is a recursive data structure and lists some tree properties, including that a tree with N nodes will have N-1 links. It then describes binary trees and how they differ from general trees by restricting each node to at most two children. Finally, it demonstrates how to insert a new node into a binary search tree by recursively checking if the new value is less than or greater than existing nodes and adding it in the left or right subtree accordingly.

Uploaded by

Dahlia Gamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views80 pages

CS212 Sep2016 08 Trees

The document discusses trees and binary search trees. It defines key tree terminology like root, parent, child, and leaf nodes. It explains that a tree is a recursive data structure and lists some tree properties, including that a tree with N nodes will have N-1 links. It then describes binary trees and how they differ from general trees by restricting each node to at most two children. Finally, it demonstrates how to insert a new node into a binary search tree by recursively checking if the new value is less than or greater than existing nodes and adding it in the left or right subtree accordingly.

Uploaded by

Dahlia Gamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 80

Arab Academy for Science , Technology & Maritime Transport

College of Computing and Information Technology

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

 Structure for Binary Tree node:


Trees: Some Terminologies
 Root: Root
We use it to access or
traverse in our tree

It’s the identity


of the Tree
Trees: Some Terminologies
Parent

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

Sub Tree Sub Tree


Trees: Properties
 The Tree is a recursive data structure:
Root
Trees: Properties
 The Tree is a recursive data structure:

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

Not BST 10 20 BST 10 20

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

Node* CreateNewNode(int data) {


Node *temp = new Node();
15 temp->data = x;
temp->left = NULL;
temp->right = NULL;
10 20 return temp;
}

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

void insert(Node *&root, int x){


15 if(root == NULL){
root = CreateNewNode(x);
return;
10 20 }
else if(x <= root->data){
insert(root->left,x);
}
8 12 17 25 else {
insert(root->right,x);
}
 Ex: Insert 19 Insert Node in a BST
void insert(Node *&root, int x){
if(root == NULL){
15 root = CreateNewNode(x);
NO
return;
}
10 20 else if(x <= root->data){
insert(root->left,x);
}
else {
8 12 17 25 insert(root->right,x);
}
 Ex: Insert 19 Insert Node in a BST
void insert(Node *&root, int x){
if(root == NULL){
15 root = CreateNewNode(x);
return;
}
10 20 else if(x <= root->data){
insert(root->left,x); NO
}
else {
8 12 17 25 insert(root->right,x);
}
 Ex: Insert 19 Insert Node in a BST
void insert(Node *&root, int x){
if(root == NULL){
15 root = CreateNewNode(x);
return;
}
10 20 else if(x <= root->data){
insert(root->left,x);
}
else {
8 12 17 25 insert(root->right,x); YES
}
 Ex: Insert 19 Insert Node in a BST
void insert(Node *&root, int x){
if(root == NULL){
15 root = CreateNewNode(x);
return;
}
10 20 else if(x <= root->data){
insert(root->left,x);
}
else {
8 12 17 25 insert(root->right,x); YES
}
 Ex: Insert 19 Insert Node in a BST
void insert(Node *&root, int x){
if(root == NULL){
15 root = CreateNewNode(x); NO
return;
}
10 20 else if(x <= root->data){
insert(root->left,x);
}
else {
8 12 17 25 insert(root->right,x);
}
 Ex: Insert 19 Insert Node in a BST
void insert(Node *&root, int x){
if(root == NULL){
15 root = CreateNewNode(x);
return;
}
10 20 else if(x <= root->data){
insert(root->left,x); YES
}
else {
8 12 17 25 insert(root->right,x);
}
 Ex: Insert 19 Insert Node in a BST
void insert(Node *&root, int x){
if(root == NULL){
15 root = CreateNewNode(x);
return;
}
10 20 else if(x <= root->data){
insert(root->left,x); YES
}
else {
8 12 17 25 insert(root->right,x);
}
 Ex: Insert 19 Insert Node in a BST
void insert(Node *&root, int x){
if(root == NULL){
15 root = CreateNewNode(x);
NO
return;
}
10 20 else if(x <= root->data){
insert(root->left,x);
}
else {
8 12 17 25 insert(root->right,x);
}
 Ex: Insert 19 Insert Node in a BST
void insert(Node *&root, int x){
if(root == NULL){
15 root = CreateNewNode(x);
return;
}
10 20 else if(x <= root->data){
insert(root->left,x); NO
}
else {
8 12 17 25 insert(root->right,x);
}
 Ex: Insert 19 Insert Node in a BST
void insert(Node *&root, int x){
if(root == NULL){
15 root = CreateNewNode(x);
return;
}
10 20 else if(x <= root->data){
insert(root->left,x);
}
else {
8 12 17 25 insert(root->right,x); YES
}
 Ex: Insert 19 Insert Node in a BST
void insert(Node *&root, int x){
if(root == NULL){
15 root = CreateNewNode(x);
return;
}
10 20 else if(x <= root->data){
insert(root->left,x);
}
else {
8 12 17 25 insert(root->right,x); YES
}
 Ex: Insert 19 Insert Node in a BST
void insert(Node *&root, int x){
if(root == NULL){
15 root = CreateNewNode(x); YES
return;
}
10 20 else if(x <= root->data){
insert(root->left,x);
}
else {
8 12 17 25 insert(root->right,x);
}
 Ex: Insert 19 Insert Node in a BST
void insert(Node *&root, int x){
if(root == NULL){
15 root = CreateNewNode(x); YES
return;
}
10 20 else if(x <= root->data){
insert(root->left,x);
}
else {
8 12 17 25 insert(root->right,x);
}
19
Node* CreateNewNode(int data) {
Insert Node in a BST Node *temp = new Node();
temp->data = x;
temp->left = NULL;
temp->right = NULL;
15 return temp;
}

10 20 void insert(Node *&root, int x){


if(root == NULL){
root = CreateNewNode(x);
return;
8 12 17 25 }
else if(x <= root->data){
insert(root->left,x);
19 }
else {
insert(root->right,x);
}
Search for a number in BST
 How we can search for a number in a BST ?
 The solution is similar to what we did in
insertion , (using recursion)
15

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)

 Copy a Binary Search Tree

 Check if 2 Trees are equal


END of Section
Thank You 
Eng. Mahmoud Osama Radwan
[email protected] – AAST CCIT

You might also like