In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
We have learned the concepts related to binary tree before, so let's analyze some classic interview questions in binary tree today.
1. Deletion of single node
-- write a function to delete all singularity nodes in the binary tree
-- requirement: after a node is deleted, its only child node replaces its position.
Examples are as follows
A > then include a pointer to the parent node in our node. Definition function: delOld1 (node), delete the single node in the binary tree with node as the root node
The idea of implementation is as follows
Let's see how the specific code is written.
# include # include "BTree.h" using namespace std;using namespace DTLib;template
< typename T >BTreeNode* createTree () {static BTreeNode ns [9]; for (int iTuno; ivoid printInOrder (BTreeNode* node) {if (node! = NULL) {printInOrder (node- > left); cout value right);} template
< typename T >Void printDualList (BTreeNode* node) {BTreeNode* g = node; cout right! = NULL)) {BTreeNode* parent = dynamic_cast (node- > parent); BTreeNode* node_child = (node- > left! = NULL)? Node- > left: node- > right; if (parent! = NULL) {BTreeNode*& parent_child = (parent- > left = = node)? Parent- > left: parent- > right; parent_child = node_child; node_child- > parent = parent;} else {node_child- > parent = NULL;} if (node- > flag ()) {delete node } ret = delOld1 (node_child);} else {delOld1 (node- > left); delOld1 (node- > right); ret = node;}} return ret;} int main () {BTreeNode* ns = createTree (); printInOrder (ns) Cout right = = NULL)) | ((node- > left = = NULL) & & (node- > right! = NULL) {BTreeNode* node_child = (node- > left! = NULL)? Node- > left: node- > right; if (node- > flag ()) {delete node;} node = node_child; delOld2 (node);} else {delOld2 (node- > left); delOld2 (node- > right);}
The test code is as follows
Int main () {BTreeNode* ns = createTree (); printInOrder (ns); cout void inOrderThread (BTreeNode* node, BTreeNode*& pre) {if (node! = NULL) {inOrderThread (node- > left, pre); node- > left = pre; if (pre! = NULL) {pre- > right = node;} pre = node; inOrderThread (node- > right, pre);}} template
< typename T >BTreeNode* inOrderThread1 (BTreeNode* node) {BTreeNode* pre = NULL; inOrderThread (node, pre); while ((node! = NULL) & & (node- > left! = NULL)) {node = node- > left;} return node;}
The test code is as follows
Int main () {BTreeNode* ns = createTree (); printInOrder (ns); the completion order of order traversal in cout is exactly the horizontal order of nodes.
Idea: use auxiliary pointers to point to the head node and tail node of the converted bi-directional linked list; connect the node with the bi-directional linked list converted from the left and right subtrees to form a complete bi-directional linked list.
Examples are as follows
Define functions: inOrderThread (node, head, tail); node is the root node and also the node accessed in the middle order; head is the first node pointing to the bi-directional linked list after successful conversion; tail is the tail node pointing to the bi-directional linked list after successful conversion.
The idea of implementation is as follows
Specific source code implementation
Template
< typename T >Void inOrderThread (BTreeNode* node, BTreeNode*& head, BTreeNode*& tail) {if (node! = NULL) {BTreeNode* h = NULL; BTreeNode* t = NULL; inOrderThread (node- > left, h, t); node- > left = t; if (t! = NULL) {t-> right = node;} head = (h! = NULL)? H: node; h = NULL; t = NULL; inOrderThread (node- > right, h, t); node- > right = h; if (h! = NULL) {h-> left = node;} tail = (t! = NULL)? T: node;}} template
< typename T >BTreeNode* inOrderThread2 (BTreeNode* node) {BTreeNode* head = NULL; BTreeNode* tail = NULL; inOrderThread (node, head, tail); return head;}
Test code
Int main () {BTreeNode* ns = createTree (); printInOrder (ns); cout
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.