Overview and simple implementation of the tree structure of the basic algorithm series

A tree is an important non-linear data structure. Intuitively, it is a structure in which data elements (called nodes in the tree) are organized in a branch relationship.

Basic concepts:
as shown in the figure: (picture reference network)
Insert picture description here
root node: A is the root node;
parent node (parent node): B is the parent node of E/F;
node degree: node's number of branches, i.e., the number of sub-tree nodes point, node B, such as the following two sub-trees, the degree 2, empathy a node degree 3, i.e., the maximum node degree of a tree;
node Weight: refers to the weight of the
node ; leaf node: node without child nodes;
height: the maximum number of layers, for example, the height of the tree above is 4;
binary tree: the number of child nodes of any node <= 2. The child nodes are divided into left and right nodes. The left and right nodes cannot be reversed at will;
full binary tree: all leaf nodes are in the last layer, and the total number of nodes is 2 to the nth power -1;
complete Binary tree: All leaf nodes are in the last layer or the penultimate layer, and the leaf nodes of the last layer are continuous on the left, and the penultimate layer is continuous on the right; a
full binary tree must be a complete binary tree;
preorder traversal: oneself— Left tree-right tree ABEKLFCGDHMIJ
middle order traversal: left tree-oneself-right tree ~
post-order traversal: left tree-right tree-oneself~

Code:

public class TreeNode{
    
    	//结点类
	int value;
	TreeNode left;
	TreeNode right;
	public TreeNode(int value){
    
    
		this.value=value;
	}
	public void setlnode(TreeNode left){
    
    
		this.left=left;
	}
	public void setrnode(TreeNode right){
    
    
		this.right=right;
	}
	public void frontshow(){
    
    
		System.out.println(value);
		if(left!=null){
    
    
			left.frontshow();
		}
		f(right!=null){
    
    
			right.frontshow();
		}
	}
	public TreeNode frontSearch(int i){
    
    
		if(this.value==i){
    
    
			return this;
		}else{
    
    
			if(left!=null)
				TreeNode target=left.frontSearch(i);
			if(target!=null)
				return target;
			if(left!=null)
				TreeNode target=right.frontSearch(i);	
			return target;
		}
	}
	public void delete(int i){
    
    
		TreeNode parent=this;
		if(parent.left.value==i){
    
    
			parent.left=null;
			return;
		}
		if(parent.right.value==i){
    
    
			parent.right=null;
			return;
		}
		parent=left;
		if(parent!=null)
			parent.delete(i);
		parent=right;
		if(parent!=null)
			parent.delete(i);
	}
}

public class BinaryTree{
    
      //创建根结点
	TreeNode root;
	public void setRoot(TreeNode root){
    
    
		this.root=root;
	}
	public TreeNode getRoot(){
    
    
		return root;
	}
	public void frontshow(){
    
    
		root.frontshow();
	}
	public TreeNode frontSearch(int i){
    
    
		return root.frontSearch(i);
	}
	public void delete(int i){
    
    
		if(root.value==i)
			root=null;
		else
			root.delete(i);
	}
}

public class TestBinaryTree{
    
    
	public static void main(String[]args){
    
    
		BinaryTree binTree=new BinaryTree;  //空树
		TreeNode root=new TreeNode(1);  //根结点
		binTree.setRoot(root);
		TreeNode rootL=new TreeNode(2);
		root.setlnode(rootL);
		TreeNode rootR=new TreeNode(3);
		root.setrnode(rootR);
	}
}

Guess you like

Origin blog.csdn.net/langxiaolin/article/details/113790729