Você está na página 1de 10

NONRECURSIVE POSTORDER BINARY TREE TRAVERSAL:

void postOrderTraversalIterative(BinaryTree *root) {


  if (!root) return;
  stack<BinaryTree*> s;
  s.push(root);
  BinaryTree *prev = NULL;
  while (!s.empty()) {
    BinaryTree *curr = s.top();
    if (!prev || prev->left == curr || prev->right == curr) {
      if (curr->left)
        s.push(curr->left);
      else if (curr->right)
        s.push(curr->right);
    } else if (curr->left == prev) {
      if (curr->right)
        s.push(curr->right);
    } else {
      cout << curr->data << " ";
      s.pop();
    }
    prev = curr;
  }
}
NONRECURSIVE POSTORDER BINARY TREE TRAVERSAL USING TWO STACKS:
1. Push the root node to the first stack.
2. Pop a node from the first stack, and push it to the second stack.
3. Then push its left child followed by its right child to the first stack.
4. Repeat step 2) and 3) until the first stack is empty.
5. Once done, the second stack would have all the nodes ready to be traversed in post-order.
Pop off the nodes from the second stack one by one and you're done.
void postOrderTraversalIterativeTwoStacks(BinaryTree *root) {
  if (!root) return;
  stack<BinaryTree*> s;
  stack<BinaryTree*> output;
  s.push(root);
  while (!s.empty()) {
    BinaryTree *curr = s.top();
    output.push(curr);
    s.pop();
    if (curr->left)
      s.push(curr->left);
    if (curr->right)
      s.push(curr->right);
  }
  while (!output.empty()) {
    cout << output.top()->data << " ";
    output.pop();
  }
}
ALGORITHM FOR PREORDER TRAVERSAL:
• Push the root node to the stack.
• while stack is not empty
. • Pop a node from the stack, and enque It in the queue (or just print it, skip last step if you are just
printing it).
. • Push its right child followed by its left child to the child stack.
• end while
• Now the Queue would have all the nodes ready to be traversed in pre-order. Deque the nodes from the
Queue one by one and you will have the pre order traversal of the tree.
1. void preOrderTraversalIterative(BinaryTree *root)  
2.   {  
3.       if (!root) return;  
4.       stack<BinaryTree*> s;  
5.       Queue<BinaryTree*> output;  
6.       s.push(root);  
7.       while (!s.empty()) {  
8.           BinaryTree *curr = s.top();  
9.           //Enque Current element in Output queue or just print          
10.           output.enque(curr);  
11.           s.pop();  
12.           if (curr->right)  
13.               s.push(curr->right);  
14.           if (curr->left)  
15.               s.push(curr->left);  
16.       }  
17.       //skip this while loop if you were printting earlier      
18.       while (!output.empty()) {  
19.           cout << output.deque()->data << " ";  
20.       }  
21.   }  

THREADED BINARY TREES:


INSERTION OF DATA:
TRAVERSAL IN INORDER,PRE AND POST ORDER
Inorder::

struct NODE
{
 struct NODE *left;
 int value;
 struct NODE *right;
 struct NODE *thread;
}
inorder(struct NODE *curr)
{
 while(all the nodes are not over )
   {
    if(curr->left != NULL &&  ! visited(curr->left)) 
      {
       visit(curr->left);
       curr = curr->left;
      }
    else
      {
      printf("%d", curr->value);  
      if(curr->right != NULL) 
         curr = curr->right;
      else
          if(curr->thread != NULL)
              curr = curr->thread;  
      }
   }
}

#include<stdio.h>
#include<conio.h>         // include header files
struct node       // structure for the node of threaded binary tree
 {
  int data;
  int Lbit,Rbit;
  struct node *Lchild,*Rchild;
 };

struct queue       // structure for queue


 {
  struct node *num[10];
  int front,rear;
 };

struct queue addq(struct node *temp,struct queue q) // add function for queue
 {
  q.num[++q.rear]=temp;
  return q;
 }

int isempty(struct queue q)    // empty condition check for the queue
 {
  if(q.rear<q.front)
   return 1;
  else
   return 0;
 }

struct queue deletq(struct queue q) // delete function for queue


 {
  q.front=q.front+1;
  return q;
 }

struct node * getnode(void)   // getnode function for creating new node
 {
  struct node *temp;
  temp=(struct node *)malloc(sizeof(struct node));
  printf("\n\nEnter the data for the node :- ");
  scanf("%d",&temp->data);
  temp->Lchild=NULL;
  temp->Rchild=NULL;
  temp->Lbit=0;
  temp->Rbit=0;
  return temp;
 }

struct node * insuc(struct node *start)  // function to find inorder successor


 {
  struct node *temp;
  temp=start->Rchild;
  if(start->Rbit==1)
   {
    while(temp->Lbit!=0)
     temp=temp->Lchild;
   }
  return temp;
 }

