Fortune left France 3 basis class4- find a topic node in the binary tree node successor, predecessor node

1. Title: find the successor of a node in a binary tree

There is a new type of binary tree nodes as follows:
public class the Node
{
public int value;
public left the Node;
public the Node right;
public the Node parent;
public the Node (int Data) = Data {this.value;}
}
The binary tree structure than normal a multi-node structure of the parent pointers pointing to the parent node. Suppose a binary Node type nodes, each node in the tree parent pointers are properly directed their parent node, the first node to the parent NULL. Only one node to node in a binary tree, implement a function that returns the node's successor nodes. In the sequence preorder traversal of the binary tree, the next node node to node is called the successor node . Find the successor of a node in a binary tree. (4,2,5,1,6,3,7 traversing sequence is 2 to 4 is a relay, the relay 5 is 2 ... a number of the relay nodes is a number of pre)

Here Insert Picture Description

2. Analysis

This problem is essentially to find the next node in a preorder traversal of the nodes as descendant node. Order traversal sequence is the left-right.
(1) For a node if it has the right subtree, then the successor node of this node is the leftmost node of its right subtree. The reason is that in order traversal is left-right order with the right subtree, then the node is a node, the next node is its right child nodes in the tree, and the right subtree is left in the right order, the lookup leftmost node. So the successor of the current node is the leftmost node of its right subtree. 1 most right subtree is the left node 6, so that the relay 1 is 6.
(2) For a node if the right child does not exist, if the left subtree of the parent node of the current node is the current node, its parent node is the successor of the current node. If not, the current node becomes the parent node, keep looking. The reason is the parent node as a node, if the node is the successor node must be before a node is the last node in the left subtree. For 2,4 is the last node in the left subtree of 2, 2 4 is a relay node, for a left subtree 1,5 is the last node, the relay node 5 is 1.
If there is no parent node (2) empty case subsequent node, the last node 7 is not the relay node. The boundary conditions require attention.

3. core code

(1) a new binary tree data structure

class Tree
{
public:
	int val;
	Tree *right;
	Tree *left;
	Tree *parent;
	Tree(int x)
	{
		this->val = x;
		this->left = NULL;
		this->right = NULL;
		this->parent = NULL;
	}
};

(2) add a node in the tree (by the constructor)

	Tree *head = new Tree(1);
	head->left = new Tree(2);
	head->left->parent = head;
	head->left->left = new Tree(4);
	head->left->left->parent = head->left;
	head->left->right = new Tree(5);
	head->left->right->parent = head->left;

	head->right = new Tree(3);
	head->right->parent = head;
	head->right->left = new Tree(6);
	head->right->left->parent = head->right;
	head->right->right = new Tree(7);
	head->right->right->parent = head->right;

(3) to find a successor node

Tree* getSuccessorNode(Tree *cur)
{
	//节点为空中继节点为空
	if(cur == NULL)
		return cur;
	//右子树存在
	if(cur->right != NULL)
	{
		cur = cur->right;
		while(cur->left!=NULL)//找右子树上最左节点
		{
			cur = cur->left;
		}
		return cur;
	}
	else//右子树不存在
	{
		//父节点为空时没有中继节点;父节点的左子树是当前节点则为中继节点
		while(cur->parent!=NULL && cur->parent->left != cur)
		{
			cur = cur->parent;
		}
		return cur->parent;
	}
}

4. The complete code

#include <iostream>
using namespace std;

class Tree
{
public:
	int val;
	Tree *right;
	Tree *left;
	Tree *parent;
	Tree(int x)
	{
		this->val = x;
		this->left = NULL;
		this->right = NULL;
		this->parent = NULL;
	}
};

Tree* getSuccessorNode(Tree *cur)
{
	//节点为空中继节点为空
	if(cur == NULL)
		return cur;
	//右子树存在
	if(cur->right != NULL)
	{
		cur = cur->right;
		while(cur->left!=NULL)//找右子树上最左节点
		{
			cur = cur->left;
		}
		return cur;
	}
	else//右子树不存在
	{
		//父节点为空时没有中继节点;父节点的左子树是当前节点则为中继节点
		while(cur->parent!=NULL && cur->parent->left != cur)
		{
			cur = cur->parent;
		}
		return cur->parent;
	}
}

int main()
{
	Tree *head = new Tree(1);
	head->left = new Tree(2);
	head->left->parent = head;
	head->left->left = new Tree(4);
	head->left->left->parent = head->left;
	head->left->right = new Tree(5);
	head->left->right->parent = head->left;

	head->right = new Tree(3);
	head->right->parent = head;
	head->right->left = new Tree(6);
	head->right->left->parent = head->right;
	head->right->right = new Tree(7);
	head->right->right->parent = head->right;
	//打印
	Tree* succ = getSuccessorNode(head->right);
	if(succ == NULL)
		cout<<"NULL";
	else
		cout<<succ->val;
		
	system("pause");
	return 0;
}

5. Added: Finding predecessor node

Similarly: (1) when the left sub-tree, the rightmost node in the left subtree is the precursor of the current node. The reason is that when the sub-tree node as the current node, the rightmost node in the left subtree is inorder traversal, the left-right-left order in the last node, the next node is a node that is in the current node, so the left rightmost sub-tree node is the current node precursor nodes
(2) of the current node is the right child of its parent, the parent node is the predecessor of the current node. Parent node as a node, in the left-right sequence, the right child is left in the right order in the right, then the parent node is the predecessor of the current node.

Published 51 original articles · won praise 1 · views 1375

Guess you like

Origin blog.csdn.net/shi_xiao_xuan/article/details/103879820