Advertisement
shashiskills

Untitled

Oct 30th, 2012
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.88 KB | None | 0 0
  1. //=======================================================================================================
  2. //Finished 9th Nov 2011 3:55 pm
  3. //code by: shashi kant
  4.  
  5. //=======================================================================================================
  6. #include<stdio.h>
  7. #include<stdlib.h>
  8. # define TREESIZE 14
  9. #define nodeDel 10
  10.  
  11.  
  12. struct node //tree node structure ::NOTE: PARENT Node can be stored ...looks more feasible
  13. {
  14. int data;
  15. struct node *rnode;
  16. struct node *lnode;
  17. };
  18.  
  19.  
  20. typedef struct node* Node;
  21. int count_node=0;
  22. int count_leave=0;
  23.  
  24.  
  25. //function declarations
  26. void count_nodes(Node root);
  27. void count_leaves(Node root);
  28. //Node delete_dataNode(Node root,int data);
  29. Node deleteNode(Node root,int data); //my version working
  30. int max(int a,int b);
  31. int maxSumOfTreeFromANode(Node root);
  32. int height(Node root);
  33. Node minimum(Node root,Node returnParent);
  34. Node binaryInsert(Node root,int data);
  35. Node binarySearch_iterative(Node root ,int data);
  36. Node maximum(Node root);
  37. void Go_binarySearch_recursive(Node root ,int data);
  38. int binarySearch_recursive(Node root ,int data);
  39. void traverse_inorder(Node root); //for binary sort
  40. void traverse_preorder(Node root);
  41. void traverse_postorder(Node root);
  42. void digraphOutput(Node root);
  43. void printOutput(Node root,FILE *fp);
  44. Node findParent(Node root,int data);
  45.  
  46.  
  47.  
  48. //function Implementations.....
  49. Node binaryInsert(Node root,int data) //iterative definition
  50. {
  51. Node prevnode,curnode=root;
  52. if(root==NULL) //tree is empty
  53. {
  54. root=(Node)malloc(sizeof(struct node ));
  55. root->data=data;
  56. root->rnode=root->lnode=NULL;
  57. return root;
  58. }
  59.  
  60. //else
  61. while(curnode!=NULL)
  62. {
  63. prevnode=curnode;
  64. curnode=(curnode->data < data)?curnode->rnode :curnode->lnode; //binary decision
  65. }
  66.  
  67. if(prevnode->data < data) //on the right side
  68. {
  69. prevnode->rnode=(Node)malloc(sizeof(struct node));
  70. prevnode->rnode->data=data;
  71. prevnode->rnode->rnode=NULL;
  72. prevnode->rnode->lnode=NULL;
  73. }
  74. else if(prevnode->data > data) //on the left side
  75. {
  76. prevnode->lnode=(Node)malloc(sizeof(struct node));
  77. prevnode->lnode->data=data;
  78. prevnode->lnode->rnode=NULL;
  79. prevnode->lnode->lnode=NULL;
  80. }
  81. else printf("Data already present in the tree :: \"NO copying of Data Protocol\" \n"); //no copying allowed
  82. return root;
  83. }
  84.  
  85.  
  86. Node binarySearch_iterative(Node root ,int data)
  87. {
  88. Node curnode=root,prevnode;
  89. if(root==NULL) //empty tree
  90. {
  91. printf("Tree empty :no data in the tree\n");
  92. return NULL;
  93. }
  94. //else
  95. while(curnode!=NULL && curnode->data!=data)
  96. {
  97. prevnode=curnode;
  98. curnode=(curnode->data < data)?curnode->rnode :curnode->lnode; //binary decision
  99. }
  100. if(curnode==NULL)
  101. {
  102. printf("Item not found \n");
  103. return NULL;
  104. }
  105. else if(curnode->data==data)
  106. {
  107. printf("Item found\n");
  108. return curnode;
  109. }
  110. return NULL;
  111. }
  112.  
  113.  
  114. void Go_binarySearch_recursive(Node root ,int data)
  115. {
  116. int x=-1;
  117. x=binarySearch_recursive(root,data);
  118. if(x==0)printf("item not found \n");
  119. }
  120.  
  121. int binarySearch_recursive(Node root ,int data) //recursive definition
  122. {
  123. int x=0;
  124. if(root!=NULL)
  125. {
  126. if(root->data == data){ printf("Item found \n");return 1;}
  127. else
  128. if(root->data < data)
  129. x=binarySearch_recursive(root->rnode,data);
  130. else
  131. x=binarySearch_recursive(root->lnode,data);
  132. }
  133. return x;
  134. }
  135.  
  136.  
  137.  
  138.  
  139. Node maximum(Node root) //returns the node with maximum data value
  140. {
  141. Node curnode=root;
  142. if(root==NULL)printf("Tree is empty");
  143. while(curnode->rnode!=NULL)curnode=curnode->rnode;
  144. printf("maximum node is %d",curnode->data);
  145. return curnode;
  146. }
  147.  
  148.  
  149. Node minimum(Node root,Node returnParent) //returns the node with least value and stores the parent value in returnParent
  150. {
  151.  
  152. Node curnode=root;
  153. if(root==NULL)printf("Tree is empty");
  154. while(curnode->lnode!=NULL){returnParent=curnode ; curnode=curnode->lnode;}
  155. //printf("minimum node is %d \n",curnode->data);
  156. return curnode;
  157.  
  158. }
  159.  
  160. int height(Node root)
  161. {
  162. if(root==NULL) return 0;
  163. return 1+max(height(root->rnode),height(root->lnode));
  164. }
  165.  
  166. int max(int a,int b)
  167. {
  168. return a>b?a:b;
  169. }
  170.  
  171.  
  172. //Finds out the Maximum sum of node values from root down to any Leaf
  173. int maxSumOfTreeFromANode(Node root) {
  174.  
  175. }
  176.  
  177. void count_nodes(Node root) //inorder way
  178. {
  179. if(root!=NULL)
  180. {
  181. count_nodes(root->rnode);
  182. count_node++;
  183. count_nodes(root->lnode);
  184. }
  185. }
  186.  
  187. void count_leaves(Node root)
  188. {
  189. if(root!=NULL)
  190. {
  191. if(root->rnode==NULL && root->lnode==NULL) count_leave++; //if both left and right subtrees are null it's a leaf
  192. count_leaves(root->rnode);
  193. count_leaves(root->lnode);
  194. }
  195.  
  196. }
  197.  
  198.  
  199. //deletion my version
  200. Node deleteNode(Node root,int data)
  201. {
  202. Node curnode,parent;
  203. if(root == NULL)
  204. {
  205. printf("Error: Tree empty\n");
  206. return root;
  207. }
  208.  
  209.  
  210.  
  211. curnode =root;
  212. while( curnode!=NULL && curnode->data!=data)
  213. {
  214. parent=curnode;
  215. curnode=(curnode->data < data)?curnode->rnode:curnode->lnode;
  216. }
  217.  
  218.  
  219. if(curnode==NULL)
  220. {
  221. printf("Error: item not found");
  222. return root;
  223. }
  224.  
  225. //else if it's a leaf node
  226. if(curnode->lnode==NULL && curnode->rnode == NULL)
  227. {
  228. if(parent->data > curnode->data)parent->lnode=NULL;
  229. else parent->rnode=NULL;
  230. free(curnode);
  231. return root;
  232. }
  233.  
  234. //case when it has atleast a child
  235. if(curnode->lnode==NULL && curnode->rnode!=NULL)
  236. {
  237. if(parent->data > curnode->data)parent->lnode=curnode->rnode;
  238. else parent->rnode=curnode->rnode;
  239. free(curnode);
  240. return root;
  241. }
  242.  
  243. if(curnode->lnode!=NULL && curnode->rnode == NULL)
  244. {
  245. if(parent->data > curnode->data)parent->lnode=curnode->lnode;
  246. else parent->rnode=curnode->lnode;
  247. free(curnode);
  248. return root;
  249. }
  250.  
  251. //cas when both children are present
  252. else{
  253. //find successor and swap the values and delete node at successor position
  254. Node SuccNode,parent;
  255. int temp;
  256. SuccNode=minimum(curnode->rnode,parent); //finding successor
  257. //parent=findParent(root,SuccNode->data);
  258.  
  259.  
  260. //if(parent->data > SuccNode->data)
  261. parent->lnode=NULL; //unecessary check min element will be at left most end hence will be on left side of Parent
  262. //else parent->rnode=SuccNode->rnode;
  263.  
  264. temp=curnode->data;
  265. curnode->data=SuccNode->data;
  266. SuccNode->data=temp;
  267.  
  268. free(SuccNode);
  269. return root;
  270.  
  271. }
  272.  
  273. }
  274.  
  275.  
  276.  
  277.  
  278. //tree traversing tech
  279. void traverse_inorder(Node root)
  280. {
  281. if(root!=NULL)
  282. {
  283. traverse_inorder(root->lnode);
  284. printf(" %d",root->data);
  285. traverse_inorder(root->rnode);
  286. }
  287. }
  288.  
  289.  
  290. void traverse_preorder(Node root)
  291. {
  292. if(root!=NULL)
  293. {
  294. printf(" %d",root->data);
  295. traverse_preorder(root->lnode);
  296. traverse_preorder(root->rnode);
  297. }
  298. }
  299.  
  300.  
  301. void traverse_postorder(Node root)
  302. {
  303. if(root!=NULL)
  304. {
  305. traverse_postorder(root->lnode);
  306. traverse_postorder(root->rnode);
  307. printf(" %d",root->data);
  308. }
  309. }
  310.  
  311. //end of traversal techniques
  312.  
  313.  
  314. //Digraph output for Graphviz
  315. void digraphOutput(Node root)
  316. {
  317. FILE *fp;
  318. fp = fopen("mytree.dot","w");
  319. fprintf(fp,"digraph G { \n");
  320. fprintf(fp,"\"%d\";\n",root->data); //print root first
  321. printOutput(root,fp);
  322. fprintf(fp,"}\n");
  323. fclose(fp);
  324. }
  325.  
  326. void printOutput(Node root,FILE *fp)
  327. {
  328. if(root!=NULL)
  329. {
  330. //fprintf(fp,"\"%s\"",root->data);
  331. //find out children declare them and then the edge
  332. if(root->lnode!=NULL)
  333. {
  334. fprintf(fp,"\"%d\";\n",root->lnode->data);
  335. fprintf(fp,"\"%d\"-> \"%d\";\n",root->data,root->lnode->data);
  336. }
  337. if(root->rnode!=NULL)
  338. {
  339. fprintf(fp,"\"%d\";\n",root->rnode->data);
  340. fprintf(fp,"\"%d\"-> \"%d\";\n",root->data,root->rnode->data);
  341. }
  342. printOutput(root->lnode,fp);
  343. printOutput(root->rnode,fp);
  344. }
  345.  
  346. }
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353. Node findParent(Node root,int data)
  354. {
  355. Node curnode =root,parent;
  356. while( curnode!=NULL && curnode->data!=data)
  357. {
  358. // printf("fine 4\n");
  359. parent=curnode;
  360. printf("%d \n",curnode->data);
  361. curnode=(curnode->data < data)?curnode->rnode:curnode->lnode;
  362. }
  363. printf("%d <-",curnode->data);
  364. printf("%d parent \n \n",parent->data);
  365. return parent;
  366. }
  367.  
  368. //the main function....
  369. int main()
  370. {
  371. Node root=NULL;
  372. int count=0;
  373. int fillList[TREESIZE]={18,10,20,4,15,19,22,1,8,11,41,21,23,58};
  374.  
  375.  
  376. for(count=0;count<TREESIZE;count++)
  377. {
  378. root=binaryInsert(root,fillList[count]);
  379. }
  380.  
  381. root=deleteNode(root,11); //optional stuff only to check if deletion Logic works
  382. printf("\n\n Max sum of tree = %d\n\n",maxSumOfTreeFromANode(root));
  383. digraphOutput(root);
  384. traverse_inorder(root);
  385. printf("\n");
  386. return 0;
  387. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement