【Data Structure and Algorithm】Tree and Binary Tree Exercises

topic

Topic 1

知一棵树边的集合为
< I , M > , < I , N > , < E , I > , < B , E > , < B , D > , < A , B > , < G , J > , < G , K > , < C , G > , < C , F > , < H , L > , < C , H > , < A , C > {<I,M>,<I,N>,<E,I>,<B,E>,<B,D>,<A,B>,<G,J>, <G,K>,<C,G>,<C,F>,<H,L>,<C,H>,<A,C>} <I,M>,<I,N>,<E,I>,<B,E>,<B,D>,<A,B>,<G,J>,<G,K>,<C,G>,<C,F>,<H,L>,<C,H>,<A,C>
Please draw the tree and answer the following questions:

  1. Which is the root node?
  2. Which are the leaf nodes?
  3. Which are the parents of node G?
  4. Which are the ancestors of node G?
  5. Which are the children of node G?
  6. Which are the descendants of node E?
  7. Which are the siblings of node E? Which are the siblings of node F?
  8. What are the level numbers of nodes B and N?
  9. What is the depth of the tree?
  10. What is the depth of the subtree rooted at node C?

The tree shape

insert image description here

Question 1 Answer

(1) Root node: A
(2) Leaf nodes: M, N, J, K
(3) Parents of node G: C
(4) Ancestors of node G: A, C
(5) Children of node G : J, K
(6) Descendants of node E: I, M, N
(7) Brothers of node E: D; brothers of node F: H, G
(8) The level numbers of nodes B and N are: 2 , 5
(9) Depth of tree: 5
(10) Depth of subtree rooted at node C: 2


Topic 2

How is a tree of degree 2 different from a binary tree?


Question 2 Answer

  • In terms of structure: there is no strict parent-child relationship between the nodes of a tree with a degree of 2, but each node in the binary tree has a clear parent node and left and right nodes.
  • Traversal method: Binary trees are commonly traversed in preorder, inorder, and postorder, while trees with a degree of 2 can be traversed in a more special traversal method, such as: starting from the root node, traversing the left child node first, and then traversing the right child Node, and finally recursively traverse the subtree.

Topic 3

It is known that there are n1 nodes of degree 1 in a tree of degree k, n2 nodes of degree 2, ..., nk nodes of degree k, how many leaf nodes are there in the tree ?


Question 3 Answer

Sum of degrees: n − 1 ① Sum of degrees: n-1 ①sum of degrees: n1①Sum
of degrees: n 1 + n 2 ∗ 2 + n 3 ∗ 3.. + nm ∗ m ② Sum of degrees: n1+n2*2+n3*3 ..+ nm*m ②Sum of degrees: n 1+n2 _2+n 33..+nmm ②Simultaneous
solution: n 0 = n 2 + . . . + ( k − 1 ) ∗ nk + 1 Simultaneous solution: n0=n2+...+(k-1)*nk+1Simultaneous solution: n 0=n2 _+...+(k1)nk+1


Topic 4

It is known that in a tree containing n nodes, there are only branch nodes with degree k and leaf nodes with degree 0, find the number of leaf nodes contained in the tree


Question 4 Answers

Let there be x leaf nodes and n − x branch nodes with degree k. Let x be leaf nodes and nx branch nodes with degree k.Let the number of leaf nodes be x , and the branch nodes with degree k be nx so
insert image description here
there are a total of1 + ( n − k ) ∗ k 1+(nk)*k1+(nk)k nodes (where 1 is the topmost node, which is the root node)
solution: x = n − ( n − 1 ) / k solution: x= n-(n-1)/kSolution: x=n(n1)/k


Topic 5

Proof: The following relationship is satisfied between the number of leaf nodes n0 and the number of non-leaf nodes n1 on a full k-ary tree: n 0 = ( k − 1 ) n 1 + 1 n_0=(k-1)n_1+1n0=k1n1+1


Question 5 Answers

Since it is a full k-ary tree, there are only leaf nodes and non-leaf nodes with degree k. Suppose there are n + 1 layers
n 1 = ( 1 − kn ) / ( 1 − k ) n_1=(1-k^ n)/(1-k)n1=(1kn)/(1k)
n 0 = k n n_0=k^n n0=kn
therefore proves that
n 0 = ( k − 1 ) n 1 + 1 n_0=(k-1)n1+1n0=k1 ) in 1+1


conclusion

  Because it is an algorithm side dish, the methods and ideas provided may not be very good, please bear with me~ If you have any questions, please leave a message to discuss. If you think this article is helpful to you, can you give me a free like? The communication between us is my biggest motivation!

Guess you like

Origin blog.csdn.net/Zchengjisihan/article/details/131521603