使用数组实现显示二叉树中的所有叶子

时间:2018-04-24 00:19:34

标签: c++ arrays binary-tree

我正在使用数组实现编写二叉树,而对于我的上一个函数displayLeafValues(),我试图只显示叶子。我现在拥有它的方式,它将输出索引值70然后是叶值。

最后一项功能的输出: 70 25 62 90 120

期望的输出: 25 62 90 120

我尝试从25开始索引,但是当我尝试显示其他值时,我得到了无限循环。我想知道是否有人可以给我一些建议。感谢y'所有。

class BinaryTree{
public:
int sizeOfArray;
int* binaryArray;

void insertElement(int x);
void searchElement(int x);
void parent(int x);
int extendSize(int x);
void preOrder(int index);
void postOrder(int index);
void displayRSR(int index);
void displayLSR(int index);
int treeLeafsCount(int index);
void displayLeafValues(int index);

BinaryTree(int sizeOfArray){
    int newSize = extendSize(sizeOfArray);
    binaryArray = new int[newSize];

    for(int x = 0; x < sizeOfArray; x++){
        binaryArray[x] = NULL;
    }
 }
};

int BinaryTree::extendSize(int x){
int value = 0;
for(int y = 0; y<x +1; y++){
    value =(2*value) +2;
 }
  return value;
}
void BinaryTree::insertElement(int x){
  int currentIndex = 0;
  std::cout <<"Adding "<< x;
  while(true){
     if(binaryArray[currentIndex] ==NULL){
         binaryArray[currentIndex] = x;
         std::cout << "Inserted at index: " << currentIndex <<std::endl;
         break;
      }
      else if(binaryArray[currentIndex] <=x){
          if(binaryArray[currentIndex] ==x){
             std::cout << "ERROR! --Repeating element" << std::endl;
             break;
          }
          else{
              std::cout<<" Right ";
              currentIndex = (2* currentIndex +2);
          }
      }
      else if(binaryArray[currentIndex] >= x){
             if(binaryArray[currentIndex] ==x){
                 std::cout << "ERROR! -- Repeating element"<<std::endl;
                 break;
              }
              else{
                 std::cout <<" Left ";
                 currentIndex = (2*currentIndex +1);
         }
      }
   }
}
void BinaryTree::searchElement(int x){
   int currentIndex = 0;
   while(true){
      if(binaryArray[currentIndex] == NULL){
      std::cout << "Not Found" << std::endl;
      break;
      }
      if(binaryArray[currentIndex] == x){
      std::cout << "Found at index: " <<currentIndex <<std::endl;
      break;
      }else if(binaryArray[currentIndex] < x){
      currentIndex = (2* currentIndex +2);
      }else if(binaryArray[currentIndex > x]){
      currentIndex = (2* currentIndex +1);
       }
     }
  }
 void BinaryTree::parent(int x){
  while(x !=0){
      x = (x-1)/2;
     }
  }
  void BinaryTree::preOrder(int index){
     if(binaryArray[index] !=NULL){
     std::cout << binaryArray[index] << " " << std::endl;
     preOrder(2* index +1);
     preOrder(2 * index +2);
     }
    }
   void BinaryTree::postOrder(int index){
      if(binaryArray[index] !=NULL){
        postOrder(2 * index +1);
        postOrder(2 * index +2);
        std::cout << binaryArray[index] << " " << std::endl;
      }
     }
  void BinaryTree::displayRSR(int index){
    if(binaryArray[index] !=NULL){
       std::cout << binaryArray[index] << " " << std::endl;
       displayRSR(2 * index +2);
      }
     }
  void BinaryTree::displayLSR(int index){
     if(binaryArray[index] !=NULL){
     std::cout << binaryArray[index]<< " " << std::endl;
     displayLSR(2 * index +1);
    }
  }
int BinaryTree::treeLeafsCount(int index){
   if(binaryArray[index] ==NULL){
       return 1;
    }
  else{
     return treeLeafsCount(2*index+1) + treeLeafsCount(2*index+2);
     }
   }
void BinaryTree::displayLeafValues(int index){
    if(binaryArray[index] !=NULL){
    std::cout << binaryArray[index]<< " " << std::endl;
        displayLeafValues(index+16); //Does work but I still get the 70
        displayLeafValues(index+21);
        displayLeafValues(index+12);
        displayLeafValues(index+14);
  }
 }
int main(){

BinaryTree tree(13);

tree.insertElement(70);
tree.insertElement(50);
tree.insertElement(100);
tree.insertElement(30);
tree.insertElement(60);
tree.insertElement(80);
tree.insertElement(110);
tree.insertElement(20);
tree.insertElement(68);
tree.insertElement(90);
tree.insertElement(120);
tree.insertElement(25);
tree.insertElement(62);

std::cout <<"Building BST is completed. \n\n";
std::cout <<"Values of the Binary Search tree. \n\n";

//Pre-Order Traversal
std::cout <<"Pre-Order Traversal of the BST : \n\n";
tree.preOrder(0);

//Post-Order Traversal
std::cout << "Post-Order Traversal of the BST :\n\n";
tree.postOrder(0);

//All right sub root values
std::cout<<"Here are all right sub root values for the BST :\n\n";
tree.displayRSR(2);

//All left sub root values
std::cout<<"Here are all left sub root values for the BST :\n\n";
tree.displayLSR(1);

//Counting Number of Leafs .
std::cout<<"\n\nNumber of Leafs =   " <<tree.treeLeafsCount(3);

//Display the leaf values
std::cout<<"\n\nHere are the leaf values in the BST:\n\n";
tree.displayLeafValues(0);
}

1 个答案:

答案 0 :(得分:0)

我找到了我需要做的答案

  if(!binaryArray[2*index+1] && !binaryArray[2*index+2]){
    std::cout<<binaryArray[index] << " " << std::endl;
   }
  if(binaryArray[2*index+1]){
        displayLeafValues(2*index+1);
   }
  if(binaryArray[2*index+2]){
    displayLeafValues(2*index+2);
   }