Definition
An ordered tree with a maximum of two Subtrees is calledBinary Tree. Binary Tree is a specialTree.
Recursive definition: a binary tree is a set of n (n> = 0) finite nodes. N = 0 is called an empty binary tree; n> 0 is a binary tree composed of a root node and two trees that are mutually exclusive.
The 1st subtree of any node in a binary tree is called its left subtree, and the root of the Left subtree is called the left subtree of the node. The 2nd subtree of any node in the binary tree is called its right subtree, the root of the Left subtree is the right child of the node. If it is a binary tree:
Figure 1. Binary Tree
Full binary tree and full Binary Tree
In a binary tree, if all the branches have left and right trees, and the leaf nodes are on the same layer, such a binary tree is calledFull Binary Tree. A binary tree with K depth and 2k-1 nodes is called a full binary tree.
If the structure of a binary tree with N nodes is the same as that of the first n nodes of a full binary tree, such a binary tree is calledFull Binary Tree.
Figure 2. Full binary and full binary
Basic Nature
The level of the root node of a binary tree is 1.
Nature 1: The level I of the binary tree can have a maximum of 2 I-1 nodes (the level of the binary tree starts from 1, I ≥ 1)
Nature 2: A K-depth binary tree can have a maximum of 2k-1 nodes. (K ≥ 1)
Nature 3: For any binary tree T, if the number of leaf nodes is N0 and the number of non-leaf nodes with the degree of 2 is N2
N0 = n2 + 1
Nature 4: The depth of the Complete Binary Tree with n (n> 0) nodes is ⎣ log2n Limit + 1; then x limit indicates the maximum integer of X.
Nature 5: If the nodes of a Complete Binary Tree with N knots are numbered by sequence (from Layer 1 to layer 1 of Layer 1, from left to right ), for any node I (1 ≤ I ≤ n), there are:
(1) If I = 1, node I has no parent and is the root of a binary tree. If I> 1, its parent is node I/2.
(2) If 2I <= N, the left child node of node I is 2I; otherwise, node I is a leaf node without a left child node.
(3) If 2I + 1 <= N, the right child of node I is node 2I + 1; otherwise, node I is a leaf node and no right child node.
ABSTRACT Data Type
Data Element: A set of data elements with the same features.
Structure Relationship: The structural relationship between data elements in the tree is determined by the binary tree definition.
Basic operations: The main operations of the tree are:
(1) create a tree inttree (& T)
(2) destroy the destroytree (& T)
(3) create tree (& T, deinition)
(4) empty tree cleartree (& T)
(5) Decision Tree treeempty (t)
(6) Finding the depth of the tree treedepth (t)
(7) get root (t)
(8) obtain the node value (T, cur_e, & E) and store the node cur_e in the tree into the E unit.
(9) Assign the value of data to assign (T, cur_e, value) and assign the value to the node cur_e of the tree T.
(10) obtain the parent (T, cur_e) and return the parent node of the node cur_e in the tree T.
(11) obtain the leftmost child leftchild (T, cur_e) and return the leftmost child of the node cur_e in the tree T.
(12) obtain the right brother rightsibling (T, cur_e) and return the right brother of the node cur_e in the tree T.
(13) insert the subtree insertchild (& T, & P, I, C) and insert the subtree C to the tree T before P points to the node's subtree I.
(14) Delete the subtree deletechild (& T, & P, I), and delete the subtree where P points to node I in tree T.
(15) traverse the tree traversetree (T, visit ())
Binary Tree implementation Binary Tree interface btree
Package Datastructure. tree. btree; public interface btree {/*** add left subtree * @ Param lchild left subtree */Public void addlefttree (btree lchild ); /*** add right subtree * @ Param rchild right subtree */Public void addrighttree (btree rchild);/*** empty tree */Public void cleartree (); /*** evaluate the depth of the tree * @ return the depth of the tree */Public int dept (); /*** calculate the left child node * @ return */Public btree getleftchild ();/*** calculate the right child node * @ return */Public btree getrightchild (); /*** get the data of the root node * @ return */public object getrootdata ();/*** whether there is a left subtree * @ return */Public Boolean haslefttree (); /*** indicates whether there is a right subtree * @ return */Public Boolean hasrighttree ();/*** determines whether it is an empty tree * @ return if it is null, true is returned, otherwise, false */Public Boolean isempty ();/*** determines whether the node is a leaf node * @ return */Public Boolean isleaf (); /*** Delete the left subtree */Public void removeleftchild ();/*** Delete the right subtree */Public void removerightchild (); /*** get the root of the tree * @ return tree */Public btree root ();/*** set the data of the root node */Public void setrootdata (object data ); /*** calculate the number of knots ** @ return number of knots */Public int size ();}
Implementation of binary linked list
package datastructure.tree.btree;public class LinkBTree implements BTree {private Object data;private BTree lChild;private BTree rChild;public LinkBTree() {this.clearTree();}public LinkBTree(Object data) {this.data = data;this.lChild = null;this.rChild = null;}@Overridepublic void addLeftTree(BTree lChild) {this.lChild = lChild;}@Overridepublic void addRightTree(BTree rChild) {this.rChild = rChild;}@Overridepublic void clearTree() {this.data = null;this.lChild = null;this.rChild = null;}@Overridepublic int dept() {return dept(this);}private int dept(BTree btree) {if(btree.isEmpty()) {return 0;}else if(btree.isLeaf()) {return 1;} else {if(btree.getLeftChild() == null) {return dept(btree.getRightChild()) + 1;} else if(btree.getRightChild() == null) {return dept(btree.getLeftChild()) + 1;} else {return Math.max(dept(btree.getLeftChild()), dept(btree.getRightChild()))+1;}}}@Overridepublic BTree getLeftChild() {return lChild;}@Overridepublic BTree getRightChild() {return rChild;}@Overridepublic Object getRootData() {return data;}@Overridepublic boolean hasLeftTree() {if(lChild != null)return true;return false;}@Overridepublic boolean hasRightTree() {if(rChild != null)return true;return false;}@Overridepublic boolean isEmpty() {if((lChild == null && rChild == null && data == null) || this == null) {return true;}return false;}@Overridepublic boolean isLeaf() {if(lChild == null && rChild == null) {return true;}return false;}@Overridepublic void removeLeftChild() {lChild = null;}@Overridepublic void removeRightChild() {rChild = null;}@Overridepublic BTree root() {return this;}@Overridepublic void setRootData(Object data) {this.data = data;}@Overridepublic int size() {return size(this);}private int size(BTree btree) {if(btree == null) return 0;else if(btree.isLeaf()) return 1;else {if(btree.getLeftChild() == null) {return size(btree.getRightChild()) + 1;} else if(btree.getRightChild() == null) {return size(btree.getLeftChild()) + 1;} else {return size(btree.getLeftChild()) + size(btree.getRightChild()) + 1;}} }}
Binary tree traversal
Binary tree traversal refers to the process of accessing all nodes in the tree in a certain order, and each node is accessed only once. There are three methods for traversal: Forward traversal, central traversal, and post-order traversal, assume that the root node, left child node, and right child node are represented by D, R, and L respectively, then, the order of forward traversal, central traversal, and post-sequential traversal is DLR, LDR, and lrd respectively. The so-called access to a node generally refers to some operation on the data in the node. Therefore, we can define an interface visit for operations on the data in the node, so that all the classes that traverse the tree can implement this interface.
Visit interface:
Package Datastructure. tree; /*** interface for node operations ** @ author administrator **/public interface visit {/*** perform some operations on the node * @ Param btree node */Public void visit (btree );}
Traverse Binary Trees
Package Datastructure. tree; /***** traverse the binary tree * @ author administrator **/public class orderbtree implements visit {/*** preorder traversal * @ Param Root Node */Public void preorder (btree Root) {visit (Root); If (root. getleftchild ()! = NULL) {preorder (root. getleftchild ();} If (root. getrightchild ()! = NULL) {preorder (root. getrightchild () ;}}/*** in-order traversal * @ Param root node */Public void inorder (btree root) {If (root. getleftchild ()! = NULL) inorder (root. getleftchild (); visit (Root); If (root. getrightchild ()! = NULL) {// system. out. println ("true"); inorder (root. getrightchild ();}/*** post-order traversal * @ Param root node */Public void postorder (btree root) {If (root. getleftchild ()! = NULL) postorder (root. getleftchild (); If (root. getrightchild ()! = NULL) postorder (root. getrightchild (); visit (Root) ;}@ overridepublic void visit (btree) {system. out. print (btree. getrootdata () + "\ t ");}}
Binary Tree Testing
Tree to be built
Package Datastructure. tree;/*** test binary tree * @ author administrator **/public class btreetest {public static void main (string ARGs []) {btree = new linkbtree ('A'); btree Bt1, bt2, bt3, bt4; Bt1 = new linkbtree ('B'); btree. addlefttree (Bt1); bt2 = new linkbtree ('D'); bt1.addlefttree (bt2); bt3 = new linkbtree ('C'); btree. addrighttree (bt3); bt4 = new linkbtree ('E'); bt3.addlefttree (bt4); bt4 = new linkbtree ('F'); bt3.addrighttree (bt4); system. out. println ("tree depth:" + btree. dept (); system. out. println ("number of nodes in the tree:" + btree. size (); system. out. println ("empty tree:" + btree. isempty (); system. out. println ("whether it is a leaf node:" + btree. isleaf (); system. out. println ("Whether the leftmost bottom edge node is a leaf node:" + btree. getrightchild (). getrightchild (). isleaf (); system. out. println ("root node:" + btree. root (); orderbtree order = new orderbtree (); system. out. println ("\ n pre-order traversal:"); Order. preorder (btree); system. out. println ("\ n sequential traversal:"); Order. inorder (btree); system. out. println ("\ n sequential traversal:"); Order. postorder (btree); btree. removeleftchild (); system. out. println ("\ n Delete the left subtree and then traverse in the middle order:"); Order. inorder (btree );}}
The result is as follows:
Tree depth: 3
Number of nodes in the tree: 6
Empty tree: false
Whether it is a leaf node: false
Whether the leftmost bottom edge node is a leaf node: True
Root Node: Datastructure. Tree. linkbtree @ dc8569
Forward traversal:
A B dcef
In-order traversal:
D B aecf
Post-order traversal:
D B EFCA
After the left subtree is deleted, the central traversal is:
A e cf