/* Definition of tree: A finite set T consisting of one or more (n >= 0) nodes has one and only one node called the root (root), when n>1, the rest of the nodes are divided into m (m > 0) ) mutually disjoint finite sets T1, T2, ..., Tm. Each set is itself a tree, called a subtree of this root. Features of tree structure: 1. Nonlinear structures, with one immediate predecessor, but possibly multiple immediate successors (1:n) 2. The definition of a tree has recursive lines, and there are trees within a tree. 3. The tree can be empty, that is, the number of nodes is 0. Several terms: root -> root node Leaf -> i.e. terminal node (no successor) Forest -> refers to the set of m disjoint trees Ordered tree -> The subtrees of the node are ordered from left to right and cannot be interchanged (left is the first) Unordered tree -> Each subtree of the node can exchange positions. Parent -> that is the upper node (direct predecessor) parent Child -> the subtree of the lower node (direct successor) child Brother -> the same level node under the same parent (children call each other brothers) sibling Cousins -> nodes whose parents are at the same level (but not the same parents) cousin Ancestors -> that is, all nodes branched from the root to this node Node -> i.e. the data element of the tree The degree of the node -> the number of subtrees that the node is attached to (a few direct successors are a few degrees). The level of the node -> the number of layers from the root to the node (the root node is the first layer) Terminal node -> node with degree 0, ie leaf Branch node -> except the node that the root of the tree thinks (also called internal node) The degree of the tree -> the maximum of the degrees of all nodes (MAX(degree of each node)) The depth (or height) of the tree -> refers to the maximum level of all nodes (Max (level of each node)) Depth is from the root node from top to bottom Height is from top to bottom of leaves Three ways to represent trees: parental notation struct Node { int data; int parent; }; Left child right brother notation: Child notation: struct ChildNode { int data; ChildNode*; }; */ /* Traversal of binary tree: Traversal definition: Refers to traversing each node according to a search route without repeating (also known as traveling). Traversal uses: It is the premise of tree structure insertion, deletion, modification, search and sorting operations, and is the basis and core of all operations in binary trees. The way to traverse: Keep in mind a convention that each node is viewed "left first, then right". There are three implementation schemes for tree traversal: DLR LDR LRD have to traverse what are you talking about? Before (root) order traversal in (root) order traversal After (root) order traversal DLR-> preorder traversal, that is, the first root is left on the right LDR->In-order traversal, that is, the root is left on the right LRD->post-order traversal, that is, left at right at the root */ # include <stdio.h> # include <stdlib.h> # include <string.h> // binary tree node typedef struct BINARYNODE { char ch; struct BINARYNODE* lchild; struct BINARYNODE* rchild; }BinaryNode; // find the height of the binary tree int CaculateLeaTreeDepth(BinaryNode* root) { if(root == NULL) { return 0; } // printf("front depth %d\n",depth); // count the maximum number of layers int leftDepth = CaculateLeaTreeDepth(root->lchild); // the number of nodes in the right subtree printf("leftDepth %d\n",leftDepth); int rightDepth = CaculateLeaTreeDepth(root->rchild); printf("rightDepth %d\n",rightDepth); int depth = leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1; return depth; } void CrestBinaryTree() { //create node BinaryNode node1 = {'A', NULL, NULL}; BinaryNode node2 = {'B', NULL, NULL}; BinaryNode node3 = {'C', NULL, NULL}; BinaryNode node4 = {'D', NULL, NULL}; BinaryNode node5 = {'E', NULL, NULL}; BinaryNode node6 = {'F', NULL, NULL}; BinaryNode node7 = {'G', NULL, NULL}; BinaryNode node8 = {'H', NULL, NULL}; //create node relationship node1.lchild = &node2; node1.rchild = &node6; node2.rchild = &node3; node3.lchild = &node4; node3.rchild = &node5; node6.rchild = &node7; node7.lchild = &node8; printf("%d\n",CaculateLeaTreeDepth(&node1)); } int main(int argc, char *argv[]) { CrestBinaryTree(); // printf("num = %d\n",num); return 0; }
Find the height of the binary tree
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325973078&siteId=291194637
Recommended
Ranking