简体   繁体   English

查找二叉树的父节点函数

[英]Find parent node function for binary tree

is it possible to write a function to return the parent node of a given node for a binary tree? 是否有可能编写一个函数来返回给定节点的二叉树的父节点?

BinaryTree *search_val(BinaryTree *bt, int val)
{
    //temp pointer 
    BinaryTree* temp = NULL;
    if(!bt->isEmpty())
    {
        //check if root is equal to value and return root if true
        if(bt->getData() == val)
        {
            return bt;
        }
        else
        {
            //search left side
            temp = search_val(bt->left(), val);
            //if not found in left, search right
            if (temp == NULL)
            {
                temp = search_val(bt->right(), val);
            }
            return temp;
        }
        return NULL;
    }
    return NULL;
 }

I just have this search function at the moment. 我现在只有这个搜索功能。 I got it from here actually. 我实际上是从这里得到的。 So I'm trying to convert this to search for the parent of a node. 所以我试图将其转换为搜索节点的父级。 The parameters will be the root node and the node whose parent we want. 参数将是根节点和我们想要其父节点的节点。 Is that even possible? 那有可能吗? I just need some hints to get started then I'll post my code. 我只需要一些提示即可开始使用,然后我将发布代码。 The purpose of creating this function is because I have a delete leaf node function that works almost perfectly....the only problem is that when I print all nodes after deleting, the supposedly deleted node still appears. 创建此功能的目的是因为我有一个删除叶节点功能,该功能几乎可以完美运行。...唯一的问题是,当删除后打印所有节点时,假定的已删除节点仍会出现。 I'm sure it's because the parent node is still linked to it in main. 我确定这是因为父节点仍在main中链接到它。 Here's my delete leaf node function: 这是我的删除叶子节点函数:

void delete_leaf_node(BinaryTree *bt, int val)
{
    BinaryTree *temp;
    temp = search_val(bt, val);
    //If node does not exist in the tree, inform the user
    if(temp == NULL)
    {
        cout << "\n   " << val << " was not found in the tree" << endl; 
    }
    //Check if node is a leaf
    else if(temp->isLeaf())
    {
        delete temp;
        cout << "\n   Leaf " << temp->getData() << " deleted" << endl;
    }
    //Inform user that node is not a leaf
    else 
        cout << "\n   " << temp->getData() << " is not a Leaf" << endl; 
    //Display using In Order Traversal to see that the node was actually deleted    
    cout << "\n   In Order Traversal after deleting: " << endl << "\n   ";
    inOrderTraverse(bt);
    cout << endl;
} 

I hope I'm making sense to someone...sorry I tried to shorten the question but couldn't. 我希望我对某人有意义...抱歉,我试图简化问题,但未能如此。

BinaryTree.h file: BinaryTree.h文件:

using namespace std;

//BinaryTree class
class BinaryTree{
    public:
        BinaryTree();
        bool isEmpty();
        bool isLeaf();
        int getData();
        void insert(const int &DATA);
        BinaryTree *left();
        BinaryTree *right();
        void makeLeft(BinaryTree *bt);
        void makeRight(BinaryTree *bt);
    private:
        bool nullTree;
        int treeData;
        BinaryTree *leftTree;
        BinaryTree *rightTree;
};

BinaryTree.cpp file: BinaryTree.cpp文件:

#include <iostream>
#include "BinaryTree.h"

using namespace std;

//constructor
BinaryTree::BinaryTree()
{
    nullTree = true;
    leftTree = NULL;
    rightTree = NULL;
}

/*
  is_empty function for BinaryTree class. Does not take any parameters. 
  Returns true if tree is empty and false otherwise.
*/
bool BinaryTree::isEmpty()
{
    return nullTree;
}

/*
  is_leaf function for BinaryTree class. Does not take any parameters. 
  Returns true if node has no children and false otherwise.
*/
bool BinaryTree::isLeaf()
{
    return ((this->leftTree->treeData == 0) && (this->rightTree->treeData == 0));
}

