zoukankan      html  css  js  c++  java
  • 二叉树

    package binarytree;
    
    
    public class BinaryTree {
    
        private Node root;
    
        public void add(int data) {
            if (root == null) {
                root = new Node(data);
            } else {
                root.addNode(data);
            }
        }
    
        public void print() {
            root.printNode();
        }
    
        private class Node {
            private int data;
            private Node left;
            private Node right;
    
            public Node(int data) {
                this.data = data;
            }
    
            public void addNode(int data) {
                if (this.data > data) {
                    if (this.left == null) {
                        this.left = new Node(data);
                    } else {
                        this.left.addNode(data);
                    }
                } else {
                    if (this.right == null) {
                        this.right = new Node(data);
                    } else {
                        this.right.addNode(data);
                    }
                }
            }
    
            //中序遍历
            public void printNode() {
                if (this.left != null) {
                    this.left.printNode();
                }
                System.out.print(this.data + "-->");
                if (this.right != null) {
                    this.right.printNode();
                }
            }
        }
    
    
    }
  • 相关阅读:
    H
    G
    hdu1430魔板
    1104. Don’t Ask Woman about Her Age(数论)
    bellman_ford寻找平均权值最小的回路
    bellman_ford算法
    强联通块tarjan算法
    割点算法
    字符串的最小表示法
    扩展KMP
  • 原文地址:https://www.cnblogs.com/songfahzun/p/8711798.html
Copyright © 2011-2022 走看看