Binary sort tree implementation and basic operations
- 2020-05-26 08:24:19
- OfStack
A two-fork sort tree is also known as a two-fork search tree. It is either an empty tree or a two-fork tree with the following properties:
If the left subtree is not empty, then the value of all nodes in the left subtree is less than the value of its root node;
If the right subtree is not empty, then the value of all nodes in the right subtree is greater than the value of its root node;
The right and left subtrees are also 2 fork sort tree.
The following code is implemented:
Construction of a two-fork tree The middle, front, back, and sequence traversal of a two-fork tree The maximum distance of a node in a two-tree
import java.util.LinkedList;
import java.util.Queue;
class Node{
public int data;
public Node left;
public Node right;
public int leftMaxDistance;
public int rightMaxDistance;
public Node(int data){
this.data=data;
this.left=null;
this.right=null;
}
}
/**
* @author TY
* implementation 2 Cross sort tree, including insertion, middle order traversal, first order traversal, after order traversal, calculate the maximum distance of all nodes
*/
public class BinaryTree {
private Node root;
public BinaryTree(){
root=null;
}
public void insert(int data){
Node newNode=new Node(data);
if(root==null)
root=newNode;
else{
Node current=root;
Node parent;
while (true) {// Find the insertion position
parent=current;
if(data<current.data){
current=current.left;
if(current==null){
parent.left=newNode;
return;
}
}else{
current=current.right;
if (current==null) {
parent.right=newNode;
return;
}
}
}
}
}
// Input the values into the build 2 tree
public void buildTree(int[] data){
for (int i = 0; i < data.length; i++) {
insert(data[i]);
}
}
// Recursive implementation of the middle order traversal method
public void inOrder(Node localRoot){
if(localRoot!=null){
inOrder(localRoot.left);
System.out.print(localRoot.data+" ");
inOrder(localRoot.right);
}
}
public void inOrder(){
this.inOrder(this.root);
}
// Recursive implementation of the first order traversal method
public void preOrder(Node localRoot){
if(localRoot!=null){
System.out.print(localRoot.data+" ");
preOrder(localRoot.left);
preOrder(localRoot.right);
}
}
public void preOrder(){
this.preOrder(this.root);
}
// Recursive implementation of the sequential traversal method
public void postOrder(Node localRoot){
if(localRoot!=null){
postOrder(localRoot.left);
postOrder(localRoot.right);
System.out.print(localRoot.data+" ");
}
}
public void postOrder(){
this.postOrder(this.root);
}
/**
* Sequence traversal 2 Fork tree: now put the root into the queue and take it from the queue each time 1 Node prints the value of the node,
* If the node has children, it places its children at the end of the queue until the queue is empty
*/
public void layerTranverse(){
if(this.root==null)
return;
Queue<Node> q=new LinkedList<Node>();
q.add(this.root);
while(!q.isEmpty()){
Node n=q.poll();
System.out.print(n.data+" ");
if(n.left!=null)
q.add(n.left);
if(n.right!=null)
q.add(n.right);
}
}
private int maxLen=0;
private int max(int a,int b){
return a>b?a:b;
}
public void findMaxDistance(Node root){
if(root==null)
return;
if(root.left==null)
root.leftMaxDistance=0;
if(root.right==null)
root.rightMaxDistance=0;
if(root.left!=null)
findMaxDistance(root.left);
if(root.right!=null)
findMaxDistance(root.right);
// Calculate the maximum distance from the root in the left word tree
if(root.left!=null)
root.leftMaxDistance=max(root.left.leftMaxDistance, root.left.rightMaxDistance)+1;
// Calculate the maximum distance from the root in the right word tree
if(root.right!=null)
root.rightMaxDistance=max(root.right.leftMaxDistance, root.right.rightMaxDistance)+1;
// To obtain 2 The maximum distance of all nodes in the fork tree
if(root.leftMaxDistance+root.rightMaxDistance>maxLen){
maxLen=root.leftMaxDistance+root.rightMaxDistance;
}
}
public static void main(String[] args) {
BinaryTree biTree=new BinaryTree();
int[] data={2,8,7,4,9,3,1,6,7,5};
biTree.buildTree(data);
System.out.print("2 Middle order traversal of fork tree: ");
biTree.inOrder();
System.out.println();
System.out.print("2 The first order traversal of the fork tree: ");
biTree.preOrder();
System.out.println();
System.out.print("2 The sequential traversal of the fork tree: ");
biTree.postOrder();
System.out.println();
System.out.print("2 Sequence traversal of fork tree: ");
biTree.layerTranverse();
System.out.println();
biTree.findMaxDistance(biTree.root);
System.out.println("2 The maximum distance of nodes in the fork tree: "+biTree.maxLen);
}
}