Home > Back-end >  Java programming
Java programming

Time:09-26

Using Java implementation of a binary tree left and right subtrees exchange

CodePudding user response:

Binary tree node class
 
Public class BinaryTreeNode {
Private BinaryTreeNode left;
Private BinaryTreeNode right;

Private int data;

Public BinaryTreeNode (int data) {
super();
This data=https://bbs.csdn.net/topics/data;
}

Public BinaryTreeNode getLeft () {
Return the left;
}

Public void setLeft BinaryTreeNode (left) {
This. Left=left;
}

Public BinaryTreeNode getRight () {
Return the right;
}

Public void setRight BinaryTreeNode (right) {
This. Right=right;
}

Public int getData () {
return data;
}

Public void setData (int data) {
This data=https://bbs.csdn.net/topics/data;
}

Public void flip () {
If (left!=null) {
Left. Flip ();
}
If (right!=null) {
Right. Flip ();
}
BinaryTreeNode node=left;
This. Left=right;
This. Right=node;
}
}

Binary tree class
 
Public class BinaryTree {
Private BinaryTreeNode root;

Public BinaryTreeNode getRoot () {
return root;
}

Public void setRoot (BinaryTreeNode root) {
This. The root=root;
}

Public void flip () {
If (root!=null) {
Root. Flip ();
}
}
}

The test class
 
Public class TestBinaryTreeFlip {

Public static void main (String [] args) {
BinaryTreeNode root=new BinaryTreeNode (1);

BinaryTreeNode left=new BinaryTreeNode (2);
BinaryTreeNode left_left=new BinaryTreeNode (4);
BinaryTreeNode left_right=new BinaryTreeNode (5);

BinaryTreeNode right=new BinaryTreeNode (3);
BinaryTreeNode right_right=new BinaryTreeNode (6);

Left. SetLeft (left_left);
Left. SetRight (left_right);
Right. SetRight (right_right);
Root. SetLeft (left);
Root. SetRight (right);

BinaryTree tree=new BinaryTree ();
Tree. SetRoot (root);

Tree. Flip ();
System. The out. Println (" ");
}
}

CodePudding user response:

reference 1/f, three and a half cents response:
binary tree node class
 
Public class BinaryTreeNode {
Private BinaryTreeNode left;
Private BinaryTreeNode right;

Private int data;

Public BinaryTreeNode (int data) {
super();
This data=https://bbs.csdn.net/topics/data;
}

Public BinaryTreeNode getLeft () {
Return the left;
}

Public void setLeft BinaryTreeNode (left) {
This. Left=left;
}

Public BinaryTreeNode getRight () {
Return the right;
}

Public void setRight BinaryTreeNode (right) {
This. Right=right;
}

Public int getData () {
return data;
}

Public void setData (int data) {
This data=https://bbs.csdn.net/topics/data;
}

Public void flip () {
If (left!=null) {
Left. Flip ();
}
If (right!=null) {
Right. Flip ();
}
BinaryTreeNode node=left;
This. Left=right;
This. Right=node;
}
}

Binary tree class
 
Public class BinaryTree {
Private BinaryTreeNode root;

Public BinaryTreeNode getRoot () {
return root;
}

Public void setRoot (BinaryTreeNode root) {
This. The root=root;
}

Public void flip () {
If (root!=null) {
Root. Flip ();
}
}
}

The test class
 
Public class TestBinaryTreeFlip {

Public static void main (String [] args) {
BinaryTreeNode root=new BinaryTreeNode (1);

BinaryTreeNode left=new BinaryTreeNode (2);
BinaryTreeNode left_left=new BinaryTreeNode (4);
BinaryTreeNode left_right=new BinaryTreeNode (5);

BinaryTreeNode right=new BinaryTreeNode (3);
BinaryTreeNode right_right=new BinaryTreeNode (6);

Left. SetLeft (left_left);
Left. SetRight (left_right);
Right. SetRight (right_right);
Root. SetLeft (left);
Root. SetRight (right);

BinaryTree tree=new BinaryTree ();
Tree. SetRoot (root);

Tree. Flip ();
System. The out. Println (" ");
}
}

Thank you for your
Thank you very much, your answer is too full of

CodePudding user response:

So long, write a binary tree is not expensive 20 points, put between the nullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related