void Linsert(struct node *S,struct node *T) // function to insert data to left
 {
  struct node *X;
  T->Lchild=S->Lchild;
  T->Lbit=S->Lbit;
  T->Rchild=S;
  S->Lchild=T;
  S->Lbit=1;
  T->Rbit=0;
  if(T->Lbit==1)
   {
    X=T->Lchild;
    while(X->Rbit!=0)
     X=X->Rchild;
    X->Rchild=T;
   }
 }

void Rinsert(struct node *S,struct node *T)// function to insert data to right
 {
  struct node *X;
  T->Rchild=S->Rchild;
  T->Rbit=S->Rbit;
  T->Lchild=S;
  S->Rchild=T;
  T->Lbit=0;
  T->Rbit=0;
  S->Rbit=1;
  if(T->Rbit==1)
   {
    X=insuc(T);
    X->Lchild=T;
   }
 }
           // create function for the threaded tree
struct node * create(struct node *head,struct queue q)
 {
  struct node *temp,*start;
  head=(struct node *)malloc(sizeof(struct node));
  head->Lchild=head;
  head->data=-0;
  head->Rchild=head;
  head->Lbit=0;
  head->Rbit=1;
  temp=getnode();
  Linsert(head,temp);
  q=addq(temp,q);
  while(isempty(q)!=1)  // loop until queue is not empty
   {
    temp=q.num[q.front];
    printf("\n\nDo you want to insert left child to %d Node (1.Yes 2.No) :- ",temp->data);
    if(getche()=='1')
     {
      start=getnode();
      Linsert(temp,start);  // insert newly created node to the left
      q=addq(start,q);
     }
    printf("\n\nDo you want to insert right child to %d Node (1.Yes 2.No) :- ",temp-
>data);
    if(getche()=='1')
     {
      start=getnode();
      Rinsert(temp,start);  // insert newly created node to the right
      q=addq(start,q);
     }
    q=deletq(q);        // delete element from the queue
   }
  return head;     // return head of the threaded binary tree
 }

void inorder(struct node *head)   // inorder traversal of threaded tree


 {
  while(1)
   {
    head=insuc(head);
    if(head->data==-0)
     break;
    printf("  %d",head->data);
   }
 }

void preorder(struct node *head)  // preorder traversal of threaded tree


 {
  struct node *start;
  int visited=0;
  start=head->Lchild;
  while(start!=head)
   {
    if(start->Lbit==1 && visited==0)
     {
      printf("  %d",start->data);
      start=start->Lchild;
     }
    else
     {
      if(visited==0)
       printf("  %d",start->data);
      if(start->Rbit==1)
       {
   start=start->Rchild;
   visited=0;
       }
      else
       {
   visited=1;
   start=start->Rchild;
       }
     }
   }
 }

void postorder(struct node *head)    // postorder traversal of threaded tree


 {
  struct node *start,*temp;
  start=head;
  while(start->Lbit!=0)
   {
    start=start->Lchild;
    if(start->Lbit==0 && start->Rbit!=0)
     start=start->Rchild;
   }
  do
   {
    printf("  %d",start->data);
    if(start->Lbit==0 && start->Rbit==0)
     {
      if(start==start->Rchild->Lchild)
       {
   do
   start=start->Rchild;
   while(start->Rbit!=0);
       }
      else if(start==start->Lchild->Rchild)
       start=start->Lchild;
      else
       {
   temp=start;
   while(temp->Lbit!=0)
   temp=temp->Lchild;
   temp=temp->Lchild;
   if(temp->Lchild==start)
    start=temp;
       }
     }
    else
     {
      while(start->Rbit!=0)
      start=start->Rchild;
      start=start->Rchild;
      if(start!=head)
       start=insuc(start);
      else
       break;
     }
   }
  while(start!=head->Lchild);
  if(start!= head)
   printf("  %d",start->data);
  else
   printf("  %d",start->Lchild->data);
 }
struct node * insert(struct node *head)   // insert function for threaded tree
 {
  struct node *start=head,*temp;
  int num,ch,flag=0;
  if(head==NULL)
   {                        // test case for empty tree.
    printf("\n\nThe tree is empty! Can't insert...");
    return head;
   }
  printf("\n\nEnter number after which you want to insert node :- ");
  scanf("%d",&num);
  do
   {
    start=insuc(start);    // search the node is present or not
    if(start->data==num)
     {
      flag=1;
      break;
     }
   }while(start!=head);
  if(flag==0)
   {                 // test case if node not present
    printf("\n\nThe node not exists! Can't insert...");
    return head;
   }
  temp=getnode();
  printf("\n\nWhere to insert node (1.Left 2.Right) :- ");
  scanf("%d",&ch);
  if(ch==1)
   Linsert(start,temp);       // function to insert node to left
  else
   Rinsert(start,temp);      // function to insert node to right
  return head;             // return the head of tree
 }

