|
3 | 3 | using namespace std;
|
4 | 4 |
|
5 | 5 | // Create a class for the BinaryTree
|
6 |
| -class BinaryTree |
7 |
| -{ |
8 |
| - // Create a struct for the TreeNode |
9 |
| - struct TreeNode |
10 |
| - { |
11 |
| - // Variables for the TreeNode |
12 |
| - int data; |
13 |
| - TreeNode* left; |
14 |
| - TreeNode* right; |
15 |
| - |
16 |
| - // Constructor for the TreeNode |
17 |
| - TreeNode(int value) : data(value), left(nullptr), right(nullptr) {} |
18 |
| - }; |
19 |
| - |
20 |
| - // Private Variables and Functions |
| 6 | +class BinaryTree { |
| 7 | + // Create a struct for the TreeNode |
| 8 | + struct TreeNode { |
| 9 | + // Variables for the TreeNode |
| 10 | + int data; |
| 11 | + TreeNode *left; |
| 12 | + TreeNode *right; |
| 13 | + |
| 14 | + // Constructor for the TreeNode |
| 15 | + TreeNode(int value) : data(value), left(nullptr), right(nullptr) {} |
| 16 | + }; |
| 17 | + |
| 18 | + // Private Variables and Functions |
21 | 19 | private:
|
22 |
| - TreeNode* root; |
23 |
| - |
24 |
| - //Insert Function |
25 |
| - TreeNode* insert(TreeNode* root, int value) |
26 |
| - { |
27 |
| - if (root == nullptr) |
28 |
| - return new TreeNode(value); |
29 |
| - |
30 |
| - if (value < root->data) |
31 |
| - root->left = insert(root->left, value); |
32 |
| - else |
33 |
| - root->right = insert(root->right, value); |
34 |
| - |
35 |
| - return root; |
36 |
| - } |
37 |
| - |
38 |
| - // Print Inorder Function |
39 |
| - void printInorder(TreeNode* head) |
40 |
| - { |
41 |
| - if (head != nullptr) |
42 |
| - { |
43 |
| - printInorder(head->left); |
44 |
| - cout << head->data << " "; |
45 |
| - printInorder(head->right); |
46 |
| - } |
| 20 | + TreeNode *root; |
| 21 | + |
| 22 | + // Insert Function |
| 23 | + TreeNode *insert(TreeNode *root, int value) { |
| 24 | + if (root == nullptr) |
| 25 | + return new TreeNode(value); |
| 26 | + |
| 27 | + if (value < root->data) |
| 28 | + root->left = insert(root->left, value); |
| 29 | + else |
| 30 | + root->right = insert(root->right, value); |
| 31 | + |
| 32 | + return root; |
| 33 | + } |
| 34 | + |
| 35 | + // Print Inorder Function |
| 36 | + void printInorder(TreeNode *head) { |
| 37 | + if (head != nullptr) { |
| 38 | + printInorder(head->left); |
| 39 | + cout << head->data << " "; |
| 40 | + printInorder(head->right); |
47 | 41 | }
|
48 |
| - |
49 |
| - // Print Preorder Function |
50 |
| - void printPreorder(TreeNode* head) |
51 |
| - { |
52 |
| - if (head != nullptr) |
53 |
| - { |
54 |
| - cout << head->data << " "; |
55 |
| - printPreorder(head->left); |
56 |
| - printPreorder(head->right); |
57 |
| - } |
| 42 | + } |
| 43 | + |
| 44 | + // Print Preorder Function |
| 45 | + void printPreorder(TreeNode *head) { |
| 46 | + if (head != nullptr) { |
| 47 | + cout << head->data << " "; |
| 48 | + printPreorder(head->left); |
| 49 | + printPreorder(head->right); |
58 | 50 | }
|
59 |
| - |
60 |
| - // Print Postorder Function |
61 |
| - void printPostorder(TreeNode* head) |
62 |
| - { |
63 |
| - if (head != nullptr) |
64 |
| - { |
65 |
| - printPostorder(head->left); |
66 |
| - printPostorder(head->right); |
67 |
| - cout << head->data << " "; |
68 |
| - } |
| 51 | + } |
| 52 | + |
| 53 | + // Print Postorder Function |
| 54 | + void printPostorder(TreeNode *head) { |
| 55 | + if (head != nullptr) { |
| 56 | + printPostorder(head->left); |
| 57 | + printPostorder(head->right); |
| 58 | + cout << head->data << " "; |
69 | 59 | }
|
| 60 | + } |
70 | 61 |
|
71 |
| - // Public Functions |
| 62 | + // Public Functions |
72 | 63 | public:
|
73 |
| - // Constructor |
74 |
| - BinaryTree() : root(nullptr) {} |
75 |
| - |
76 |
| - // Insert Function |
77 |
| - void insert(int value) |
78 |
| - { |
79 |
| - root = insert(root, value); |
80 |
| - } |
81 |
| - |
82 |
| - // Print Inorder Function |
83 |
| - void printInorder() |
84 |
| - { |
85 |
| - printInorder(root); |
86 |
| - cout << endl; |
87 |
| - } |
88 |
| - |
89 |
| - // Print Preorder Function |
90 |
| - void printPreorder() |
91 |
| - { |
92 |
| - printPreorder(root); |
93 |
| - cout << endl; |
94 |
| - } |
95 |
| - |
96 |
| - // Print Postorder Function |
97 |
| - void printPostorder() |
98 |
| - { |
99 |
| - printPostorder(root); |
100 |
| - cout << endl; |
101 |
| - } |
| 64 | + // Constructor |
| 65 | + BinaryTree() : root(nullptr) {} |
| 66 | + |
| 67 | + // Insert Function |
| 68 | + void insert(int value) { root = insert(root, value); } |
| 69 | + |
| 70 | + // Print Inorder Function |
| 71 | + void printInorder() { |
| 72 | + printInorder(root); |
| 73 | + cout << endl; |
| 74 | + } |
| 75 | + |
| 76 | + // Print Preorder Function |
| 77 | + void printPreorder() { |
| 78 | + printPreorder(root); |
| 79 | + cout << endl; |
| 80 | + } |
| 81 | + |
| 82 | + // Print Postorder Function |
| 83 | + void printPostorder() { |
| 84 | + printPostorder(root); |
| 85 | + cout << endl; |
| 86 | + } |
102 | 87 | };
|
103 | 88 |
|
104 |
| -int main() |
105 |
| -{ |
106 |
| - // Create tree |
107 |
| - BinaryTree binaryTree; |
| 89 | +int main() { |
| 90 | + // Create tree |
| 91 | + BinaryTree binaryTree; |
108 | 92 |
|
109 |
| - binaryTree.insert(10); |
110 |
| - binaryTree.insert(6); |
111 |
| - binaryTree.insert(15); |
112 |
| - binaryTree.insert(3); |
113 |
| - binaryTree.insert(8); |
114 |
| - binaryTree.insert(20); |
| 93 | + binaryTree.insert(10); |
| 94 | + binaryTree.insert(6); |
| 95 | + binaryTree.insert(15); |
| 96 | + binaryTree.insert(3); |
| 97 | + binaryTree.insert(8); |
| 98 | + binaryTree.insert(20); |
115 | 99 |
|
116 |
| - cout << "InOrder: "; |
117 |
| - binaryTree.printInorder(); |
| 100 | + cout << "InOrder: "; |
| 101 | + binaryTree.printInorder(); |
118 | 102 |
|
119 |
| - cout << "PreOrder: "; |
120 |
| - binaryTree.printPreorder(); |
| 103 | + cout << "PreOrder: "; |
| 104 | + binaryTree.printPreorder(); |
121 | 105 |
|
122 |
| - cout << "PostOrder: "; |
123 |
| - binaryTree.printPostorder(); |
| 106 | + cout << "PostOrder: "; |
| 107 | + binaryTree.printPostorder(); |
124 | 108 |
|
125 |
| - return 0; |
| 109 | + return 0; |
126 | 110 | }
|
0 commit comments