SlideShare ist ein Scribd-Unternehmen logo
1 von 4
#include <stdio.h>
#include <stdlib.h>
// Definição da estrutura do nó da árvore
struct Node {
int data;
int height; // Fator de balanceamento
struct Node* left;
struct Node* right;
};
// Função para criar um novo nó
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->height = 1;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Função para calcular a altura da árvore
int calculateHeight(struct Node* root) {
if (root == NULL) {
return 0; // Árvore vazia tem altura 0
}
// Calcule a altura das subárvores esquerda e direita
int leftHeight = calculateHeight(root->left);
int rightHeight = calculateHeight(root->right);
// A altura da árvore é a maior altura entre as subárvores mais 1 para contar o nível atual
root->height = 1 + (leftHeight > rightHeight ? leftHeight : rightHeight);
return root->height;
}
// Função para inserir um novo nó na árvore
struct Node* insertNode(struct Node* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insertNode(root->left, data);
} else if (data > root->data) {
root->right = insertNode(root->right, data);
}
// Recalcule o fator de balanceamento após a inserção
calculateHeight(root);
return root;
}
// Função para calcular o número de nós na árvore
int countNodes(struct Node* root) {
if (root == NULL) {
return 0;
}
return 1 + countNodes(root->left) + countNodes(root->right);
}
// Função para imprimir as folhas na ordem "esquerda-raiz-direita"
void printLeaves(struct Node* root) {
if (root == NULL) {
return;
}
// Primeiro, verifique se o nó atual é uma folha (não tem filhos)
if (root->left == NULL && root->right == NULL) {
printf("%d ", root->data); // Imprima o conteúdo da folha
}
// Em seguida, chame a função para os filhos esquerdo e direito
printLeaves(root->left);
printLeaves(root->right);
}
int main() {
struct Node* root = NULL;
// Inserir 5 nós com valores aleatórios na árvore
root = insertNode(root, 10);
root = insertNode(root, 5);
root = insertNode(root, 15);
root = insertNode(root, 3);
root = insertNode(root, 7);
// Calcular o número de nós na árvore e imprimir o resultado
int numNodes = countNodes(root);
printf("Número de nós na árvore: %dn", numNodes);
// Imprimir as folhas na ordem "esquerda-raiz-direita"
printf("Folhas na ordem e-r-d: ");
printLeaves(root);
printf("n");
// Calcular a altura da árvore e atualizar o fator de balanceamento em cada nó
calculateHeight(root);
printf("Altura da árvore: %dn", root->height);
return 0;
}

Weitere ähnliche Inhalte

Kürzlich hochgeladen

representações cartograficas - 1 ano.pptx
representações cartograficas - 1 ano.pptxrepresentações cartograficas - 1 ano.pptx
representações cartograficas - 1 ano.pptxCarladeOliveira25
 
Conferência SC 24 | Gestão logística para redução de custos e fidelização
Conferência SC 24 | Gestão logística para redução de custos e fidelizaçãoConferência SC 24 | Gestão logística para redução de custos e fidelização
Conferência SC 24 | Gestão logística para redução de custos e fidelizaçãoE-Commerce Brasil
 
66ssssssssssssssssssssssssssssss4434.pptx
66ssssssssssssssssssssssssssssss4434.pptx66ssssssssssssssssssssssssssssss4434.pptx
66ssssssssssssssssssssssssssssss4434.pptxLEANDROSPANHOL1
 
Conferência SC 24 | Data Analytics e IA: o futuro do e-commerce?
Conferência SC 24 | Data Analytics e IA: o futuro do e-commerce?Conferência SC 24 | Data Analytics e IA: o futuro do e-commerce?
Conferência SC 24 | Data Analytics e IA: o futuro do e-commerce?E-Commerce Brasil
 
Conferência SC 24 | Otimize sua logística reversa com opções OOH (out of home)
Conferência SC 24 | Otimize sua logística reversa com opções OOH (out of home)Conferência SC 24 | Otimize sua logística reversa com opções OOH (out of home)
Conferência SC 24 | Otimize sua logística reversa com opções OOH (out of home)E-Commerce Brasil
 
EP GRUPO - Mídia Kit 2024 - conexão de marcas e personagens
EP GRUPO - Mídia Kit 2024 - conexão de marcas e personagensEP GRUPO - Mídia Kit 2024 - conexão de marcas e personagens
EP GRUPO - Mídia Kit 2024 - conexão de marcas e personagensLuizPauloFerreira11
 
