MCPcopy Create free account
hub / github.com/anupam-kumar-krishnan/Competitive_Programming / iInsert

Method iInsert

All Data Structures/TREE/BST.cpp:34–83  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

32};
33
34void BST::iInsert(int key)
35{
36
37 Node *t = root;
38 Node *p;
39 Node *r;
40
41 // root is empty
42 if (root == nullptr)
43 {
44 p = new Node;
45 p->data = key;
46 p->lchild = nullptr;
47 p->rchild = nullptr;
48 root = p;
49 return;
50 }
51
52 while (t != nullptr)
53 {
54 r = t;
55 if (key < t->data)
56 {
57 t = t->lchild;
58 }
59 else if (key > t->data)
60 {
61 t = t->rchild;
62 }
63 else
64 {
65 return;
66 }
67 }
68
69 // Now t points at NULL and r points at insert location
70 p = new Node;
71 p->data = key;
72 p->lchild = nullptr;
73 p->rchild = nullptr;
74
75 if (key < r->data)
76 {
77 r->lchild = p;
78 }
79 else
80 {
81 r->rchild = p;
82 }
83}
84
85void BST::Inorder(Node *p)
86{

Callers 1

mainFunction · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected