In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to delete the nodes in the binary tree, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Algorithm:
1. Rear drive algorithm:
/ * Recursive solution: 1. Find the node 2 that needs to be deleted. The deleted node is only the right subtree or the left subtree, and the root node of the right or left subtree is directly regarded as the deleted node 3. When both the left and right subtrees of the deleted node exist, the largest node of the left subtree is also called the precursor as the deleted node, or the smallest node of the right subtree, also known as the rear drive, is regarded as the deleted node. , /
two。 Precursor algorithm:
/ * Recursive solution: 1. Find the node 2 that needs to be deleted. The deleted node is only the right subtree or the left subtree, and the root node of the right or left subtree is directly regarded as the deleted node 3. When both the left and right subtrees of the deleted node exist, the largest node of the left subtree is also called the precursor as the deleted node, or the smallest node of the right subtree, also known as the rear drive, is regarded as the deleted node. , /
Title:
Rear drive algorithm:
Func deleteNode (root * TreeNode, key int) * TreeNode {if root = = nil {return nil} if key
< root.Val { root.Left = deleteNode(root.Left,key) return root } if key >Root.Val {root.Right = deleteNode (root.Right,key) return root} / / the node to be deleted has been found through recursion At this time, because of recursion, the pointer root = = nil {return root.Left} if root.Left = = nil {return root.Right} / / is used to replace the current node min: = root.Right for min.Left! = nil {min = min.Left} root.Val = min.Val / / replace the value of the node to be deleted Here is to copy root.Right = deleteMin (root.Right) / / here delete the smallest node moved from the right subtree return root} func deleteMin (root * TreeNode) * TreeNode {/ / left subtree is not there, it means that this node is the smallest node to be deleted / / there are two cases: one: this node is a leaf node, which is directly assigned to nil as a deleted node. / / 2: this node has no left subtree, only the right subtree. In this case, you need to replace the right subtree with the node if root.Left = = nil {right: = root.Right root.Right = nil return right} root.Left = deleteMin (root.Left) / / if the left subtree is always there, always use the left subtree to find the smallest node return root}.
Precursor algorithm
Func deleteNode (root * TreeNode, key int) * TreeNode {if root = = nil {return nil} if key
< root.Val { root.Left = deleteNode(root.Left,key) return root } if key >Root.Val {root.Right = deleteNode (root.Right,key) return root} / / the node to be deleted has been found through recursion At this time, because of recursion, the pointer root = = nil {return root.Left} if root.Left = = nil {return root.Right} / / is used to replace the current node max: = root.Left for max.Right! = nil {max = max.Right} root.Val = max.Val root.Left = deleteMax (root.Left) return root}
Func deleteMax (root * TreeNode) * TreeNode {if root.Right = = nil {left: = root.Left root.Left = nil return left} root.Right = deleteMax (root.Right) return root} / * Recursive solution: 1. Find the node 2 that needs to be deleted. The deleted node is only the right subtree or the left subtree, and the root node of the right or left subtree is directly regarded as the deleted node 3. When both the left and right subtrees of the deleted node exist, the largest node of the left subtree is also called the precursor as the deleted node, or the smallest node of the right subtree, also known as the rear drive, is regarded as the deleted node. * / after reading the above, do you know how to delete nodes in the binary tree? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.