Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不使用集合类的二叉搜索树迭代器实现_Java_Algorithm_Iterator_Binary Search Tree - Fatal编程技术网

Java 不使用集合类的二叉搜索树迭代器实现

Java 不使用集合类的二叉搜索树迭代器实现,java,algorithm,iterator,binary-search-tree,Java,Algorithm,Iterator,Binary Search Tree,我很难理解如何实现二进制搜索迭代器。 我的问题是如何在不使用集合类的情况下实现遍历“按顺序”的迭代器 问题是,我不知道如何添加“parent”,因为我在迭代器中找到了前4个项,但我似乎无法向上,因为“parent”要么抛出空指针,要么没有得到正确的项 那么如何添加“家长” void添加(字符串){ if(字符串比较(值)

我很难理解如何实现二进制搜索迭代器。 我的问题是如何在不使用集合类的情况下实现遍历“按顺序”的迭代器

问题是,我不知道如何添加“parent”,因为我在迭代器中找到了前4个项,但我似乎无法向上,因为“parent”要么抛出空指针,要么没有得到正确的项

那么如何添加“家长”

void添加(字符串){
if(字符串比较(值)<0){
if(left.left==null){
left.parent=左;
}
if(left==null){
大小++;
左=新节点(字符串);
如果。。。

谢谢。

我建议采用三类设计:

  • BinarySearchTree:公共类,它表示一个二进制搜索树。它包含作为BinarySearchTreeNode的树根
  • BinaryTreeNode:私有嵌套类,它表示一个节点,它有键和引用:子节点和父节点
  • BinarySearchTreeIterator:私有嵌套类,它表示迭代器,它使用对BinarySearchTreeNode的引用来了解当前元素

    public class BinarySearchTree implements Iterable<String> {
    
        private BinaryTreeNode root = null;
        private int elements;
    
        @Override
        public Iterator<String> iterator() {
            return new BinarySearchTreeIterator(root);
        }
    
        private static class BinarySearchTreeIterator implements Iterator<String> {
    
            private BinaryTreeNode node;
    
            public BinarySearchTreeIterator(BinaryTreeNode node) {
                if (node != null) {
                    this.node = smallest(node);
                } else {
                    this.node = node;
                }
            }
    
            @Override
            public boolean hasNext() {
                return node != null;
            }
    
            private static BinaryTreeNode smallest(BinaryTreeNode n) {
                if (n.left != null) {
                    return smallest(n.left);
                } else {
                    return n;
                }
            }
    
            @Override
            public String next() {
                String result = node.key;
                if (node.right != null) {
                    node = smallest(node.right);
                } else {
                    while (node.parent != null && node.parent.left != node) {
                        node = node.parent;
                    }
                    node = node.parent;
                }
                return result;
            }
        }
    
        private static class BinaryTreeNode {
    
            private String key;
            private BinaryTreeNode parent;
            private BinaryTreeNode left;
            private BinaryTreeNode right;
    
            public BinaryTreeNode(String key) {
                this.key = key;
            }
        }
    
        public boolean insert(String key) {
            if (key == null) {
                return false;
            }
            int lastElements = elements;
            this.root = insert(key, root, null);
            return lastElements < elements;
        }
    
        private BinaryTreeNode insert(String key, BinaryTreeNode node, BinaryTreeNode parent) {
            BinaryTreeNode result = node;
            if (node == null) {
                result = new BinaryTreeNode(key);
                result.parent = parent;
                this.elements++;
            } else {
                int compare = key.compareTo(node.key);
                if (compare < 0) {
                    result.left = insert(key, node.left, node);
                } else if (compare > 0) {
                    result.right = insert(key, node.right, node);
                }
            }
            return result;
        }
    
        public static void main(String[] args) {
            BinarySearchTree tree = new BinarySearchTree();
            String[] strings = {"l", "f", "t", "c", "g", "p", "u"};
            for (String string : strings) {
                System.out.println("insert: '" + string + "' " + tree.insert(string));
            }
            System.out.println("--");
    
            for (String s : tree) {
                System.out.println(s);
            }
        }
    }
    

    我建议采用三级设计:

    • BinarySearchTree:公共类,它表示一个二进制搜索树。它包含作为BinarySearchTreeNode的树根
    • BinaryTreeNode:私有嵌套类,它表示一个节点,它有键和引用:子节点和父节点
    • BinarySearchTreeIterator:私有嵌套类,它表示迭代器,它使用对BinarySearchTreeNode的引用来了解当前元素

      public class BinarySearchTree implements Iterable<String> {
      
          private BinaryTreeNode root = null;
          private int elements;
      
          @Override
          public Iterator<String> iterator() {
              return new BinarySearchTreeIterator(root);
          }
      
          private static class BinarySearchTreeIterator implements Iterator<String> {
      
              private BinaryTreeNode node;
      
              public BinarySearchTreeIterator(BinaryTreeNode node) {
                  if (node != null) {
                      this.node = smallest(node);
                  } else {
                      this.node = node;
                  }
              }
      
              @Override
              public boolean hasNext() {
                  return node != null;
              }
      
              private static BinaryTreeNode smallest(BinaryTreeNode n) {
                  if (n.left != null) {
                      return smallest(n.left);
                  } else {
                      return n;
                  }
              }
      
              @Override
              public String next() {
                  String result = node.key;
                  if (node.right != null) {
                      node = smallest(node.right);
                  } else {
                      while (node.parent != null && node.parent.left != node) {
                          node = node.parent;
                      }
                      node = node.parent;
                  }
                  return result;
              }
          }
      
          private static class BinaryTreeNode {
      
              private String key;
              private BinaryTreeNode parent;
              private BinaryTreeNode left;
              private BinaryTreeNode right;
      
              public BinaryTreeNode(String key) {
                  this.key = key;
              }
          }
      
          public boolean insert(String key) {
              if (key == null) {
                  return false;
              }
              int lastElements = elements;
              this.root = insert(key, root, null);
              return lastElements < elements;
          }
      
          private BinaryTreeNode insert(String key, BinaryTreeNode node, BinaryTreeNode parent) {
              BinaryTreeNode result = node;
              if (node == null) {
                  result = new BinaryTreeNode(key);
                  result.parent = parent;
                  this.elements++;
              } else {
                  int compare = key.compareTo(node.key);
                  if (compare < 0) {
                      result.left = insert(key, node.left, node);
                  } else if (compare > 0) {
                      result.right = insert(key, node.right, node);
                  }
              }
              return result;
          }
      
          public static void main(String[] args) {
              BinarySearchTree tree = new BinarySearchTree();
              String[] strings = {"l", "f", "t", "c", "g", "p", "u"};
              for (String string : strings) {
                  System.out.println("insert: '" + string + "' " + tree.insert(string));
              }
              System.out.println("--");
      
              for (String s : tree) {
                  System.out.println(s);
              }
          }
      }
      

      你能提供一些你已经尝试过的信息吗?互联网上有很多关于二叉树遍历的通用算法的内容。例如[link[()你能提供一些你已经尝试过的信息吗?互联网上有很多关于二叉树遍历的通用算法的内容。例如[link[()
      insert: 'l' true
      insert: 'f' true
      insert: 't' true
      insert: 'c' true
      insert: 'g' true
      insert: 'p' true
      insert: 'u' true
      --
      c
      f
      g
      l
      p
      t
      u