-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTreeEveryOrder.cpp
More file actions
72 lines (67 loc) · 1.52 KB
/
binaryTreeEveryOrder.cpp
File metadata and controls
72 lines (67 loc) · 1.52 KB
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
71
72
#include <iostream>
using namespace std;
static int count = 0;
struct bin
{
int data;
bin *left;
bin *right;
};
struct bin *createNode(int data)
{
struct bin *rootNode;
rootNode = new struct bin;
if (rootNode)
{
rootNode->data = data;
rootNode->left = NULL;
rootNode->right = NULL;
return rootNode;
}
}
void preOrder(struct bin *rootNode)
{
if (rootNode)
{
cout << rootNode->data << " ";
//cin possible
preOrder(rootNode->left);
preOrder(rootNode->right);
}
}
void inOrder(struct bin *rootNode)
{
if (rootNode)
{
inOrder(rootNode->left);
cout << rootNode->data << " ";
inOrder(rootNode->right);
}
}
void postOrder(struct bin *rootNode)
{
if (rootNode)
{
postOrder(rootNode->left);
postOrder(rootNode->right);
cout << rootNode->data << " ";
}
}
int main()
{
struct bin *rootNode;
rootNode = createNode(1);
rootNode->left = createNode(2);
rootNode->right = createNode(3);
rootNode->left->left=createNode(4);
rootNode->left->right=createNode(5);
rootNode->right->left=createNode(6);
rootNode->right->right=createNode(7);
cout<<"Preorder traversal Binary Tree:";
preOrder(rootNode);
cout<<endl;
cout<<"Inorder traversal Binary Tree:";
inOrder(rootNode); cout<<endl;
cout<<"Postorder traversal Binary Tree:";
postOrder(rootNode); cout<<endl;
}