[email protected] * * * * * *...">

当前位置:网站首页>Basic knowledge of binary tree, BFC, DFS

Basic knowledge of binary tree, BFC, DFS

2022-07-06 03:57:00 Pupu pupu

Author: intellectuals
Computer science
Controllable things Heavy and calm Uncontrollable things Optimistic face
[email protected]

️ ️ ️
————————————————
Copyright notice : This paper is about CSDN Blogger 「 Pu Shangqing sky 」 The original article of


Knowledge point

Binary tree

  • If specified The number of node layers of the root is 1, Then the... Of a non empty binary tree i There is a maximum of 2 Of i-1 Power (i > 0) Nodes
  • If the root node depth of the binary tree is specified as 1, Then the depth is K Two fork tree Maximum number of nodes 2 Of k Power -1
  • For any binary tree , If the number of leaf nodes is n0, Degree is 2 The number of non leaf nodes is n2, be n0=n2+1
  • have n The depth of a complete binary tree of nodes k be equal to log With 2 Bottom n+1, Rounding up
  • Subscript relation
    If the parent node is known parent The subscript
    Left the child :leftChild = 2parent+1
    The right child :rightChild = 2parent +2
    If the child is known child node
    parent = (child-1)/2

-

Binary tree traversal

DFS: Depth-first traversal - Stack

The former sequence traversal :

traversal order : recursive
Put the root node in front
1. First , Handle The root node
2. next , Complete traversal of the root The left subtree
3. Last , Complete traversal of the root Right subtree
 Insert picture description here
① We are from Root node Start ,
namely : A{ } { }
② Because we are traversing first The left subtree
therefore , A { B{}{} } { }
A { B{ D{}{} }{} } { }

A { B{ D{}{undefinedG} }{} } { }
③ Last traversal Right subtree :

A { B{ D{}{undefinedG} }{} } { C{}{} }

A { B{ D{}{undefinedG} }{} } { C{undefinedE}{} }

A { B{ D{}{undefinedG} }{} } { C{undefinedE}{ F{}{} } }

A { B{ D{}{undefinedG} }{} } { C{undefinedE}{ F{undefinedH}{} } }

So we get the result of our first order traversal :
A B D G C E F H
 Insert picture description here

In the sequence traversal

  • traversal order :( recursive )
    1. First traverse the whole tree The left subtree
    2. Intermediate treatment The root node
    3. Then traverse the whole tree Right subtree
     Insert picture description here
    Let's go through The left subtree , The traversal method is In the sequence traversal

{ {}B{} } A { }

{ { {}D{} }B{} } A { }

{ { {}D{undefinedG} }B{} } A { }

Let's go over Right subtree , The traversal method is still In the sequence traversal

{ { {}D{undefinedG} }B{} } A { {}C{} }

{ { {}D{undefinedG} }B{} } A { {E}C{} }

{ { {}D{undefinedG} }B{} } A { {undefinedE}C{ {}F{} } }

{ { {}D{undefinedG} }B{} } A { {undefinedE}C{ {H}F{} } }

So we get our In the sequence traversal The result :
D G B A E C H F

After the sequence traversal

  • traversal order ( recursive )
    1. First traversal The left subtree
    2. Re traversal Right subtree
    3. Finally deal with The root node
     Insert picture description here
    Let's go through The left subtree , The traversal method is After the sequence traversal :

{ {}{}B } { }A

{ { {}{}D }{}B } { }A

{ { {}{undefinedG}D }{}B } { }A

Let's go over Right subtree , The traversal method is still After the sequence traversal

{ { {}{undefinedG}D }{}B } { }A

{ { {}{undefinedG}D }{}B } { {}{}C }A

{ { {}{undefinedG}D }{}B } { {undefinedE}{}C }A

{ { {}{undefinedG}D }{}B } { {undefinedE}{ {}{}F }C }A

{ { {}{undefinedG}D }{}B } { {undefinedE}{ {undefinedH}F}{}C }A

So we get our After the sequence traversal The result :
G D B E H F C A

BFC: Breadth first traversal

Sequence traversal ( queue )

purpose : Determine whether it is a complete binary tree

  • From top to bottom , Traverse the binary tree from left to right
  •  Insert picture description here

Code order

1. Start-up phase : Put the root node in the queue

 Insert picture description here
2. Turn on the cycle , Until the queue is empty (isEmpty)

  • Take the first node from the queue
  • Sequence traversal passes through this node ( Print )
  • Put the left of the node / Put the right child node into the queue
     Insert picture description here

Praise first and then watch , Develop habits !!!^ _ ^
Update your knowledge every day !!!
It's not easy to code words , Everyone's support is my driving force to stick to it . Don't forget after you like Focus on I oh !

原网站

版权声明
本文为[Pupu pupu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202132255176852.html