-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreetraversal.cpp
77 lines (55 loc) · 1.92 KB
/
treetraversal.cpp
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
73
74
75
76
77
#include <iostream>
using namespace std;
//two tree traversal algorithms
//BFS first seachred the width DFS starts with depth
//refer to video for visualization of how they work
//here we focus on DFS
//there are 3
//preorder, inrdoer, and postorder
//preorder, print data left child roiht child
//inorder left child print data right child
//postorder lieft child, right child, print data
struct Node{
int data;
Node* left; //left child
Node* right;//right child
};
Node* createNode(int data){
Node* newNode = new Node;
newNode -> data = data;
newNode -> left = newNode -> right = nullptr; //sets both as nullptr bec its the only node right nwo
return newNode; //whoever invokes teh function will recoeve a reference to the new Node
}
//its a recursive function , which means it will keep going until it hist the base case
//preorder
void printTree(Node* root){
if(root == nullptr) return; //base case
cout << root -> data << endl; //print data
printTree(root->left);//calls itself for the left child
printTree(root->right);//calls itself for the right child
}
void PrintTrees(Node* root){
if(root == nullptr) return; //base case
PrintTrees(root->left);
cout << root -> data << endl;
PrintTrees(root->right);
}
void PrintT(Node* root){
if(root == nullptr) return; //base case
PrintT(root->left);
PrintT(root->right);
cout << root -> data << endl;
}
int main(){
//each node has 3 parts, data, left child pointer, and right child pointer
Node* root = createNode(1);
root -> left = createNode(2);
root -> right = createNode(3);
root -> left -> left = createNode(4);
root -> left -> right = createNode(5);
root -> right -> right = createNode(6);
root -> right -> left = createNode(7);
root -> left -> right -> left = createNode(9);
root -> right -> right -> left = createNode(15);
printTree(root);
}