请有人告诉我如何在二叉树中找到或不存在值? 我想在二叉树的左或右节点中找到值?
BinarySearchTree.prototype = {
//more code
contains: function(value){
var found = false,
current = this._root
//make sure there's a node to search
while(!found && current){
//if the value is less than the current node's, go left
if (value < current.value){
current = current.left;
//if the value is greater than the current node's, go right
} else if (value > current.value){
current = current.right;
//values are equal, found it!
} else {
found = true;
}
}
//only proceed if the node was found
return found;
}
}
答案 0 :(得分:0)
我建议使用递归方法,而不是使用import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
* @see https://stackoverflow.com/a/37293513/230513
*/
public class SimTest {
private void display() {
JFrame f = new JFrame("SimTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Simulation(8));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static class Simulation extends JPanel {
private final Font font = this.getFont().deriveFont(36f);
private int count;
private int data;
Timer t = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
repaint();
if ((data += 100) == 1000) {
t.stop();
System.out.println("Saving results…");//generateCsvFile();
if (--count == 0) {
System.out.println("Fin!");
System.exit(0);
} else {
init();
t.restart();
}
}
}
});
public Simulation(int count) {
this.count = count;
init();
t.start();
}
private void init() {
data = 0;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setFont(font);
g.drawString(String.valueOf(data), 8, g.getFontMetrics().getHeight());
}
@Override
public Dimension getPreferredSize() {
return new Dimension(256, 128);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new SimTest()::display);
}
}
进行循环。
while
此函数将返回具有搜索值的节点,如果您只想检查具有特定值的节点是否存在,只需使用function searchBST(rootNode, val) {
if (rootNode.key === null)
return null;
var nodeKey = parseInt(rootNode.val);
if (val < nodeKey) {
return searchBST(rootNode.left, val);
} else if (val > nodeKey) {
return searchBST(rootNode.right, val);
} else {
return rootNode.value;
}
}
和false
<编辑返回值/ p>
答案 1 :(得分:0)
如果在树中找到了值,您可以离开该函数。
function Node(value) {
this.value = value;
// this.left = null;
// this.right = null;
}
function BinarySearchTree() {
this._root = null;
}
BinarySearchTree.prototype = {
insert: function (value) {
var node = this,
side = '_root';
while (node[side]) {
node = node[side];
if (value === node.value) {
return;
}
side = value < node.value ? 'left' : 'right';
}
node[side] = new Node(value);
},
contains: function (value) {
var current = this._root
while (current) {
document.write('value: ' + current.value + '<br>');
if (value === current.value) {
return true; // leave the function
}
current = value < current.value ? current.left : current.right;
}
return false;
},
};
var tree = new BinarySearchTree(),
i;
for (i = 0; i < 10; i++) {
tree.insert(Math.floor(Math.random() * 1000));
}
document.write(tree.contains(42) + '<br>');
tree.insert(42);
document.write(tree.contains(42) + '<br>');
document.write('<pre>' + JSON.stringify(tree, 0, 4) + '</pre>');