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

Imóvel do Banco, Apartamento T5 com Sótão; Bank Property, Apartment near Cent...
Imóvel do Banco, Apartamento T5 com Sótão; Bank Property, Apartment near Cent...Imóvel do Banco, Apartamento T5 com Sótão; Bank Property, Apartment near Cent...
Imóvel do Banco, Apartamento T5 com Sótão; Bank Property, Apartment near Cent...imostorept
 
Pesquisa de satisfação - Encontro Fazemos Acontecer
Pesquisa de satisfação - Encontro Fazemos AcontecerPesquisa de satisfação - Encontro Fazemos Acontecer
Pesquisa de satisfação - Encontro Fazemos AcontecerMarina Evangelista
 
Digitalização do varejo | Tecnologia na gestão do negócio: como alavancar a e...
Digitalização do varejo | Tecnologia na gestão do negócio: como alavancar a e...Digitalização do varejo | Tecnologia na gestão do negócio: como alavancar a e...
Digitalização do varejo | Tecnologia na gestão do negócio: como alavancar a e...E-Commerce Brasil
 
Cavaleiros do Futebol: Equipamentos e Moda para Campeões.pptx
Cavaleiros do Futebol: Equipamentos e Moda para Campeões.pptxCavaleiros do Futebol: Equipamentos e Moda para Campeões.pptx
Cavaleiros do Futebol: Equipamentos e Moda para Campeões.pptxEduardoRodriguesOliv2
 
Objeções - Jeb Blount livro negociios de
Objeções - Jeb Blount livro negociios deObjeções - Jeb Blount livro negociios de
Objeções - Jeb Blount livro negociios derafaelventura53
 
DROGASIL EQUIPE LOGISTICA MERCADOLOGICA.pptx
DROGASIL EQUIPE LOGISTICA MERCADOLOGICA.pptxDROGASIL EQUIPE LOGISTICA MERCADOLOGICA.pptx
DROGASIL EQUIPE LOGISTICA MERCADOLOGICA.pptxednamonteiro13
 
b2ee375d-671f-406c-8c60-df328a75e662.pdf
b2ee375d-671f-406c-8c60-df328a75e662.pdfb2ee375d-671f-406c-8c60-df328a75e662.pdf
b2ee375d-671f-406c-8c60-df328a75e662.pdfRenandantas16
 

Kürzlich hochgeladen (7)

Imóvel do Banco, Apartamento T5 com Sótão; Bank Property, Apartment near Cent...
Imóvel do Banco, Apartamento T5 com Sótão; Bank Property, Apartment near Cent...Imóvel do Banco, Apartamento T5 com Sótão; Bank Property, Apartment near Cent...
Imóvel do Banco, Apartamento T5 com Sótão; Bank Property, Apartment near Cent...
 
Pesquisa de satisfação - Encontro Fazemos Acontecer
Pesquisa de satisfação - Encontro Fazemos AcontecerPesquisa de satisfação - Encontro Fazemos Acontecer
Pesquisa de satisfação - Encontro Fazemos Acontecer
 
Digitalização do varejo | Tecnologia na gestão do negócio: como alavancar a e...
Digitalização do varejo | Tecnologia na gestão do negócio: como alavancar a e...Digitalização do varejo | Tecnologia na gestão do negócio: como alavancar a e...
Digitalização do varejo | Tecnologia na gestão do negócio: como alavancar a e...
 
Cavaleiros do Futebol: Equipamentos e Moda para Campeões.pptx
Cavaleiros do Futebol: Equipamentos e Moda para Campeões.pptxCavaleiros do Futebol: Equipamentos e Moda para Campeões.pptx
Cavaleiros do Futebol: Equipamentos e Moda para Campeões.pptx
 
Objeções - Jeb Blount livro negociios de
Objeções - Jeb Blount livro negociios deObjeções - Jeb Blount livro negociios de
Objeções - Jeb Blount livro negociios de
 
DROGASIL EQUIPE LOGISTICA MERCADOLOGICA.pptx
DROGASIL EQUIPE LOGISTICA MERCADOLOGICA.pptxDROGASIL EQUIPE LOGISTICA MERCADOLOGICA.pptx
DROGASIL EQUIPE LOGISTICA MERCADOLOGICA.pptx
 
b2ee375d-671f-406c-8c60-df328a75e662.pdf
b2ee375d-671f-406c-8c60-df328a75e662.pdfb2ee375d-671f-406c-8c60-df328a75e662.pdf
b2ee375d-671f-406c-8c60-df328a75e662.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; }