Conferência SC 24 | A força da geolocalização impulsionada em ADS e Fullcomme...
Conferência SC 24 | A força da geolocalização impulsionada em ADS e Fullcomme...Conferência SC 24 | A força da geolocalização impulsionada em ADS e Fullcomme...
Conferência SC 24 | A força da geolocalização impulsionada em ADS e Fullcomme...E-Commerce Brasil
 
Questionárionnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
QuestionárionnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnQuestionárionnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
QuestionárionnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnGustavo144776
 
Conferência SC 24 | Inteligência artificial no checkout: como a automatização...
Conferência SC 24 | Inteligência artificial no checkout: como a automatização...Conferência SC 24 | Inteligência artificial no checkout: como a automatização...
Conferência SC 24 | Inteligência artificial no checkout: como a automatização...E-Commerce Brasil
 
A LOGÍSTICA ESTÁ PREPARADA PARA O DECRESCIMENTO?
A LOGÍSTICA ESTÁ PREPARADA PARA O DECRESCIMENTO?A LOGÍSTICA ESTÁ PREPARADA PARA O DECRESCIMENTO?
A LOGÍSTICA ESTÁ PREPARADA PARA O DECRESCIMENTO?Michael Rada
 
Conferência SC 24 | Estratégias de precificação: loja própria e marketplace
Conferência SC 24 | Estratégias de precificação: loja própria e marketplaceConferência SC 24 | Estratégias de precificação: loja própria e marketplace
Conferência SC 24 | Estratégias de precificação: loja própria e marketplaceE-Commerce Brasil
 
Products Catalogue-01-Electronics thin wall heat shrink tubing wire and cable...
Products Catalogue-01-Electronics thin wall heat shrink tubing wire and cable...Products Catalogue-01-Electronics thin wall heat shrink tubing wire and cable...
Products Catalogue-01-Electronics thin wall heat shrink tubing wire and cable...Welldonelily Skype
 
Conferência SC 24 | Estratégias omnicanal: transformando a logística em exper...
Conferência SC 24 | Estratégias omnicanal: transformando a logística em exper...Conferência SC 24 | Estratégias omnicanal: transformando a logística em exper...
Conferência SC 24 | Estratégias omnicanal: transformando a logística em exper...E-Commerce Brasil
 
Conferência SC 2024 | Tendências e oportunidades de vender mais em 2024
Conferência SC 2024 | Tendências e oportunidades de vender mais em 2024Conferência SC 2024 | Tendências e oportunidades de vender mais em 2024
Conferência SC 2024 | Tendências e oportunidades de vender mais em 2024E-Commerce Brasil
 
Conferência SC 24 | Social commerce e recursos interativos: como aplicar no s...
Conferência SC 24 | Social commerce e recursos interativos: como aplicar no s...Conferência SC 24 | Social commerce e recursos interativos: como aplicar no s...
Conferência SC 24 | Social commerce e recursos interativos: como aplicar no s...E-Commerce Brasil
 
Analise Ergonomica FisioPrev aula de ergonomia
Analise Ergonomica FisioPrev aula de ergonomiaAnalise Ergonomica FisioPrev aula de ergonomia
Analise Ergonomica FisioPrev aula de ergonomiaGabrielPasquinelli1
 
Conferência SC 24 | Estratégias de diversificação de investimento em mídias d...
Conferência SC 24 | Estratégias de diversificação de investimento em mídias d...Conferência SC 24 | Estratégias de diversificação de investimento em mídias d...
Conferência SC 24 | Estratégias de diversificação de investimento em mídias d...E-Commerce Brasil
 
Conferência SC 24 | O custo real de uma operação
Conferência SC 24 | O custo real de uma operaçãoConferência SC 24 | O custo real de uma operação
Conferência SC 24 | O custo real de uma operaçãoE-Commerce Brasil
 
Conferência SC 24 | Estratégias de precificação para múltiplos canais de venda
Conferência SC 24 | Estratégias de precificação para múltiplos canais de vendaConferência SC 24 | Estratégias de precificação para múltiplos canais de venda
Conferência SC 24 | Estratégias de precificação para múltiplos canais de vendaE-Commerce Brasil
 
