---
# System prepended metadata

title: Programming style

---

# Programming style
## 解釋
```
bool Tree::insertNode(TreeType value) {
    int nodeHeight = 0;
    Node *newNode = new Node;
    if (newNode == NULL) {
        return false;
    }
    //初始化
    newNode->left = NULL;
    newNode->right = NULL;
    newNode->value = value;
    newNode->count = 1;
    newNode->parent = NULL;
    if (root == NULL) {
        root = newNode;
    } else {
        Node *currentNode = root;
        Node *parentNode = NULL;
        
        //走到適合的地方插入節點
        while (currentNode != NULL) {
            nodeHeight += 1;
            parentNode = currentNode;
            
            //判斷大小 並且放到合適的位置
            if (newNode->value < currentNode->value) {
                currentNode = currentNode->left;
            } else if (newNode->value > currentNode->value) {
                currentNode = currentNode->right;
            } else {
                currentNode->count += 1;
                return true;
            }
        }
        nodeCount += 1;
        newNode->parent = parentNode;
        
        //將節點接上
        if (newNode->value <= parentNode->value) {
            parentNode->left = newNode;
        } else {
            parentNode->right = newNode;
        }
    }
    if (nodeHeight > treeHeight) {
        treeHeight = nodeHeight;
    }
    return true;
}
```