普通二叉树实现

package shu;
package shu;

public class myNode {
	Node root;

	public myNode(Node root) {
		super();
		this.root = root;
	}
//判空
	public boolean isEmpty() {
		return root == null;
	}

	
	private void pre(Node root) {
		if (root != null) {
			// 输出root
			System.out.print(root.data+" ");
			// 输出left
			pre(root.left);
			// 输出right
			pre(root.right);
		}
		
	}
	
	public void pre(){
		System.out.println("先序遍历:");
		pre(root);
	}

	
	private int height(Node root) {
		if (root == null) {
			return 0;
		} else {
			int lh = this.height(root.left);
			int rh = this.height(root.right);
			return lh > rh ? lh + 1 : rh + 1;
		}

	}
	public int height(){
		System.out.println();
		System.out.print("高度:");
		return height(this.root);
	}
	private int size(Node root){
		if(root == null){return 0;}
		else{
			int ls = size(root.left);
			int rs = size(root.right);
			return ls+rs+1;
		}
	}
	
	public int size(){
		System.out.println();
		System.out.print("节点共有:");
		return size(root);
	}

}



package shu;

public class Node {
	Node left;
	Object data;
	Node right;
	public Node(Node left, Object data, Node right) {
		super();
		this.left = left;
		this.data = data;
		this.right = right;
	}
	
	

}

 package shu;

public class te {
	public static void main(String[] args) {
		Node n5 = new Node(null, 1, null);
		Node n4 = new Node(null, 2, null);
		Node n3 = new Node(null, 3, null);
		Node n2 = new Node(n4, 4, n5);
		Node n1 = new Node(n2, 4, n3);
		myNode m = new myNode(n1);

		System.out.println(m.isEmpty());
		m.pre();
		System.out.println(m.size());
		System.out.println(m.height());
	}

}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值