java achieve binary tree, before, during and after the order recursive and non-recursive binary tree traversal and queries (Java)

Some basic knowledge of the tree:

Link: https: //pan.baidu.com/s/1g6ZxO_o44LUyW3fl-3Ve6g
extraction code: k5dx

Huffman binary tree and knowledge:

Link: https: //pan.baidu.com/s/1IjwG5BYStf2KC06FpXq4tw
extraction code: omm2

Construction of a binary tree:

package com.dn.binarytree;

import java.awt.List;
import java.util.*;


public class BinaryTree {
	private TreeNode  root = null;
	
	public BinaryTree(){
		root = new TreeNode(1, "A");
	}
	
	/**
	 * 构建二叉树
	 *         A
	 *     B       C
	 * D      E        F
	 */
	public void createBinaryTree(){
		TreeNode nodeB = new TreeNode(2, "B");
		TreeNode nodeC = new TreeNode(3, "C");
		TreeNode nodeD = new TreeNode(4, "D");
		TreeNode nodeE = new TreeNode(5, "E");
		TreeNode nodeF = new TreeNode(6, "F");
		root.leftChild = nodeB;
		root.rightChild = nodeC;
		nodeB.leftChild = nodeD;
		nodeB.rightChild = nodeE;
		nodeC.rightChild = nodeF;
	}
	
	/**
	 * 求二叉树的高度
	 * @author Administrator
	 *
	 */
	public int getHeight(){
		return getHeight(root);
	}
	
	private int getHeight(TreeNode node) {
		if(node == null){
			return 0;
		}else{
			int i = getHeight(node.leftChild);
			int j = getHeight(node.rightChild);
			return (i<j)?j+1:i+1;
		}
	}

	/**
	 * 获取二叉树的结点数
	 * @author Administrator
	 *
	 */
	public int getSize(){
		return getSize(root);
	}
	
	
	private int getSize(TreeNode node) {
		if(node == null){
			return 0;
		}else{
			return 1+getSize(node.leftChild)+getSize(node.rightChild);
		}
	}
	/*
	 * 根据前序遍历的结果来构建二叉树
	 * ABD##E##C#F##
	 */
	public void createBinaryPre(ArrayList<String> data) {
		
		createBinaryTree(data.size(),data);
	}

	private TreeNode createBinaryTree(int size, ArrayList<String> data) {
		if (data.size()==0) {
			return null;
		}
		String d=data.get(0);
		TreeNode node;
		int index=size-data.size();//数组的长度减去现在的数据长度就是索引的值
		if(d.equals("#")) {
			node=null;//如果取得的为#则置空
			data.remove(0);
			return node;
			
		}
		node=new TreeNode(index,d);
		if(index==0) {
			root=node;
		}
		
			data.remove(0);
			node.leftChild=createBinaryTree(size,data);
			node.rightChild=createBinaryTree(size,data);
		
		return node;
		
	}

	/**
	 * 前序遍历——迭代
	 * @author Administrator
	 *
	 */
	public void preOrder(TreeNode node){
		if(node == null){
			return;
		}else{
			System.out.println("preOrder data:"+node.getData());
			preOrder(node.leftChild);
			preOrder(node.rightChild);
		}
	}
	
	/**
	 * 前序遍历——非迭代
	 */
	
	public void nonRecOrder(TreeNode node){
		if(node == null){
			return;
		}
		Stack<TreeNode> stack = new Stack<TreeNode>();
		stack.push(node);
		while(!stack.isEmpty()){
			//出栈和进栈
			TreeNode n = stack.pop();//弹出根结点
			//压入子结点
			System.out.println("nonRecOrder data"+n.getData());
			if(n.rightChild!=null){
				stack.push(n.rightChild);
				
			}
			if(n.leftChild!=null){
				stack.push(n.leftChild);
			}
		}
	}
	/*
	 * 中序遍历——迭代
	 * @author Administrator
	 *
	 */
	public void midOrder(TreeNode node){
		if(node == null){
			return;
		}else{
			midOrder(node.leftChild);
			System.out.println("midOrder data:"+node.getData());
			midOrder(node.rightChild);
		}
	}
	
	/**
	 * 后序遍历——迭代
	 * @author Administrator
	 *
	 */
	public void postOrder(TreeNode node){
		if(node == null){
			return;
		}else{
			postOrder(node.leftChild);
			postOrder(node.rightChild);
			System.out.println("postOrder data:"+node.getData());
		}
	}
	public class TreeNode{
		private int index;
		private String data;
		private TreeNode leftChild;
		private TreeNode rightChild;
		
	
		public int getIndex() {
			return index;
		}


		public void setIndex(int index) {
			this.index = index;
		}


		public String getData() {
			return data;
		}


		public void setData(String data) {
			this.data = data;
		}