struct node * inpre(struct node * node) //function to find inorder predecessor


 {
  struct node * temp;
  temp=node->Lchild;
  if(node->Lbit==1)
   {
    while(temp->Rbit==1)
    temp=temp->Rchild;
   }
  return temp;
 }

struct node *parent(struct node* node)


 {
  struct node *temp,*f;
  if(node->Lbit==0 && node->Rbit==0)
   {
    if(node==node->Rchild->Lchild )
     f=insuc(node);
    else
     f=node->Lchild;
   }
  else
   {
    temp=node;
    while(temp->Lbit!=0)
     temp=temp->Lchild;
    temp=temp->Lchild;
    if(temp->Rchild==node)
     f=temp;
    else
     {
      temp=node;
      while(temp->Rbit!=0 )
       temp=temp->Rchild;
      temp=insuc(temp);
      f=temp;
     }
   }
  return f;
 }
          // search function to search element
struct node * search(struct node *head,int key)
 {
  struct node* root=head;
  do
   {
    root=insuc(root);
    if(root==head)
     return NULL;
    if(root->data==key)
     return root;
   }while(1);
 }
          // function to delete node from threaded tree
struct node * deletnode(struct node *head,int no)
 {
  struct node *temp,*f,*rp,*s,*temp2,*root=head,*temp3;
  if(head==NULL)
   {                // test case if tree is empty
    printf("\n\nThe tree is empty! Can't delete...");
    return head;
   }
  temp=search(root,no);
  if(temp==NULL)
   {                 // test case if element to be deleted is not found
    printf("\n\nNode not found! Can't delete...");
    return head;
   }
  else
   {
    temp3=parent(temp);
    if(temp->Lbit==0 && temp->Rbit==0)
     {
      if(temp3->Lchild==temp)
       {
   rp=temp->Lchild;
   temp3->Lbit=0;
       }
      else if(temp3->Rchild==temp)
       {
   rp=temp->Rchild;
   temp3->Rbit=0;
       }
     }
    else if(temp->Lbit==0)
     insuc(temp)->Lchild=temp->Lchild;
    else if(temp->Rbit==0)
     inpre(temp)->Rchild=temp->Rchild;
    else
     {
      rp=insuc(temp);
      f=parent(rp);
      if(f!=temp)
       {
   if(rp->Rbit!=0)
    f->Lchild=rp->Rchild;
   else
    f->Lchild=rp;
   f->Lbit=rp->Rbit;
   rp->Rchild=temp->Rchild;
   rp->Rbit=1;
       }
      inpre(temp)->Rchild=rp;
      rp->Lchild=temp->Lchild;
      rp->Lbit=1;
     }
    if(head->Lchild==temp)
     head->Lchild=rp;
    else if(temp3->Lchild== temp)
     temp3->Lchild=rp;
    else
     temp3->Rchild=rp;
    free(temp);
   }
  if(head->Lchild==head)
   {
    free(head);
    head=NULL;
   }
  inorder(head);       // display inoreder traversal after deletion of node
  return head;      // return the head of the tree
 }

int main(void)      // main function


 {
  struct node *s=NULL;    // make the head node & make is NULL
  struct queue q;
  char ch1,ch2;
  int num;
  q.front=0;
  q.rear=-1;
  do
   {
    clrscr();
    printf("\n\nWhich operation do you want to perform...");
    printf("\n\n1.Create a threaded binary tree.");
    printf("\n2.Insert into threaded binary tree.");
    printf("\n3.Delete from threaded binary tree.");
    printf("\n4.Perform inorder traversal.");
    printf("\n5.Perform preorder traversal.");
    printf("\n6.Perform postorder traversal.");
    printf("\n7.Exit");
    printf("\n\nEnter your choice :- ");
    scanf("%d",&ch1);   // read the choice from the user
    switch(ch1)      // go to appropriate cace as per users choice
     {
      case 1:
      s=create(s,q);     // create function
      break;
      case 2:
      s=insert(s);       // function for inserting node
      break;
      case 3:
      printf("\n\nEnter Number to delete:- ");
      scanf("%d",&num);
      s=deletnode(s,num);     // call the deletenode function
      break;
      case 4:
      inorder(s);       // perform the inorder traversals
      break;
      case 5:
      preorder(s);     // perform the preorder traversals
      break;
      case 6:
      postorder(s);   // perform the postorder traversals
      break;
      case 7:
      printf("\n\nThanks for using the program.");
      break;
      default:
      printf("\n\nInvalid Choice! Please try again...");
      break;
     }
    getch();
   }while(ch1!=7);     // loop until user not exits
  return 0;
 }                  // end of main function

Você também pode gostar