C++ Binary Search Tree - Ballance Check Function

Hi Guys,

I cant manage to combine these 2 functions into 1, It checks the left and the right trees to see if the smaller key is on the left and the bigger key on the right. We have minimum and maximum values to be met for the left and right trees.

ty

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
void BST::TreeBallance(){
  int isLeftTree = BST::TreeBallanceLeftPrivate(root->left, root->key);
  int isRightTree = BST::TreeBallanceRightPrivate(root->right, root->key);
  if(isLeftTree == 0 && isRightTree == 0){
    cout << "The Tree Is Not Ballanced!!" << endl;
  }else
  {
    cout << "The Tree Is Ballanced." << endl;
  }
}

int BST::TreeBallanceLeftPrivate(node* Ptr, int Max){
  if(Ptr == NULL){
    return 1;
  }
  if(Ptr->left != NULL && Ptr->left->key > Ptr->key && Ptr->left->key > Max){
    return 0;
  }
  if(Ptr->right != NULL && Ptr->right->key < Ptr->key && Ptr->right->key > Max){
    return 0;
  }
  if((!TreeBallanceLeftPrivate(Ptr->left, Max))||(!TreeBallanceLeftPrivate(Ptr->right, Max))){
    return 0;
  }
  else{ 
    return 1;
  }
}

int BST::TreeBallanceRightPrivate(node* Ptr, int Min){
  if(Ptr == NULL){
    return 1;
  }
  if(Ptr->left != NULL && Ptr->left->key > Ptr->key && Ptr->left->key < Min){
    return 0;
  }
  if(Ptr->right != NULL && Ptr->right->key < Ptr->key && Ptr->right->key < Min){
    return 0;
  }
  if((!TreeBallanceRightPrivate(Ptr->left, Min))||(!TreeBallanceRightPrivate(Ptr->right, Min))){
    return 0;
  }
  else{ 
    return 1;
  }
}
Really no one? :(
Topic archived. No new replies allowed.