Data structure of the binary tree (1): ordinary binary tree
What is a binary tree:
Binary tree (Binary tree) refers to an ordered tree with nodes in the tree not greater than 2. It is the simplest and most important tree. The recursive definition of a binary tree is: a binary tree is an empty tree, or a non-empty tree consisting of a root node and two disjoint, left subtrees and right subtrees called the root; left subtree And the right subtree is the same binary tree.
As shown in the figure:
Related terms:
① Node: contains a data element and a number of information pointing to the branch of the subtree.
②The degree of the node: the number of subtrees a node has is called the degree of the node.
③Leaf nodes: also called terminal nodes, nodes without subtrees or nodes with zero degree.
④Branch node: also called non-terminal node, the node whose degree is not zero is called non-terminal node.
⑤ Tree degree: the maximum value of the degree of all nodes in the tree.
⑥ Node level: starting from the root node, suppose that the root node is the first level, and the child nodes of the root node are the second level, and so on. If a node is located at the Lth level, its child nodes Located on the L+1 floor.
⑦The depth of the tree: also called the height of the tree, the maximum level of all nodes in the tree is called the depth of the tree.
⑧ Ordered tree: If the order of the subtrees in the tree is sequential, then the tree is called an ordered tree.
⑨Unordered tree: If the order of the subtrees in the tree is not sequential, the tree is called an unordered tree.
⑩Forest: A forest is composed of m (m≥0) trees that do not intersect each other. If the root node of a non-empty tree is deleted, the tree becomes a forest, and the trees in the forest are composed of the subtrees of the original root node.
Special types:
1. Full binary tree: If a binary tree has only nodes with degree 0 and nodes with degree 2, and the nodes with degree 0 are on the same level, the binary tree is a full binary tree.
2. Complete binary tree: A binary tree with a depth of k and n nodes is called complete if and only if each node corresponds to a node numbered from 1 to n in a full binary tree of depth k. Binary tree.
The characteristic of a complete binary tree is that the leaf nodes can only appear on the two layers with the largest sequence, and the largest sequence of descendants under the left branch of a node is equal to or greater than the largest sequence of descendants under the right branch.
Binary tree properties :
Nature 1: There are at most 2i-1 (i≥1) nodes on the i-th level of the binary tree.
Property 2: The binary tree with depth h contains at most 2h-1 nodes.
Property 3: If in any binary tree, there are n0 leaf nodes and n2 nodes with degree 2, then there must be n0=n2+1.
Property 4: The depth of a complete binary tree with n nodes is log2x+1 (where x represents the largest integer not greater than n).
Property 5: If a complete binary tree with n nodes is numbered sequentially (1≤i≤n), then for the node numbered i (i≥1):
when i=1, the node is the root, It has no parent node.
When i>1, the number of the parent node of the node is i/2.
If 2i≤n, there is a left leaf numbered 2, otherwise there is no left leaf.
If 2+1≤n, then there is a right leaf numbered 2i+1, otherwise there is no right leaf.
Java implementation:
Below we try to use java code to implement a conventional binary tree.
In this binary tree, we add some data to each node of the tree, namely HeroNode, and each node stores the name of a hero.
In this binary tree, we define methods for it:
including three ways of traversing the tree in pre-order, middle-order, and post-order, and the way
of deleting tree nodes.
The structure of the tree is shown in the figure:
the output of pre-order, middle-order, and post-order traversal is shown in the figure:
code show as below:
package Tree;
public class BinaryTreeDemo {
public static void main(String[] args) {
BinaryTree binaryTree=new BinaryTree();
HeroNode root=new HeroNode("孙悟空");
HeroNode node1=new HeroNode("猪八戒");
HeroNode node2=new HeroNode("唐三藏");
HeroNode node3=new HeroNode("沙和尚");
HeroNode node4=new HeroNode("牛魔王");
HeroNode node5=new HeroNode("红孩儿");
HeroNode node6=new HeroNode("白骨精");
binaryTree.setRoot(root);
root.setLeftNode(node1);
root.setRightNode(node2);
node1.setLeftNode(node3);
node1.setRightNode(node4);
node2.setLeftNode(node5);
node2.setRightNode(node6);
System.out.println("=======前序遍历=======");
binaryTree.frontShow();
System.out.println("=======中序遍历=======");
binaryTree.middleShow();
System.out.println("=======后序遍历=======");
binaryTree.endShow();
System.out.println("=======删除树节点测试======");
binaryTree.deleteNode("唐三藏");
System.out.println("=======再次前序遍历查看结果=======");
binaryTree.frontShow();
}
}
class BinaryTree{
HeroNode root;
public void setRoot(HeroNode root) {
this.root=root;
}
public void frontShow() {
if(this.root!=null) {
this.root.frontShow();
}else {
System.out.println("二叉树为空,无法遍历");
}
}
public void middleShow() {
if(this.root!=null) {
this.root.middleShow();
}else {
System.out.println("二叉树为空,无法遍历");
}
}
public void endShow() {
if(this.root!=null) {
this.root.endShow();
}else {
System.out.println("二叉树为空,无法遍历");
}
}
public void deleteNode(String value) {
if(this.root!=null) {
if(this.root.value.equals(value)) {
this.root=null;
}else {
root.deleteNode(value);
}
}else {
System.out.println("空树,无法删除");
}
}
}
class HeroNode{
HeroNode leftNode;
HeroNode rightNode;
String value;
public HeroNode(String value) {
this.value=value;
}
public HeroNode getLeftNode() {
return leftNode;
}
public void setLeftNode(HeroNode leftNode) {
this.leftNode=leftNode;
}
public HeroNode getRightNode() {
return rightNode;
}
public void setRightNode(HeroNode rightNode) {
this.rightNode=rightNode;
}
@Override
public String toString() {
return "HeroNode [leftNode=" + leftNode + ", rightNode=" + rightNode + ", value=" + value + "]";
}
/**
* 三种遍历方法
*/
//前序遍历:
public void frontShow() {
System.out.println(this.value);
if(this.getLeftNode()!=null) {
this.getLeftNode().frontShow();
}
if(this.getRightNode()!=null) {
this.getRightNode().frontShow();
}
}
//中序遍历
public void middleShow() {
if(this.getLeftNode()!=null) {
this.getLeftNode().middleShow();
}
System.out.println(this.value);
if(this.getRightNode()!=null) {
this.getRightNode().middleShow();
}
}
//后序遍历
public void endShow() {
if(this.getLeftNode()!=null) {
this.getLeftNode().endShow();
}
if(this.getRightNode()!=null) {
this.getRightNode().endShow();
}
System.out.println(this.value);
}
//删除节点
public void deleteNode(String value) {
if(this.leftNode!=null&&this.leftNode.value.equals(value)) {
this.leftNode=null;
return;
}
if(this.rightNode!=null&&this.rightNode.value.equals(value)) {
this.rightNode=null;
return;
}
if(this.leftNode!=null) {
this.leftNode.deleteNode(value);
}
if(this.rightNode!=null) {
this.rightNode.deleteNode(value);
}
//System.out.println("没有找到要删除的元素");
}
}