Binary tree looking for successor node

topic

Find a node in a binary tree successor node
[Title] There is a new binary tree node types is as follows:
public class the Node {public int value; public the Node left;
public the Node right; public the Node parent;
public the Node (int Data) {the this Data = .Value;}
} the
structure of a plurality of parent pointer pointing to a parent node than conventional binary tree node structure. False
provided a Node binary type nodes, each node in the tree parent pointer is
correctly point to their parent the parent node, the first node to the null. To only one in
a node in a binary tree node, implement a function that returns the node's successor nodes. In the second
sequence preorder traversal of the tree, the next node node to node is called the successor node.

Thinking

Remember these two rules:
1. If the current node has a right child, you have been looking for its leftmost nodes, until there are no nodes left to return to the node is the successor node;
2, if the current node · when the point is not right subtree, it is determined whether the left node parent node of the current node is the current node, if the parent node is returned, if not then let the parent node * current node sequentially repeats the above process until found so far,

Code

// 中序遍历寻找后继结点->cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
using namespace std;

struct Node
{
	int val;
	struct Node*parent;
	struct Node*left;
	struct Node*right;
	struct Node(int data) : val(data), left(nullptr), right(nullptr) {};
};
Node*getmostleft(Node*node);
	Node* getsucessful(Node*node)
	{
		if (node == NULL)
			return node;
		if (node->right != NULL)
		{
		return	getmostleft(node->right);
			
		}
		else {
			Node*parent = node->parent;
			while (parent != NULL&&parent->left != node)
			{
				node = parent;
				parent = node->parent;
			}
			return parent;
		}
	}

	Node*getmostleft(Node*node)
	{
		if (node == NULL)
			return node;
		
		while (node->left != NULL)
		{
			node = node->left;
		}
		return node;
	}
int main()
{
	Node*head= new Node(10);
	head->parent = NULL;
	head->left = new Node(3);
	head->left->parent = head;
	head->left->left = new Node(1);
	head->left->left->parent = head->left;
	head->left->left->right = new Node(2);
	head->left->left->right->parent = head->left->left;
	head->left->right = new Node(4);
	head->left->right->parent = head->left;
	head->left->right->right = new Node(5);
	head->left->right->right->parent = head->left->right;
	head->right = new Node(9);
	head->right->parent = head;
	head->right->left = new Node(8);
	head->right->left->parent = head->right;
	head->right->left->left = new Node(7);
	head->right->left->left->parent = head->right->left;
	head->right->right = new Node(10);
	head->right->right->parent = head->right;
	
	Node* test = head->left->left;
	Node*result1 = getsucessful(test);
	cout << result1->val << " " << endl;

	Node*test1 = head->left->left->right;
	Node*result2= getsucessful(test1);
	cout << result2->val << " " << endl;

	Node*test02 = head->right->right;
	Node*result3 = getsucessful(test02);
	cout << result3<< " " << endl;

	Node*test04= test = head->right->left->left;
	Node*result4 = getsucessful(test04);
	cout << result4->val << " " << endl;
    return 0;
}


Published 36 original articles · won praise 2 · Views 790

Guess you like

Origin blog.csdn.net/weixin_42076938/article/details/104735973