当前位置:网站首页>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;
}边栏推荐
- 字符串 最长公共前缀
- SQL to object thinking vs SQL of object thinking
- 虚幻引擎图文笔记:使用VAT(Vertex Aniamtion Texture)制作破碎特效(Houdini,UE4/UE5)上 Houdini端
- ShardingSphere-Proxy 4.1 分库分表
- [MySQL learning notes 21] storage engine
- clang frontend command failed with exit code 250
- Linked list delete nodes in the linked list
- 字符串 实现 strStr()
- 【论文阅读|深读】LINE: Large-scale Information Network Embedding
- 依赖属性、依赖附加属性以及类型转换
猜你喜欢

Puzzle (019.2) hexagonal lock

ShardingSphere-Proxy 5.0 分库分表(一)

How do wechat applets make their own programs? How to make small programs on wechat?

Redis(二)分布式锁与Redis集群搭建

我希望按照我的思路尽可能将canvas基础讲明白

The left sliding menu +menu item icon is grayed out

Principle of distribution: understanding the gossip protocol

WebApi性能优化

Kotlin advanced generic

How to "transform" small and micro businesses (I)?
随机推荐
Houdini图文笔记:Your driver settings have been set to force 4x Antialiasing in OpenGL applications问题的解决
Tutorial on installing SSL certificates in Microsoft Exchange Server 2007
Processing picture class library
The gradle configuration supports the upgrade of 64 bit architecture of Xiaomi, oppo, vivo and other app stores
Flutter replaces the default icon of Gaud positioning
在Microsoft Exchange Server 2007中安装SSL证书的教程
How to make a self-made installer and package the program to generate an installer
The left sliding menu +menu item icon is grayed out
原生小程序开发注意事项总结
ShardingSphere-Proxy 4.1 分庫分錶
SparseArray details
Cocopod error failed: undefined method `map 'for nil:nilclass
The way that flutter makes the keyboard disappear (forwarding from the dependent window)
我希望按照我的思路盡可能將canvas基礎講明白
How to make small programs on wechat? How to make small programs on wechat
Redis (I) principle and basic use
WebApi性能优化
Encoding format for x86
广发证券靠谱吗?是否合法?开股票账户安全吗?
虚幻引擎图文笔记:使用VAT(Vertex Aniamtion Texture)制作破碎特效(Houdini,UE4/UE5)上 Houdini端