// Function to insert a new node in complete binary tree void insert(struct node **root, int data, struct Queue* queue) { // Create a new node for given data struct node *temp = newNode(data); // If the tree is empty, initialize the root with new node. if (!*root) *root = temp; else { // get the front node of the queue. struct node* front = getFront(queue); // If the left child of this front node doesn’t exist, set the // left child as the new node if (!front->left) front->left = temp; // If the right child of this front node doesn’t exist, set the // right child as the new node else if (!front->right) front->right = temp; // If the front node has both the left child and right child, // Dequeue() it. if (hasBothChild(front)) Dequeue(queue); } // Enqueue() the new node for later insertions Enqueue(temp, queue); }
void insert(struct treeNode **root, int data, struct queue *q, struct stack *s) { treeNode *temp = initializeTN(data); if(temp == NULL) //if file is empty { exit(1); } if(!*root) { *root = temp; } else { node *front = frontOne(q); if(front->treeN->left == NULL) { front->treeN->left = temp; front->treeN->left->parent = front->treeN; } else if(front->treeN->right == NULL) { front->treeN->right = temp; front->treeN->right->parent = front->treeN; } if(hasBothChild(front->treeN)) { dequeue(q); } } enqueue(temp, q); push(temp, s); }