Professional Documents
Culture Documents
called as the Branches the Branches which are directed in the node is called indegree branches the Branches which are directed out the node is called outdegree branches //function to find the the successor void successor(node *root) { int key; parent=NULL; current=NULL; if(root==NULL) cout<<"empty tree\n"; else { cout<<"please enter the number\n"; cin>>key; current=bineary_search(key); } if(flag==1)//main process { if(current->pr==NULL) cout<<"the successor is not found\n";//element found and current is pointing to leaf else { current=current->pr; while(current->pl!=NULL) { current=current->pl; } cout<<"the successor is="<<current->iteam; } } else cout<<"not found\n"; }
//function to perform level order node *a[MAX_size]; int rear=-1; int front=-1; void addq(node *root) { if(MAX_size-1!=rear) { rear++; a[rear]=root; } } node *delq(void) { if(front!=rear) { front++;
return(a[front]); } } int emptyq(void) { if(rear==front) return 1;//if empty queue else return 0; } void level_order(node *root) { if(root==NULL) { cout<<"empty"; return; } addq(root); while(!(emptyq())) { root=delq(); if(root) { cout<<" "<<root->iteam; if(root->pl) addq(root->pl); if(root->pr) addq(root->pr); } else break; } } /*output input 4 5 7 8 15 45 output 4 5 45 15 7 8 */ //iterative inorder int top=-1; int stack_empty(void) { if(top==-1) return 1; else return 0; } void push(node *ptr) { top++; a[top]=ptr; } node *pop(void) { if(!(stack_empty()))
{ node *temp; temp=a[top]; top--; return(temp); } } void iter_Inorder(node *root) { if(root==NULL) { cout<<"empty"; return; } while(1) { for(1;root;root=root->pl)//repeat untill is root point to null push(root); root=pop(); if(root==NULL) break; cout<<" "<<root->iteam; root=root->pr; } } /*output input:-3 21 23 43 65 output:-3 21 23 43 65*/
//inorder traversal for threaded bineary tree node *find_succ(node *temp) { current=temp; current=current->pr_child;//right child if(!current->pr_thread)//if pointer to right thread is true the it is the leaf { while(!current->pl_thread) { current=current->pl_child; } } temp=current; return temp; } void inorder_threader(node *root) { node *temp=root; while(1) { temp=find_succ(temp); if(temp==root) break;
cout<<" "<<temp->iteam; } } //insert the thread at right of given key element //s is the address of the key elemnt //r is the new node with value inserted void insert_right_thread(node *s,node *r) { node *temp; r->right_child=parent->right_child; r->right_thread=parent->right_thread; r->left_child=parent; r->left_thread=TRUE; s->right_child=child; s->left_thread=FALSE; if(!r->right_thread)//if right thread is true the is's leaf { temp=insucc(r); temp->left_child=r; } } //function to create MAX HEAP in a preinsearted array //note that winner or losser tree is completely a diffrent concept than the MAX HEAP void topdown(int n,int a[]) { int k,j,key,p; for(k=1;k<n;k++)//0 is for root,since 0 is the parent itself no need to consider { key=a[k]; j=k; p=(j-1)/2; while((j>0) && (key>a[p])) { a[j]=a[p]; j=p; p=(j-1)/2; } a[j]=key; } } /*input:-54 13 27 65 32 19 64 output:-65 54 64 13 32 19 27 */ //BST binery search function node *bineary_search(int key) { current=root; parent=root; flag=0; while(current!=NULL) { if(key==current->iteam) { flag=1; return(current);
} else if(key<current->iteam) { parent=current; current=current->pl; } else if(key>current->iteam) { parent=current; current=current->pr; } } return (current); } void print_search(node *root,int key) { if(root==NULL) { cout<<"empty"; return; } else node *current=bineary_search(key); if(flag==1) cout<<"found"; else cout<<"not found"; } //function to insert a node to tree void insert_tree(void) { BTREE *temp; int x; temp=(node *)new(node); cout<<"please enter the number\n"; cin>>x; temp->iteam=x; temp->pl=NULL; temp->pr=NULL; if(root==NULL) { root=temp; } else { current=root; while(current!=NULL) { parent=current; if(current->iteam==x) { cout<<"Duplicate iteam\n"; break; } else if(current->iteam>x ) { current=current->pl;
} else if(current->iteam<x) { current=current->pr; } if(current==NULL)//if current is NULL then it as reached to leaf //here parent is pointing to the leaf node address { if(parent->iteam>x) { parent->pl=temp; break; } else { parent->pr=temp; break; } } } } } //ADT of THE BST here the bineary tree is called as BTREE objects:=a tree consists of a finite set of element called as node it can have no node(empty bineary tree),only one node called root ,of any number of node,which for a left child and right child for all b1,b2,b3 E bineary tree,and data E element BTREE create() ::= if bineary tree is empty create new BTREE and return 1 ::=if the root is NULL return 1 else return 0 if not empty BTREE MakeBT(b1,data,b2)::=return a bineary tree whose left subtree is b1 ,whose root is iteam & right subtree is b2 BTREE is_empty(b1) ::=return 1 for left subtree is empty return 0 for not empty BTREE is_empty(data) ::=return 1 for root or data field is empty return 0 for not empty BTREE is_empty(b2) ::=return 1 for right subtree is empty return 0 for not empty //function for copying the entire bineary tree node copy(node *orginal)//the root of the existing tree { node *temp; if(orginal!=NULL) { temp=get_node();//a small function to allocate memory using MALLOC temp->pl=copy(orignal->pl); temp->pr=copy(orignal->pr); temp->iteam=copy(orignal->iteam); return temp; } return NULL; } BTREE empty(b1)
//function for finding equality int equal(node BT1,node BT2) { //pl is left and pr is right child //return 1 for equality else return 0 //first check that both tree are exist //if exist then both iteam are equal return 1 else return 0 return((!BT1 && !BT2) || ((BT1 && BT2)&&(BT1->iteam==BT2->iteam)&&equal(BT1->pl,BT2>pl)&&equal(BT1->pr,BT2->pr)); } //ADT of MAX heap objects: a complete binary tree of n > 0 elements organized so that the value in each node is at least as large as those in its children functions: for all heap belong to MaxHeap, item belong to Element, n,max_size belong to integer function:MaxHeap Create(max_size) ::= create an empty heap that can hold a maximum of max_size elements Boolean HeapFull(heap, n) ::= if (n==max_size) return TRUE else return FALSE MaxHeap Insert(heap, item, n)::= if (!HeapFull(heap,n)) insert item into heap and return the resulting heap else return error Boolean HeapEmpty(heap, n) ::= if (n>0) return FALSE else return TRUE Element Delete(heap,n) ::= if (!HeapEmpty(heap,n)) return one instance of the largest element in the heap and remove it from the heap else return error //function for simple find in unions int simple_find(int i) { foe(1;parent[i]>=0;i=parent[i]); return i; } //function for union void simple_union(int i,int j) { parent[i]=j; } //function for wighted rule void wighted_rule_union(int i,int j) { int temp=parent[i]+parent[j]; if(parent[i]>parent[j])//the least value become to root { parent[i]=j;//j become the root parent[j]=temp; } else { parent[j]=i;//j become the root parent[i]=temp; } } //function for collapsing rule int collasping_rule(int i)//the i is key below which the tree as to be collasped { int root,trail,lead; for(root=i;parent[root]>=0;root=parent[root]);
//loop for find the root of given subtree element for(trail=i;trail!=root;trial=lead) { lead=parent[trial]; parent[trial]=root; } return root; } ---------------------------------------end--------------------------------------