Thursday, March 24, 2011

prac 8 new & old

#include
#include

struct TreeNodeRec
{
int key;
struct TreeNodeRec *leftPtr;
struct TreeNodeRec *rightPtr;
};

typedef struct TreeNodeRec TreeNode;

void CreateTree(TreeNode **nodePtr);
TreeNode *makeTreeNode(int value);
TreeNode *insert(TreeNode *nodePtr, int item);
TreeNode *search(TreeNode *nodePtr, int item);
void printInorder(const TreeNode *nodePtr);
void printPreorder(const TreeNode *nodePtr);
void printPostorder(const TreeNode *nodePtr);

int sumValue(TreeNode *nodePtr);
int findLargest(TreeNode *nodePtr);
int isPositive(TreeNode *nodePtr);

int sumValue(TreeNode *nodePtr)
{
if(nodePtr == NULL)
return 0;
else
return nodePtr->key + sumValue(nodePtr->leftPtr) + sumValue(nodePtr->rightPtr);
}

int findLargest(TreeNode *nodePtr)
{
int left, right, largest = 0;

if(nodePtr != NULL)
{
largest = nodePtr->key;
left = findLargest(nodePtr->leftPtr);
right = findLargest(nodePtr->rightPtr);

if(largest < left)
largest = left;

if(largest < right)
largest = right;
}
return largest;
}

int isPositive(TreeNode *nodePtr)
{
if(nodePtr == NULL)
return 1;
else
{
if(nodePtr->key <0)
return 0;
else
return 1 && isPositive(nodePtr->leftPtr) &&isPositive(nodePtr->rightPtr);
}
}

void CreateTree(TreeNode **nodePtr)
{
*nodePtr = NULL;
}

TreeNode *makeTreeNode(int value)
{
TreeNode *newNodePtr = NULL;
newNodePtr = (TreeNode*)malloc(sizeof(TreeNode));

if (newNodePtr == NULL)
{
printf("Out of memory\n");
exit(1);
}
else
{
newNodePtr->key = value;
newNodePtr->leftPtr = NULL;
newNodePtr->rightPtr = NULL;
}
return newNodePtr;
}

TreeNode *insert(TreeNode *nodePtr, int item)
{
if (nodePtr == NULL)
{
nodePtr = makeTreeNode(item);
}
else if (item < nodePtr->key)
{
nodePtr->leftPtr = insert(nodePtr->leftPtr, item);
}
else if (item > nodePtr->key)
{
nodePtr->rightPtr = insert(nodePtr->rightPtr, item);
}

return nodePtr;
}

TreeNode *search(TreeNode *nodePtr, int target)
{
if (nodePtr != NULL)
{
if (target < nodePtr->key)
{
nodePtr = search(nodePtr->leftPtr, target);
}
else if (target > nodePtr->key)
{
nodePtr = search(nodePtr->rightPtr, target);
}
}
return nodePtr;
}

void printInorder(TreeNode *nodePtr)
{
if (nodePtr != NULL)
{
printInorder(nodePtr->leftPtr);
printf(" %d", nodePtr->key);
printInorder(nodePtr->rightPtr);
}
}

void printPreorder(TreeNode *nodePtr)
{
if (nodePtr != NULL)
{
printf(" %f", nodePtr->key);
printPreorder(nodePtr->leftPtr);
printPreorder(nodePtr->rightPtr);
}
}

void printPostorder(TreeNode *nodePtr)
{
if (nodePtr != NULL)
{
printPostorder(nodePtr->leftPtr);
printPostorder(nodePtr->rightPtr);
printf(" %f", nodePtr->key);
}
}
/*
//old prac8
#include
#include

typedef struct node* LINK;

struct node {
int value;
LINK left;
LINK right;
};

int sumValue(LINK root);
int findLargest(LINK root);
int isPositive(LINK root);
LINK makeTreeNode(int value);
LINK insert(LINK nodePtr, int item);
void inOrder(LINK nodePtr);

int main(void) {
int i,n,item, ary[] = {5, 8, 2, 6, 7, 9, 3, -4, 0, 1};
LINK root = NULL;

for(i = 0; i < 10; i++)
root = insert(root, ary[i]);

inOrder(root);
printf("\n\n");

//HERE: add the function call stmts and print the result

printf("Enter number of items ");
scanf(" %d", &n);

for (i = 0; i < n; i++)
{
scanf(" %d", &item);
root = insert(root, item);
}
inOrder(root);
printf("\n\n");

return 0;
}

LINK makeTreeNode(int value) {
LINK newNodePtr = NULL;

newNodePtr = (LINK)malloc(sizeof(*newNodePtr));

if (newNodePtr == NULL) {
printf("Out of memory\n");
exit(100);
}
else {
newNodePtr->value = value;
newNodePtr->left = NULL;
newNodePtr->right = NULL;
}
return newNodePtr;
}

LINK insert(LINK nodePtr, int item) {
if (nodePtr == NULL)
nodePtr = makeTreeNode(item);
else if (item < nodePtr->value)
nodePtr->left = insert(nodePtr->left, item);
else if (item > nodePtr->value)
nodePtr->right = insert(nodePtr->right, item);

return nodePtr;
}

void inOrder(LINK nodePtr) {
if(nodePtr != NULL) {
inOrder(nodePtr->left);
printf("%d ", nodePtr->value);
inOrder(nodePtr->right);
}
}
*/

No comments:

Post a Comment