扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
一、概念
二叉搜索树也成二叉排序树,它有这么一个特点,某个节点,若其有两个子节点,则一定满足,左子节点值一定小于该节点值,右子节点值一定大于该节点值,对于非基本类型的比较,可以实现Comparator接口,在本文中为了方便,采用了int类型数据进行操作。
要想实现一颗二叉树,肯定得从它的增加说起,只有把树构建出来了,才能使用其他操作。
二、二叉搜索树构建
谈起二叉树的增加,肯定先得构建一个表示节点的类,该节点的类,有这么几个属性,节点的值,节点的父节点、左节点、右节点这四个属性,代码如下
static class Node{ Node parent; Node leftChild; Node rightChild; int val; public Node(Node parent, Node leftChild, Node rightChild,int val) { super(); this.parent = parent; this.leftChild = leftChild; this.rightChild = rightChild; this.val = val; } public Node(int val){ this(null,null,null,val); } public Node(Node node,int val){ this(node,null,null,val); } }
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流