-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeDataStructure
More file actions
84 lines (67 loc) · 1.12 KB
/
TreeDataStructure
File metadata and controls
84 lines (67 loc) · 1.12 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
73
74
75
76
77
78
79
80
81
82
83
// Trees.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
struct tnode {
int val;
tnode *lchild;
tnode *rchild;
};
tnode *root; //first node in a tree, similar to head of a linked list
tnode *myNewNode()
{
tnode *t;
t = new tnode;
t->lchild = NULL;
t->rchild = NULL;
return t;
}
void insert(tnode* c, int target)
{
//we need to have another pointer, a previous pointer, to keep track of the parent
tnode *p; //previous pointer
p = NULL;
while (c)
{
p = c;
if (target < c->val)
c = c->lchild;
else
c = c->rchild;
}
c = myNewNode();
c->val = target;
while (root != NULL)
{
if (target < p->val)
c = c->lchild;
else
c = c->rchild;
}
return;
}
void print(tnode *r)
{
if (r == NULL)
return;
print(r->lchild);
cout << r->val;
print(r->rchild);
return;
}
int main()
{
ifstream inFile;
inFile.open("C://Users//Thuy//Desktop//372 Data Structures//Trees//Trees//test.txt");
int num;
while (inFile)
{
inFile >> num;
insert(root, num);
}
print(root);
inFile.close();
return 0;
}