当前位置:网站首页>On binary tree
On binary tree
2022-06-25 10:18:00 【Fly upward】
Catalog
2.2 Properties of binary trees
1. Tree structure

1.1 Definition
1.2 Tree representation
class Node {
int value; // Data stored in the tree
Node firstChild; // The first child quoted
Node nextBrother; // The next brother quotes
}
1.3 The application of tree
Used for file system management : Directories and files
2. The binary tree theorem
2.1 Concept
Characteristics of binary trees :

2.2 Two special binary trees


2.2 Properties of binary trees
Round up 2.3 Binary tree storage
// Child representation
class Node {
int val; // Data fields
Node left; // Left child's quote , It often represents the whole left sub tree rooted by the left child
Node right; // Right child's quote , It often represents the whole right subtree with the right child as the root
}
// The expression of children's parents
class Node {
int val; // Data fields
Node left; // Left child's quote , It often represents the whole left sub tree rooted by the left child
Node right; // Right child's quote , It often represents the whole right subtree with the right child as the root
Node parent; // The root node of the current node
}2.6 Traversal of binary tree
1. The former sequence traversal (Preorder Traversal Also called preorder traversal )—— Root node ---> The left subtree of the root ---> Right subtree of root .2. In the sequence traversal (Inorder Traversal)—— The left subtree of the root ---> The root node ---> Right subtree of root .3. After the sequence traversal (Postorder Traversal)—— The left subtree of the root ---> Right subtree of root ---> The root node .
The first binary tree

Lesson 2 binary tree

before 、 in 、 Post traversal , Using recursive methods is to print the root .
1) The former sequence traversal
Solution 1
void preOrder(BTNode root) {
if(root == null) {
return;
}
System.out.print(root.val + " ");
preOrder(root.left);
preOrder(root.right);
}
Solution 2 : Non recursive method
void preorderNor(TreeNode root) {
List<Integer> ret = new ArrayList<>();
Stack<TreeNode> stack = new Stack();
TreeNode cur = root;
while (cur != null || stack.isEmpty()) {
while (cur != null) {
// Into the stack
stack.push(cur);
// Put the stack elements into the sequence table
ret.add(cur.val);
cur = cur.left;
}
TreeNode top = stack.pop();
cur = top.right;
}
return ret;
}2) In the sequence traversal
Solution 1
void inOrder(BTNode root) {
if(root == null) {
return;
}
inOrder(root.left);
System.out.print(root.val + " ");
inOrder(root.right);
}Solution 2 : Non recursive method
void inorderNor(TreeNode root) {
List<Integer> ret = new ArrayList<>();
Stack<TreeNode> stack = new Stack();
TreeNode cur = root;
while (cur != null || stack.isEmpty()) {
while (cur != null) {
// Into the stack
stack.push(cur);
cur = cur.left;
}
TreeNode top = stack.pop();
// Put the top element of the stack into the sequence table
ret.add(top.val);
cur = top.right;
}
return ret;
}3) After the sequence traversal
Solution 1
// After the sequence traversal
void postOrder(BTNode root) {
if(root == null) {
return;
}
postOrder(root.left);
postOrder(root.right);
System.out.print(root.val + " ");
}
Solution 2 : Non recursive method
void inorderNor(TreeNode root) {
List<Integer> ret = new ArrayList<>();
Stack<TreeNode> stack = new Stack();
TreeNode cur = root;
TreeNode prev = null;
while (cur != null || stack.isEmpty()) {
while (cur != null) {
// Into the stack
stack.push(cur);
cur = cur.left;
}
// Read the top element of the stack
TreeNode top = stack.peek();
if () {
stack.pop();
ret.add(top.val);
prev = top;
}else {
cur = top.right;
}
}
return ret;
}Be careful :
Know the preorder traversal and In the sequence traversal , Find the post order traversal :
First, get the root from the front of the preorder traversal , Then take the root and find it in the sequence traversed by the middle order , To the left of the root is the left subtree , To the right of the root is the right subtree . In turn, cycle .
Know the postorder traversal and In the sequence traversal , Find the post order traversal :
First, get the root from the back of the sequence traversal , Then take the root and find it in the sequence traversed by the middle order , To the left of the root is the left subtree , To the right of the root is the right subtree . In turn, cycle .
2.7 Sequence traversal
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ret = new ArrayList<>();
if (root == null) return ret;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty) {
int size = queue.size();// How many nodes are there in the current layer
List<Integer> lst = new ArrayList<>();
while (size != 0) {
TreeNode cur = queue.poll();
list.add(cur.val);
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
size--;
}
ret.add(list);
}
return ret;
}边栏推荐
- Methodchannel of flutter
- IdentityServer4 定义概念
- ‘Flutter/Flutter. h‘ file not found
- DigiCert和GlobalSign单域名OV SSL证书对比评测
- Houdini图文笔记:Your driver settings have been set to force 4x Antialiasing in OpenGL applications问题的解决
- Create menu file
- 新学派:不诈骗经济学
- Modbus protocol and serialport port read / write
- Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
- The path of Architects
猜你喜欢

Vscode attempted to write the procedure to a pipeline that does not exist

虚幻引擎图文笔记:使用VAT(Vertex Aniamtion Texture)制作破碎特效(Houdini,UE4/UE5)上 Houdini端

Kotlin advanced generic

How do wechat sell small commodity programs do? How to open wechat apps to sell things?

Flask博客实战 - 实现侧边栏文章归档及标签

Pytorch_ Geometric (pyg) uses dataloader to report an error runtimeerror: sizes of tenants must match except in dimension 0

Minio基本使用与原理

Jetpack compose layout (III) - custom layout

P2P network core technology: Gossip protocol

How to "transform" small and micro businesses (II)?
随机推荐
Kotlin advanced - class
How do wechat applets make their own programs? How to make small programs on wechat?
How to "transform" small and micro businesses (I)?
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
An auxiliary MVP architecture project quick development library -mvpfastdagger
MySQL source code reading (II) login connection debugging
Flask博客实战 - 实现个人中心及权限管理
字符串 实现 strStr()
CyCa 2022 children's physical etiquette primary teacher class Shenzhen headquarters station successfully concluded
Android database security: after the user exits, the transaction rollback log still stores relevant data information
Floating window --- create an activity floating window (can be dragged)
Encoding format for x86
Use evo
[buuctf.reverse] 121-125
申请多域名SSL证书的要求及注意事项
I hope to explain the basics of canvas as clearly as possible according to my ideas
How to make a self-made installer and package the program to generate an installer
NetCore性能排查
【动态规划】—— 数字三角形
i++ 和 ++i的真正区别