/*
  getData function for BinaryTree class. Does not take any parameters. 
  Returns treeData value.
*/
int BinaryTree::getData()
{
    if(!isEmpty());
    return treeData;
}

/*
  insert function for BinaryTree class. Takes one parameter, passed by
  reference. Returns true if node has no children and false otherwise.
*/
void BinaryTree::insert(const int &DATA)
{
    //create empty children and insert DATA
    treeData = DATA;
    if(nullTree) 
    {
        nullTree = false;
        leftTree = new BinaryTree;
        rightTree = new BinaryTree;
    }
}

/*
  left function for BinaryTree class. It points to the left node.
  Does not take any parameters. Returns left node.
*/
BinaryTree *BinaryTree::left()
{
    if(!isEmpty());
    return leftTree;
}

/*
  right function for BinaryTree class. It points to the right node.
  Does not take any parameters. Returns right node.
*/
BinaryTree *BinaryTree::right()
{
    if(!isEmpty());
    return rightTree;
}

/*
  makeLeft function for BinaryTree class. Takes a pointer to a tree node as a parameter. 
  makes the parameter the left child of a node. Does not return any value
*/
void BinaryTree::makeLeft(BinaryTree *bt)
{
    if(!isEmpty());
    leftTree = bt;
}

/*
  makeRight function for BinaryTree class. Takes a pointer to a tree node as a parameter. 
  makes the parameter the right child of a node. Does not return any value
*/
void BinaryTree::makeRight(BinaryTree *bt)
{
    if (!isEmpty());
    rightTree = bt;
}

Thanks 谢谢

That depends on your BinaryTree implementation. 这取决于您的BinaryTree实现。 As far as I see, if you don't save a reference inside each node to his parent, you can't directly access to it when deleting 据我了解,如果您没有在每个节点内保存对父节点的引用,则删除时将无法直接访问它

Edit 编辑

You can modify your BinaryTree class with: 您可以使用以下方法修改BinaryTree类:

class BinaryTree{
    public:
        BinaryTree();
        bool isEmpty();
        bool isLeaf();
        int getData();
        void insert(const int &DATA);
        BinaryTree *left();
        BinaryTree *right();
        void makeLeft(BinaryTree *bt);
        void makeRight(BinaryTree *bt);

        void setParent(BinaryTree *parent);
        BinaryTree* getParent();
    private:
        bool nullTree;
        int treeData;
        BinaryTree *leftTree;
        BinaryTree *rightTree;

        BinaryTree* parent;
};

Then in your .cpp : 然后在您的.cpp

BinaryTree::BinaryTree()
{
    nullTree = true;
    leftTree = NULL;
    rightTree = NULL;
    parent = NULL;
}

void BinaryTree::setParent(BinaryTree *parent){
    this->parent = parent;
}

BinaryTree* BinaryTree::getParent(){
    return parent;
}

Your delete function will look like: 您的删除功能将如下所示:

void delete_leaf_node(BinaryTree *bt, int val)
{
    BinaryTree *temp;
    temp = search_val(bt, val);
    //If node does not exist in the tree, inform the user
    if(temp == NULL)
    {
        cout << "\n   " << val << " was not found in the tree" << endl; 
    }
    //Check if node is a leaf
    else if(temp->isLeaf())
    {
        // You must distinguish which child you are
        BinaryTree* parent = temp->getParent();
        BinaryTree* leftChild = parent->left;
        BinaryTree* rightChild = parent->right;
        if(leftChild == temp){
            parent->left = null;
        }
        if(rightChild == temp){
            parent->right = null;
        }
        delete temp;
        cout << "\n   Leaf " << temp->getData() << " deleted" << endl;
    }
    //Inform user that node is not a leaf
    else 
        cout << "\n   " << temp->getData() << " is not a Leaf" << endl; 
    //Display using In Order Traversal to see that the node was actually deleted    
    cout << "\n   In Order Traversal after deleting: " << endl << "\n   ";
    inOrderTraverse(bt);
    cout << endl;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM