Java 二叉树:按升序打印数据
我是java的初学者。。我刚刚在线开始数据结构。 我想按升序打印我添加到二叉树中的值Java 二叉树:按升序打印数据,java,binary-tree,Java,Binary Tree,我是java的初学者。。我刚刚在线开始数据结构。 我想按升序打印我添加到二叉树中的值 List<Integer> treeValues = new ArrayList<Integer>(); List<Integer> treeToList(Node node){ if (node == null) return; printInorder(node.left); if(!node.isEmpty){
List<Integer> treeValues = new ArrayList<Integer>();
List<Integer> treeToList(Node node){
if (node == null)
return;
printInorder(node.left);
if(!node.isEmpty){
treeValues.add(node.value);
}
printInorder(node.right);
}
void sortedTree(Node node){
List<Integer> treeData = treeToList(node);
Collections.sort(treeData);
for(int i=0; i<treeData.size();i++ ){
System.out.println(treeData.get(i));
}
}
我创建了一个打印方法,并使用以下值进行了尝试:
9,5,2,8,3
它打印此输出并停止
2,3,8
节点必须是构造函数:
构造器1
建造师2
添加方法:
public void add(int值){
if(Objects.isNull(根)){
根=新节点(值);
root.isEmpty=false;
}
节点电流=根;
while(true){
如果(值<当前值){
if(当前.左.isEmpty){
current.left.prev=当前;
current=current.left;
当前值=当前值;
current.isEmpty=false;
current.left=新节点();
current.right=新节点();
打破
}否则{
current=current.left;
}
}否则{
if(当前.右.isEmpty){
current.right.prev=当前;
current=current.right;
当前值=当前值;
current.isEmpty=false;
current.left=新节点();
current.right=新节点();
打破
}否则{
current=current.right;
}
}
}
}
打印方法
ArrayList list=new ArrayList();
节点电流=根;while(true){
如果(!current.left.isEmpty){
如果(!list.contains(current.left)){
current=current.left;
继续;
}
}否则{
系统输出打印项次(当前值);
列表。添加(当前);
如果(!current.right.isEmpty&&!list.contains(current.right)){
current=current.right;
继续;
}
current=current.prev.prev;
}
要从BST打印数据,您需要进行顺序遍历。对于二进制搜索树(BST),顺序遍历以非递减顺序提供节点。要以非递增顺序获取BST的节点,可以使用顺序遍历反转的顺序遍历的一种变体
算法顺序(树)
1.遍历左子树,即调用顺序(左子树)
2.访问根。
3.遍历右子树,即调用顺序(右子树)
时间复杂度:O(n)
如果树不是BST。您可以创建列表,将树的值写入列表,并按升序对列表进行排序
List<Integer> treeValues = new ArrayList<Integer>();
List<Integer> treeToList(Node node){
if (node == null)
return;
printInorder(node.left);
if(!node.isEmpty){
treeValues.add(node.value);
}
printInorder(node.right);
}
void sortedTree(Node node){
List<Integer> treeData = treeToList(node);
Collections.sort(treeData);
for(int i=0; i<treeData.size();i++ ){
System.out.println(treeData.get(i));
}
}
List treeValues=new ArrayList();
列表树列表(节点){
if(node==null)
返回;
printInorder(node.left);
如果(!node.isEmpty){
treeValues.add(node.value);
}
printInorder(node.right);
}
void sortedTree(节点){
列表树数据=树列表(节点);
集合。排序(treeData);
对于(int i=0;iYou应该按顺序遍历并按顺序打印数据。还要确保树是BST。这很好,但存在一个问题,它会将每个节点打印两次,我认为这对于正常的按顺序遍历是正确的,但是OP的树还需要一件事,它有不应该打印的空叶节点。它应该选中node.isEmpty
在打印之前,仅当节点不为空时才打印。此外,该值被称为value
,而不是key
@MhdGhd。它不会打印两次。您可以检查它,如果树不是二元搜索树,则可以进行遍历(按序、按前序、按后序)并将数据写入列表,然后对列表进行排序。@DavidConrad感谢您的评论,我做了一些更改。@lolo非常感谢它的工作,但它有一个小问题,它只打印根两次,其余的都很完美
public void add(int value) {
if (Objects.isNull(root)) {
root = new Node(value);
root.isEmpty = false;
}
Node current = root;
while (true) {
if (value < current.Value) {
if (current.left.isEmpty) {
current.left.prev = current;
current = current.left;
current.Value = value;
current.isEmpty = false;
current.left = new Node();
current.right = new Node();
break;
} else {
current = current.left;
}
} else {
if (current.right.isEmpty) {
current.right.prev = current;
current = current.right;
current.Value = value;
current.isEmpty = false;
current.left = new Node();
current.right = new Node();
break;
} else {
current = current.right;
}
}
}
}
ArrayList<Node> list = new ArrayList();
Node current = root;while(true){
if(!current.left.isEmpty ){
if(!list.contains(current.left)){
current=current.left;
continue;
}
} else {
System.out.println(current.Value);
list.add(current);
if(!current.right.isEmpty && !list.contains(current.right)){
current=current.right;
continue;
}
current=current.prev.prev;
}
/* Given a binary tree, print its nodes in inorder*/
void printInorder(Node node)
{
if (node == null)
return;
/* first recur on left child */
printInorder(node.left);
/* then print the data of node */
if(!node.isEmpty){
System.out.print(node.value+ " ");
}
/* now recur on right child */
printInorder(node.right);
}
List<Integer> treeValues = new ArrayList<Integer>();
List<Integer> treeToList(Node node){
if (node == null)
return;
printInorder(node.left);
if(!node.isEmpty){
treeValues.add(node.value);
}
printInorder(node.right);
}
void sortedTree(Node node){
List<Integer> treeData = treeToList(node);
Collections.sort(treeData);
for(int i=0; i<treeData.size();i++ ){
System.out.println(treeData.get(i));
}
}