Ética NO AMBIENTE DE TRABALHO, fundamentosdas relações.pdf
Ética NO AMBIENTE DE TRABALHO,  fundamentosdas relações.pdfÉtica NO AMBIENTE DE TRABALHO,  fundamentosdas relações.pdf
Ética NO AMBIENTE DE TRABALHO, fundamentosdas relações.pdfInsttLcioEvangelista
 

Kürzlich hochgeladen (20)

representações cartograficas - 1 ano.pptx
representações cartograficas - 1 ano.pptxrepresentações cartograficas - 1 ano.pptx
representações cartograficas - 1 ano.pptx
 
Conferência SC 24 | Gestão logística para redução de custos e fidelização
Conferência SC 24 | Gestão logística para redução de custos e fidelizaçãoConferência SC 24 | Gestão logística para redução de custos e fidelização
Conferência SC 24 | Gestão logística para redução de custos e fidelização
 
66ssssssssssssssssssssssssssssss4434.pptx
66ssssssssssssssssssssssssssssss4434.pptx66ssssssssssssssssssssssssssssss4434.pptx
66ssssssssssssssssssssssssssssss4434.pptx
 
Conferência SC 24 | Data Analytics e IA: o futuro do e-commerce?
Conferência SC 24 | Data Analytics e IA: o futuro do e-commerce?Conferência SC 24 | Data Analytics e IA: o futuro do e-commerce?
Conferência SC 24 | Data Analytics e IA: o futuro do e-commerce?
 
Conferência SC 24 | Otimize sua logística reversa com opções OOH (out of home)
Conferência SC 24 | Otimize sua logística reversa com opções OOH (out of home)Conferência SC 24 | Otimize sua logística reversa com opções OOH (out of home)
Conferência SC 24 | Otimize sua logística reversa com opções OOH (out of home)
 
EP GRUPO - Mídia Kit 2024 - conexão de marcas e personagens
EP GRUPO - Mídia Kit 2024 - conexão de marcas e personagensEP GRUPO - Mídia Kit 2024 - conexão de marcas e personagens
EP GRUPO - Mídia Kit 2024 - conexão de marcas e personagens
 
Conferência SC 24 | A força da geolocalização impulsionada em ADS e Fullcomme...
Conferência SC 24 | A força da geolocalização impulsionada em ADS e Fullcomme...Conferência SC 24 | A força da geolocalização impulsionada em ADS e Fullcomme...
Conferência SC 24 | A força da geolocalização impulsionada em ADS e Fullcomme...
 
Questionárionnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
QuestionárionnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnQuestionárionnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
Questionárionnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
 
Conferência SC 24 | Inteligência artificial no checkout: como a automatização...
Conferência SC 24 | Inteligência artificial no checkout: como a automatização...Conferência SC 24 | Inteligência artificial no checkout: como a automatização...
Conferência SC 24 | Inteligência artificial no checkout: como a automatização...
 
A LOGÍSTICA ESTÁ PREPARADA PARA O DECRESCIMENTO?
A LOGÍSTICA ESTÁ PREPARADA PARA O DECRESCIMENTO?A LOGÍSTICA ESTÁ PREPARADA PARA O DECRESCIMENTO?
A LOGÍSTICA ESTÁ PREPARADA PARA O DECRESCIMENTO?
 
Conferência SC 24 | Estratégias de precificação: loja própria e marketplace
Conferência SC 24 | Estratégias de precificação: loja própria e marketplaceConferência SC 24 | Estratégias de precificação: loja própria e marketplace
Conferência SC 24 | Estratégias de precificação: loja própria e marketplace
 
Products Catalogue-01-Electronics thin wall heat shrink tubing wire and cable...
Products Catalogue-01-Electronics thin wall heat shrink tubing wire and cable...Products Catalogue-01-Electronics thin wall heat shrink tubing wire and cable...
Products Catalogue-01-Electronics thin wall heat shrink tubing wire and cable...
 
Conferência SC 24 | Estratégias omnicanal: transformando a logística em exper...
Conferência SC 24 | Estratégias omnicanal: transformando a logística em exper...Conferência SC 24 | Estratégias omnicanal: transformando a logística em exper...
Conferência SC 24 | Estratégias omnicanal: transformando a logística em exper...
 
Conferência SC 2024 | Tendências e oportunidades de vender mais em 2024
Conferência SC 2024 | Tendências e oportunidades de vender mais em 2024Conferência SC 2024 | Tendências e oportunidades de vender mais em 2024
Conferência SC 2024 | Tendências e oportunidades de vender mais em 2024
 
