/* Code to find the sum of all elements in the tree */
void in_order_sum(struct btnode *p)
{
    if (!p)
    {
        return;
    }
    in_order_sum(p->l);
    sum = sum + p->value;
    in_order_sum(p->r);
}
Exemple #2
0
void in_order_sum (treeNode *p)
{
	if (!p)
	{
		return;
	}
	in_order_sum(p->left);
	sum = sum + p->data;
	in_order_sum(p->right);
}
void main()
{ 
 
    /* Inserting elements in the binary tree */
    root = newnode(50);
    root->l = newnode(20);
    root->r = newnode(30);
    root->l->l = newnode(70);
    root->l->r = newnode(80);
    root->l->l->l = newnode(10);
    root->l->l->r = newnode(40);
    root->l->r->r = newnode(60);
    printf("The elements of Binary tree are:");
    in_order_traversal(root);
    in_order_sum(root);
    printf("\nThe sum of all the elements are:%d", sum);
}
Exemple #4
0
int main()
{
	//treeNode *root = NULL;
	int x;
	int ch, num, num1;
	treeNode * temp;
	treeNode *med;
	int m = 1;

	do {
		printf("\nSelect a choice from the menu below.");
		printf("\n1. Generate Binary Search Tree");
		printf("\n2. Print the BST in pre-order format");
		printf("\n3. Print the BST in in-order format");
		printf("\n4. Print the BST in post-order format");
		printf("\n5. Print the BST in breadth-first format");
		printf("\n6. Find a value in the BST");
		printf("\n7. Find the minimum value in the BST nodes");
		printf("\n8. Find the maximum value in the BST nodes");
		printf("\n9. Calculate the average value of the BST nodes");
		printf("\n10. Find the median value of the BST nodes");
		printf("\n11. Calculate the sum of the BST nodes");
		printf("\n12. Count the number of BST nodes");
		printf("\n13. Delete a value in the BST");
		printf("\n14. Exit Program");
		printf("\n");
		printf("\nEnter Choice: ");
		scanf("%d", &ch);
		switch (ch) {
		case 1: 
			genrateTree();
			printf("\n");
			printf("\nTree in IN-ORDER : ");
			PrintInorder(root);
			printf("\n");
			break;

		case 2:
			PrintPreorder(root);
			break;

		case 3: 
			PrintInorder(root);
			break;

		case 4:
			PrintPostorder(root);
			break;
		case 5:
			printLevelOrder(root);
			break;
		case 6:
			printf("\nEnter the element to be Find in TREE: ");
			scanf("%d", &num);
			temp=Find(root, num);
			if (temp->data == num)
			{
				printf("Element Found\n");
			}
			else
			{
				printf("Element NOT Found\n");
			}
			break;
		case 7:
			temp = FindMin(root);
			printf("Minimum element is %d\n", temp->data);
			break;
		case 8:
			temp = FindMax(root);
			printf("Maximum element is %d\n", temp->data);
			break;
		case 9:
			in_order_sum(root);
			printf("Average element is %d\n", sum / counter(root));
			sum = 0;
			break;
		case 10:
			med = medianTraverse(root, findHeight(root));
			printf("Median Value is %d\n", med->data);
			break;
		case 11:
			in_order_sum(root);
			printf("\nThe sum of all the elements are:%d\n", sum);
			sum = 0;
			break;
		case 12:
			printf("Total Number of Nodes %d\n", counter(root));
			break;
		case 13:
			PrintInorder(root);
			printf("\n");
			printf("\nEnter the element to be Deleted: ");
			scanf("%d", &num);
			Delete(root, num);
			break;
		case 14:
			exit(0);
			break;

		default: exit(0);
		}
		//printf("%d", rootNode->data);
		printf("\n");
		printf("\nIf you want to return to the menu, press 1.");
		printf("\nChoice: ");
		scanf("%d", &num);
	} while (num == 1);
	
}