FIND MAX AND MINIMUM ( INSERT NODE )

I NEED THAT

WRITE A C++ CODE TO INSERT NODE INTO BST AND ASLO TO FIND THE MINIMUM AND MAXIMUM IN THE TREE...???


the snip of code bellow should give you a ideal on how write one



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 

typedef NodeType* NodePtr;

struct NodeType
{
 ComponentType component;
 NodePtr   Link;
};

// method that construct an empty list

HybridList::HybridList()
// constructor
// postcondition: head == NULL
{
   head == NULL;
}


// a method that test for empty list

bool HybridList::IsEmpty() const
{
 
 return (head == NULL); // return true if head == NULL.
}

// printing the list

void HybidList::Print() const
{
 //component member have been output
 
  NodePtr currentPtr = head;
 while(currentPtr != NULL)
  {
   cout<< currentPtr->component <<endl;
   currentPtr = currentPtr->Link;
  }
 }
 
// inserting item function
void HybridList::Insert( ComponentType item)
{
 // precondition: component members of the list are in ascending order
 //postcondition: new Node containing item is in proper place

 NodePtr newNodePtr = new NodeType; // set-up node to be inserted
 newNodePtr->component = item;
 
 NodePtr prevPtr = NULL;

 NodePtr currentPtr = head;

 while(currentPtr != NULL && item > currentPtr->component)
  {
   prevPtr = currentPtr;
   currentPtr = currentPtr->Link; // searching for proper place for item
  }
 
 // insert item
 newNodePtr->Link = currentPtr;
 if (prevPtr == NULL) head = newNodePtr;
 
  else
   
    prevPtr->Link = newNodePtr;
 delete newNodePtr;
}
didnt understand man......
@dnambembe, that's a list and he wanted a binary tree. Though you shouldn't do people's homework anyway.
@ahmadijaz, we're not going to do it for you. Write a basic binary tree node class, see how you can iterate through it (hint: use a recursive function). Then ask what exactly is your problem and we'll help.
Last edited on
Topic archived. No new replies allowed.