A binary tree is supplied in the data file against your name. Read the file, and
ID: 3633838 • Letter: A
Question
A binary tree is supplied in the data file against your name. Read the file, and create the tree in the standard pointer-based repreentation. Maintain links for left and right chlidren. Don't maintain parent links. In the first line of the file, the number of nodes is supplied. If this number is n, nodes in the tree are identified as 0,1,2,...,n - 1. For each node, its ID and the IDs of its two child nodes are supplied in the file (-1 indicates no child, that is, NULL). While creating the tree, you also need to identify the root of the tree (it is not the node with ID 0). Return a pointer to the root node. In all of the subsequent parts, this pointer should be passed as the tree, not the raw data from the file.Print all the leaves of the tree.
Determine the maximum level in the tree at which a leaf appears.
Print all the leaf nodes at the maximum level.
Explanation / Answer
i have code for exact problem , #include #include typedef struct _treenode { int id; struct _treenode *L; struct _treenode *R; } treenode; typedef treenode *tree; int nlp = 0; tree readtree ( ) { int n, i, id, L, R; treenode *nodearray; int *isroot; scanf("%d", &n); /* It does not matter whether we allocate n nodes one by one, or in a single chunk, so I go for the second alternative. */ nodearray = (tree)malloc(n * sizeof(treenode)); isroot = (int *)malloc(n * sizeof(int)); for (i=0; i= 0) { nodearray[i].R = &(nodearray[R]); isroot[R] = 0; } } for (i=0; i L); if ((T -> L == NULL) && (T -> R == NULL)) { printf("%4d", T -> id); if (++nlp == 18) { printf(" "); nlp = 0; } } printleaves(T -> R); } int maxleaflevel ( tree T, int level ) { int l, r; if (T == NULL) return -1; if ((T -> L == NULL) && (T -> R == NULL)) return level; l = maxleaflevel(T -> L, level + 1); r = maxleaflevel(T -> R, level + 1); return (l >= r) ? l : r; } int minleaflevel ( tree T, int level ) { int l, r; if (T == NULL) return 1000000000; if ((T -> L == NULL) && (T -> R == NULL)) return level; l = minleaflevel(T -> L, level + 1); r = minleaflevel(T -> R, level + 1); return (l L == NULL) && (T -> R == NULL)) { if (level == LEVEL) printf("%d ", T -> id); return; } if (T -> L != NULL) leavesatlevel(T->L,level+1,LEVEL); if (T -> R != NULL) leavesatlevel(T->R,level+1,LEVEL); } void printpaths ( tree T, int level, int ID[], int LEVEL ) { int i; if (T == NULL) return; if ((T -> L == NULL) && (T -> R == NULL)) { if (level == LEVEL) { printf(" "); for (i=0; i L != NULL) { ID[level + 1] = T -> L -> id; printpaths(T -> L, level + 1, ID, LEVEL); } if (T -> R != NULL) { ID[level + 1] = T -> R -> id; printpaths(T -> R, level + 1, ID, LEVEL); } } int main ( ) { tree T; int l, s, ID[1000]; T = readtree(); printf("Id of root = %d ", T -> id); printf("Leaves in the tree are "); printleaves(T); printf(" "); l = maxleaflevel(T,0); s = minleaflevel(T,0); printf("Maximum level of a leaf in T is %d ", l); printf("Minimum level of a leaf in T is %d ", s); printf("Leaves at maximum level are "); leavesatlevel(T,0,l); printf(" "); printf("Leaves at minimum level are "); leavesatlevel(T,0,s); printf(" "); printf("Longest root-to-leaf paths: "); ID[0] = T -> id; printpaths(T,0,ID,l); printf("Shortest root-to-leaf paths: "); ID[0] = T -> id; printpaths(T,0,ID,s); exit(0); }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.