		public TreeNode(int index,String data){
			this.index = index;
			this.data = data;
			this.leftChild = null;
			this.rightChild = null;
		}
	}
	
	
	public static void main(String[] args){
		BinaryTree binaryTree = new BinaryTree();
		binaryTree.createBinaryTree();
		int height = binaryTree.getHeight();
		System.out.println("treeHeihgt:"+height);
		int size = binaryTree.getSize();
		System.out.println("treeSize:"+size);
//		binaryTree.preOrder(binaryTree.root);
//		binaryTree.midOrder(binaryTree.root);
//		binaryTree.postOrder(binaryTree.root);
		binaryTree.nonRecOrder(binaryTree.root);
		System.out.println("通过前序遍历的数组来创建二叉树:");
		ArrayList<String> data=new ArrayList<>();
		String [] dataArray=new String[]{"A","B","D","#","#","E","#","#","F","#","#"};
		for(String d:dataArray) {
			data.add(d);
		}
		binaryTree.createBinaryPre(data);
		binaryTree.preOrder(binaryTree.root);
	}
}

Print Results:
treeHeihgt:. 3
TreeSize:. 6
nonRecOrder dataA
nonRecOrder dataB
nonRecOrder dataD
nonRecOrder DataE
nonRecOrder dataC
nonRecOrder DATAf
ago preorder traversal of the array to create a binary tree:
preOrder Data: A
preOrder Data: B
preOrder Data: D
preOrder Data: E
preOrder Data: F

Code analysis:

(1) Since then the build tree, you need to connect one node to complete, so we need to build a BinaryTree, which contains the index, data, left child and right child.
(2) Here we need a root root node
(3) createBinaryTree used to create the tree, which is connected to each node,
(4) For the binary tree height, we just need to focus on the distance between the root and leaf nodes, with recursive and +1 to count the height
(5) to find the number of binary tree nodes, 1 + getSize (node.leftChild) + getSize (node.rightChild), is achieved by recursively + 1,
(6) for traversing the binary tree, here introduce the preamble binary tree, sequence and subsequent empathy. For preorder traversal, we only need to print first node, and then call the left child, right child finally can be implemented with a recursive
(7) non-iterative method you need to use the knowledge of the stack, last-out
(8) According to array structure out of a preorder traversal, to restore the binary tree.

Find a binary tree

1, find the concept of a binary tree

Find a binary tree, also known as binary sort tree (Binary Sort Tree). Finding a binary tree or a blank, or the following recursive conditions are satisfied:
  (1) the left and right subtree of the search tree are each a search tree;
  (2) If the left subtree of the search tree is not empty, then its left child each tree node value is less than the value of the root node;
  (3) if the right subtree of the search tree is not empty, each of its node value of the right subtree of the root node is greater than the value.
----------------
Disclaimer: This article is CSDN blogger " 'logged out'" original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/fightfaith/article/details/50639010

Queries binary code to achieve:

package com.dn.binarytree;

public class SearchBinaryTree {
	TreeNode root=null;
	public void searchBinaryTree() {
		
	}
	/*
	 * 中序遍历
	 */
	public void midOrder(TreeNode node) {
		if(node==null) {
			return ;
		}else {
			midOrder(node.leftchild);
			System.out.println(node.data);
			midOrder(node.rightchid);
		}
	}//通过中序刚好可以从小到大打印出来
	/*
	 * 创建二叉树,添加节点
	 */
	public TreeNode put(int data) {
		TreeNode node=null;
		TreeNode parent=null;
		if(root==null) {
			node=new TreeNode(0,data);
			node=root;
			return node;
		}
		node=root;
		while(node!=null) {
			parent=node;
			if(data>node.data) {
				node=node.rightchid;
						
			}else if(data<node.data){
				node=node.leftchild;
			}else {
				return node;
			}
		}
		//将此节点添加到相应位置
		node=new TreeNode(0,data);
		if(data<parent.data) {
			parent.leftchild=node;
		}else {
			parent.rightchid=node;
		}
		node.parent=parent;
		return node;
		}
	class TreeNode{
		public TreeNode(int key, int data) {
			super();
			this.key = key;
			this.data = data;
			this.leftchild=null;
			this.rightchid=null;
		}
		public int getKey() {
			return key;
		}
		public void setKey(int key) {
			this.key = key;
		}
		public TreeNode getLeftchild() {
			return leftchild;
		}
		public void setLeftchild(TreeNode leftchild) {
			this.leftchild = leftchild;
		}
		public TreeNode getRightchid() {
			return rightchid;
		}
		public void setRightchid(TreeNode rightchid) {
			this.rightchid = rightchid;
		}
		public int getData() {
			return data;
		}
		public void setData(int data) {
			this.data = data;
		}
		private int key;
		private TreeNode leftchild;
		private TreeNode rightchid;
		private TreeNode parent;
		private int data;
	}
public  static void main(String avgs[]) {
	SearchBinaryTree tree=new SearchBinaryTree();
	int [] intArray=new int[] {50,30,20,44,87,16,7,77};
	for(int i:intArray) {
	tree.put(i);
	}
	
}
}

Published 63 original articles · won praise 12 · views 4043

Guess you like

Origin blog.csdn.net/qq_45353823/article/details/104253926