Untitled
unknown
c_cpp
2 years ago
1.0 kB
11
Indexable
#include<iostream>
#include<vector>
using namespace std;
class BinaryTree {
private:
vector<int> treearray;
public:
BinaryTree() {}
void insert(int value) {
treearray.push_back(value);
}
void displaypostorder(int index=0) {
if(index<treearray.size()) {
displaypostorder(2*index + 1);
displaypostorder(2*index + 2);
cout<<treearray[index]<<" ";
}
}
void displayinorder(int index=0) {
if(index<treearray.size()) {
displayinorder(2*index + 1);
cout<<treearray[index]<<" ";
displayinorder(2*index + 2);
}
}
};
int main () {
BinaryTree binaryTree;
binaryTree.insert(40);
binaryTree.insert(20);
binaryTree.insert(30);
binaryTree.insert(4);
binaryTree.insert(5);
cout<<" Binary Tree (Post-order): ";
binaryTree.displaypostorder();
cout<<endl;
cout<<" Binary Tree (In-order): ";
binaryTree.displayinorder();
cout<<endl;
return 0;
}
Editor is loading...
Leave a Comment