MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / insertNode

Function insertNode

Trees/avltree.c:79–117  ·  view source on GitHub ↗

Insert node

Source from the content-addressed store, hash-verified

77
78// Insert node
79struct Node *insertNode(struct Node *node, int key)
80{
81 // Find the correct position to insertNode the node and insertNode it
82 if (node == NULL)
83 return (newNode(key));
84
85 if (key < node->key)
86 node->left = insertNode(node->left, key);
87 else if (key > node->key)
88 node->right = insertNode(node->right, key);
89 else
90 return node;
91
92 // Update the balance factor of each node and
93 // Balance the tree
94 node->height = 1 + max(height(node->left),
95 height(node->right));
96
97 int balance = getBalance(node);
98 if (balance > 1 && key < node->left->key)
99 return rightRotate(node);
100
101 if (balance < -1 && key > node->right->key)
102 return leftRotate(node);
103
104 if (balance > 1 && key > node->left->key)
105 {
106 node->left = leftRotate(node->left);
107 return rightRotate(node);
108 }
109
110 if (balance < -1 && key < node->right->key)
111 {
112 node->right = rightRotate(node->right);
113 return leftRotate(node);
114 }
115
116 return node;
117}
118
119struct Node *minValueNode(struct Node *node)
120{

Callers 1

mainFunction · 0.70

Calls 6

getBalanceFunction · 0.85
rightRotateFunction · 0.85
leftRotateFunction · 0.85
newNodeFunction · 0.70
maxFunction · 0.70
heightFunction · 0.70

Tested by

no test coverage detected