function BST(){ this.root = null this.insert = insert this.find = find this.mirror = mirror; } function Node(data,left,right){ this.data = data this.left = left this.right = right this.show = show } function show() { return this.data; } function mirror(root){ if(root == null){ return } if(root.left == null && root.right == null){ return } let temp = root.left; root.left = root.right; root.right = temp; mirror(root.left) mirror(root.right) } function insert(data){ var n = new Node(data,null,null) if(this.root == null){ this.root = n }else{ var current = this.root while(true){ if(data < current.data){ if(current.left == null){ current.left = n break } current = current.left }else{ if(current.right == null){ current.right = n 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 } } } function inorder(node){ if(!(node == null)){ inorder(node.left) console.log(node.show()) inorder(node.right) } } var nums = new BST() nums.insert(23) nums.insert(22) nums.insert(16) nums.insert(5) nums.insert(3) nums.insert(99) nums.insert(22) inorder(nums.root) mirror(nums.root) console.log(nums.root)
很简单的 看就完了。
声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com