js 实现二叉树增删查

function Node(data,left,right){
    this.data = data;
    this.left = left;
    this.right = right;
}
function BST(){
    this.root = null;
    this.insert = insert;
    this.find = find;
    this.remove = remove;
}
/*
* 二叉树增加节点
*/
function insert(data){
    var node = new Node(data,null,null);
    if(this.root == null){
        this.root = node;
    }else{
        var current = this.root;
        while(true){
            if(current.data > data){
                if(current.left === null){
                    current.left = node;
                    break;
                }
                current = current.left;
            }else{
                if(current.right === null){
                    current.right = node;
                    break;
                }
                current = current.right;
            }
        }
    }
}
/*
* 二叉树查节点
*/
function find(data){
    var current = this.root;
    while(true){
        if(data === current.data){
            return current;
        }
        current = data < current.data ? current.left : current.right;
        if(current === null){
            return null;
        }
    }
}
/*
* 二叉树删除节点 (只删除叶子节点、只有一个子节点的Node
*/
function remove(data){
    this.root = removeNode(this.root,data);
}
function removeNode(node,data){
    if(node === null){
        return null;
    }
    if(data === node.data){
        if(node.left === null && node.right === null){
            return null;
        }
        if(node.left === null){
            return node.right;
        }
        if(node.right === null){
            return node.left;
        }
    }else if(data < node.data){
        node.left = removeNode(node.left,data);
        return node;
    }else{
        node.right = removeNode(node.right,data);
        return node;
    }
}
var bst = new BST();
bst.insert(5);
bst.insert(3);
bst.insert(7);
bst.insert(2);
bst.remove(2);

       二叉树的缺点:如果节点要频繁删除的话,不适合使用二叉树,特别是被删除的节点有两个子节点的;
  二叉树的优点:查找无规律的数字或者字母的数据结构适合二叉树的使用场景!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是二叉树增删的C语言代码示例。 首先,我们需要定义二叉树的结构体: ```c struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; ``` 接下来,我们分别实现二叉树的增、删、改、操作: 1. 增加节点 ```c struct TreeNode* insertNode(struct TreeNode* root, int val) { if (!root) { struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode)); node->val = val; node->left = NULL; node->right = NULL; return node; } if (val < root->val) { root->left = insertNode(root->left, val); } else if (val > root->val) { root->right = insertNode(root->right, val); } return root; } ``` 2. 删除节点 ```c struct TreeNode* deleteNode(struct TreeNode* root, int key) { if (!root) { return NULL; } if (root->val == key) { if (!root->left) { struct TreeNode* tmp = root->right; free(root); return tmp; } else if (!root->right) { struct TreeNode* tmp = root->left; free(root); return tmp; } else { struct TreeNode* p = root->right; while (p->left) { p = p->left; } root->val = p->val; root->right = deleteNode(root->right, p->val); } } else if (root->val > key) { root->left = deleteNode(root->left, key); } else { root->right = deleteNode(root->right, key); } return root; } ``` 3. 修改节点 ```c struct TreeNode* modifyNode(struct TreeNode* root, int oldVal, int newVal) { root = deleteNode(root, oldVal); root = insertNode(root, newVal); return root; } ``` 4. 找节点 ```c struct TreeNode* searchNode(struct TreeNode* root, int val) { if (!root || root->val == val) { return root; } if (root->val > val) { return searchNode(root->left, val); } else { return searchNode(root->right, val); } } ``` 以上就是二叉树增删的C语言代码示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值