Conferência SC 24 | Social commerce e recursos interativos: como aplicar no s...
Conferência SC 24 | Social commerce e recursos interativos: como aplicar no s...Conferência SC 24 | Social commerce e recursos interativos: como aplicar no s...
Conferência SC 24 | Social commerce e recursos interativos: como aplicar no s...
 
Analise Ergonomica FisioPrev aula de ergonomia
Analise Ergonomica FisioPrev aula de ergonomiaAnalise Ergonomica FisioPrev aula de ergonomia
Analise Ergonomica FisioPrev aula de ergonomia
 
Conferência SC 24 | Estratégias de diversificação de investimento em mídias d...
Conferência SC 24 | Estratégias de diversificação de investimento em mídias d...Conferência SC 24 | Estratégias de diversificação de investimento em mídias d...
Conferência SC 24 | Estratégias de diversificação de investimento em mídias d...
 
Conferência SC 24 | O custo real de uma operação
Conferência SC 24 | O custo real de uma operaçãoConferência SC 24 | O custo real de uma operação
Conferência SC 24 | O custo real de uma operação
 
Conferência SC 24 | Estratégias de precificação para múltiplos canais de venda
Conferência SC 24 | Estratégias de precificação para múltiplos canais de vendaConferência SC 24 | Estratégias de precificação para múltiplos canais de venda
Conferência SC 24 | Estratégias de precificação para múltiplos canais de venda
 
Ética NO AMBIENTE DE TRABALHO, fundamentosdas relações.pdf
Ética NO AMBIENTE DE TRABALHO,  fundamentosdas relações.pdfÉtica NO AMBIENTE DE TRABALHO,  fundamentosdas relações.pdf
Ética NO AMBIENTE DE TRABALHO, fundamentosdas relações.pdf
 

Empfohlen

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 

Empfohlen (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

arvore binaraia cabalah 333 aula 023 prof Kerloson

  • 1. #include <stdio.h> #include <stdlib.h> // Definição da estrutura do nó da árvore struct Node { int data; int height; // Fator de balanceamento struct Node* left; struct Node* right; }; // Função para criar um novo nó struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->height = 1; newNode->left = NULL; newNode->right = NULL; return newNode; } // Função para calcular a altura da árvore int calculateHeight(struct Node* root) { if (root == NULL) { return 0; // Árvore vazia tem altura 0 } // Calcule a altura das subárvores esquerda e direita int leftHeight = calculateHeight(root->left); int rightHeight = calculateHeight(root->right);
  • 2. // A altura da árvore é a maior altura entre as subárvores mais 1 para contar o nível atual root->height = 1 + (leftHeight > rightHeight ? leftHeight : rightHeight); return root->height; } // Função para inserir um novo nó na árvore struct Node* insertNode(struct Node* root, int data) { if (root == NULL) { return createNode(data); } if (data < root->data) { root->left = insertNode(root->left, data); } else if (data > root->data) { root->right = insertNode(root->right, data); } // Recalcule o fator de balanceamento após a inserção calculateHeight(root); return root; } // Função para calcular o número de nós na árvore int countNodes(struct Node* root) { if (root == NULL) { return 0; } return 1 + countNodes(root->left) + countNodes(root->right);
  • 3. } // Função para imprimir as folhas na ordem "esquerda-raiz-direita" void printLeaves(struct Node* root) { if (root == NULL) { return; } // Primeiro, verifique se o nó atual é uma folha (não tem filhos) if (root->left == NULL && root->right == NULL) { printf("%d ", root->data); // Imprima o conteúdo da folha } // Em seguida, chame a função para os filhos esquerdo e direito printLeaves(root->left); printLeaves(root->right); } int main() { struct Node* root = NULL; // Inserir 5 nós com valores aleatórios na árvore root = insertNode(root, 10); root = insertNode(root, 5); root = insertNode(root, 15); root = insertNode(root, 3); root = insertNode(root, 7); // Calcular o número de nós na árvore e imprimir o resultado int numNodes = countNodes(root); printf("Número de nós na árvore: %dn", numNodes);
  • 4. // Imprimir as folhas na ordem "esquerda-raiz-direita" printf("Folhas na ordem e-r-d: "); printLeaves(root); printf("n"); // Calcular a altura da árvore e atualizar o fator de balanceamento em cada nó calculateHeight(root); printf("Altura da árvore: %dn", root->height); return 0; }