You are on page 1of 2

/*ALGO ASSIGNMENT: B8 Kumar Chandan(11104688) Arpit Garg(11104666)*/ #include<stdio.

h> struct NodePtr{ struct NodePtr *Left; struct NodePtr *Right; int value; }; NodePtr CreateTree(FILE * fpData) { int in; fscanf(fpData, "%i", &in); Tree Tr = (NodePtr)malloc(sizeof(Node)); Tr->Left = NULL; Tr->Right = NULL; Tr->value = in; while((fscanf(fpData, "%i", &in)) != EOF) { InsertInTree(in, Tr); printf("\n %p", Tr); } return Tr; } void InsertInTree(int value,Tree Tr) { if(Tr == NULL) { Tr->Left = (NodePtr)malloc(sizeof(Node)); Tr->Left->Left = NULL; Tr->Left->Right = NULL; Tr->Left->value = value; printf("\n %i ", value); return; } if(Tr->Left == NULL) { InsertInNull(value, Tr->Left); } else if(Tr->Right == NULL) { InsertInNull(value, Tr->Right); } else { if(Tr->Left->Left == NULL || Tr->Left->Right == NULL) InsertInTree(value , Tr->Left); else InsertInTree(value, Tr->Right); } } template <typename T> bool BST<T>::Delete(const Tr & itemToDelete) {

return Delete(leaf, itemToDelete); } template <typename Tr> bool BST<Tr>::Delete(Node<Tr>* & ptr, const Tr& key) if (ptr==nullptr) { return false; // item not in BST } if (key < ptr->data) { Delete(ptr->LeftChild, key); } else if (key > ptr->data) { Delete(ptr->RightChild, key); } else { Node<Tr> *temp; if (ptr->LeftChild==nullptr) { temp = ptr->RightChild; delete ptr; ptr = temp; } else if (ptr->RightChild==nullptr) { temp = ptr->LeftChild; delete ptr; ptr = temp; } else //2 children { temp = ptr->RightChild; Node<Tr> *parent = nullptr; while(temp->LeftChild!=nullptr) { parent = temp; temp = temp->LeftChild; } ptr->data = temp->data; if (parent!=nullptr) Delete(parent->leftChild,parent->leftChild->data ); else Delete(ptr->rightChild,ptr->RightChild->data); } } }

You might also like