#include <stdio.h> #include <stdlib.h> #include <math.h> typedef struct BstNode { int…
- User
#include <stdio.h> #include <stdlib.h> #include <math.h>
typedef struct BstNode { int data; struct BstNode* lchild; struct BstNode* rchild; } BstNode;
BstNode* root = NULL;
BstNode* GetNewNode(int data) { BstNode* newNode = (BstNode*)malloc(sizeof(BstNode)); newNode->data = data; newNode->lchild = NULL; newNode->rchild = NULL; return newNode; }
BstNode* Insert(BstNode* root, int data) { if (root == NULL) { root = GetNewNode(data); } else if (data <= root->data) { root->lchild = Insert(root->lchild, data); } else { root->rchild = Insert(root->rchild, data); } return root; }
int max(int a, int b) { return (a > b) ? a : b; }
int FindHeight(BstNode* root) { if (root == NULL) { return -1; } else { int leftHeight = FindHeight(root->lchild); int rightHeight = FindHeight(root->rchild); return max(leftHeight, rightHeight) + 1; } }
typedef struct QNode { BstNode* data; struct QNode* next; } QNode;
typedef struct { QNode* front; QNode* rear; int n; } BiTQueue;
BiTQueue* InitQueue() { BiTQueue* newqueue = (BiTQueue*)malloc(sizeof(BiTQueue)); newqueue->front = NULL; newqueue->rear = NULL; newqueue->n = 0; return newqueue; }
void Push(BiTQueue* Q, BstNode* T) { QNode* node = (QNode*)malloc(sizeof(QNode)); node->data = T; node->next = NULL;
if (Q->rear == NULL) { Q->front = node; Q->rear = node; } else { Q->rear->next = node; Q->rear = node; } Q->n++;}
BstNode* Pop(BiTQueue* Q) { if (Q->front == NULL) { return NULL; }
BstNode* T = Q->front->data; QNode* temp = Q->front; if (Q->front == Q->rear) { Q->front = NULL; Q->rear = NULL; } else { Q->front = Q->front->next; } Q->n--; free(temp); return T;}
void LeveOrderTraverse(BstNode* T, int level) { if (T == NULL) { return; }
BiTQueue* Q = InitQueue(); BstNode* p = NULL; int currentLevel = 0; Push(Q, T); while (Q->n > 0) { int nodesAtCurrentLevel = Q->n; while (nodesAtCurrentLevel > 0) { p = Pop(Q); if (currentLevel == level) { printf("%d,", p->data); } if (p->lchild != NULL) { Push(Q, p->lchild); } if (p->rchild != NULL) { Push(Q, p->rchild); } nodesAtCurrentLevel--; } currentLevel++; if (currentLevel > level) { break; } }}
int main() { int cnt,level,a; scanf("%d",&cnt); for (int i=0;i<cnt;i++){ scanf("%d",&a); root=Insert(root,a); getchar(); } scanf("%d",&level); LeveOrderTraverse(root,level); return 0; }
- Assistant
The given code is an implementation of a binary search tree and a level order traversal function. The main function reads an integer
cntfrom the user, representing the number of elements to insert into the binary search tree. Then, it enters a loop to read each element and insert it into the tree using theInsertfunction. After the loop, it reads an integerlevelfrom the user, representing the level at which to perform the level order traversal. Finally, it calls theLeveOrderTraversefunction with the root of the binary search tree and the level to traverse.To test the code, you can input the number of elements to insert, followed by the elements themselves, separated by spaces. For example:
5 4 2 6 1 3 2This will create a binary search tree with the following structure:
4 / \ 2 6 / \ 1 3Then, it will perform a level order traversal at level 2 and print the elements on that level, which in this case is
1,3,.Note that the code assumes the elements are integers and does not handle any input errors.
- User
#include<stdio.h> #include <stdlib.h> typedef struct BTNode{ int data; struct BTNode* left; struct BTNode* right; }BTNode; BTNode* root=NULL; int a; BTNode* GetNewNode(int data){ BTNode* newNode=(BTNode*)malloc(sizeof(BTNode)); newNode->data=data; newNode->left=newNode->right=NULL; return newNode; } BTNode* Insert(BTNode* root,int data){ if(root==NULL) root=GetNewNode(data); else if(data>=root->data) Insert(root->right,data); else if(data<=root->data) Insert(root->left,data); return root; } void PreOrder(BTNode *root){ if(root==NULL) return;
printf("%d,",root->data); PreOrder(root->left); PreOrder(root->right);} int main(){ scanf("%d",&a); getchar(); root=Insert(root,a); while (scanf("%d",&a) != EOF){ getchar(); Insert(root,a); } PreOrder(root); }
- Assistant
The given code is an implementation of a binary tree and its pre-order traversal function.
In the code, a structure
BTNodeis defined to represent a node of the binary tree. It has an integerdataand two pointersleftandrightto point to the left and right child nodes of the current node.The main function reads an integer
afrom the user and inserts it into the binary tree using theInsertfunction. It then uses a while loop to read more integers from the user untilEOF(End of File) is encountered. Each integer is inserted into the binary tree by calling theInsertfunction. Finally, it calls thePreOrderfunction with the root of the binary tree to perform the pre-order traversal and print the elements.To test the code, you can input a series of integers, separated by spaces, and then press
Ctrl+Zin Windows orCtrl+Din Unix-like systems to indicate the end of input. For example:1 2 3 4 5 6This will create a binary tree with the following structure:
1 / \ 2 3 / \ 4 5 \ 6 - User
correct the code
- Assistant
Apologies for the mistake. The issue in the code is in the
Insertfunction. When recursively calling theInsertfunction for the left or right child, the returned value is not assigned to the corresponding child pointer of the current node.Please see the corrected code below:
#include <stdio.h> #include <stdlib.h> typedef struct BTNode { int data; struct BTNode* left; struct BTNode* right; } BTNode; BTNode* root = NULL; BTNode* GetNewNode(int data) { BTNode* newNode = (BTNode*)malloc(sizeof(BTNode)); newNode->data = data; newNode->left = newNode->right = NULL; return newNode; } BTNode* Insert(BTNode* root, int data) { if (root == NULL) { root = GetNewNode(data); } else if (data >= root->data) { root->right = Insert(root->right, data); } else if (data < root->data) { root->left = Insert(root->left, data); } return root; } void PreOrder(BTNode* root) { if (root == NULL) { return; } printf("%d,", root->data); PreOrder(root->left); PreOrder(root->right); } int main() { int a; scanf("%d", &a); // Read the first integer getchar(); // Consume the newline character root = Insert(root, a); // Insert the first integer into the tree while (scanf("%d", &a) != EOF) { getchar(); // Consume the newline character root = Insert(root, a); // Insert each subsequent integer into the tree } PreOrder(root); // Perform pre-order traversal return 0; }