Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node {
- int data;
- struct Node *left, *right;
- };
- Node * newNode(int data) {
- Node * temp = new Node;
- temp->data = data;
- temp->left = NULL;
- temp->right = NULL;
- return temp;
- }
- // STANGA - RADACINA - DREAPTA (SRD sau inorder)
- void printInOrder(struct Node* node) {
- if (node == NULL) {
- return;
- }
- // prima oara mergem recursiv pe copilul din stanga
- printInOrder(node->left);
- // afisam ce contine nodul
- cout << node->data << " ";
- // mergem recursiv pe copilul din dreapta
- printInOrder(node->right);
- }
- // Stanga-Dreapta Radacina (SDR postorder)
- void printPostOrder(struct Node* node) {
- if (node == NULL) {
- return;
- }
- printPostOrder(node->left);
- printPostOrder(node->right);
- //display the data inside the node
- cout << node->data << " ";
- }
- // RADACINA -> STANGA -> DREAPTA (RSD)
- void printPreorder(struct Node* node) {
- if (node == NULL) {
- return;
- }
- cout << node->data << " ";
- printPreorder(node->left);
- printPreorder(node->right);
- }
- int main() {
- std::cout << "Hello, World!" << std::endl;
- struct Node * root = newNode(8);
- root->left = newNode(3);
- root->left->left= newNode(1);
- root->left->right = newNode(6);
- root->left->right->left = newNode(4);
- root->left->right->right = newNode(7);
- root->right = newNode(10);
- root->right->right = newNode(14);
- root->right->right->left = newNode(13);
- cout << "Postorder tree traverals (STANGA - DREAPTA - RADACINA )"<<endl;
- printPostOrder(root);
- cout << "-----------------------------------" << endl;
- cout << "Inorder tree traverals (STANGA - RADACINA - DREAPTA )"<<endl;
- printInOrder(root);
- cout << "-----------------------------------" << endl;
- cout << "Preorder tree traverals (RADACINA -STANGA - DREAPTA )"<<endl;
- printPreorder(root);
- return 0;
- }/**/
Advertisement
Add Comment
Please, Sign In to add comment