Ch4.5: find the (in-order tranversely) successor of a node in BST.

There are 3 methods:

1. simply in order tranverse and get the ascending array, every next element is the successor of previous element.

2. It is Hawstein's method: There are 2 conditions: 

1st: have right child, go find the left least grand child.

2nd don't. go up to find the closest ancestor.


Here is whole code:

// Ch4.4: Write an algorithm to find the ‘next’ node (i.e., in-order successor) of a given node in a binary search tree where each node has a link to its parent.
// based on Hawstein's solution

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      

using namespace std;

class BinNode{
public:
    int key;
    BinNode *parent, *lChild, *rChild;
    BinNode(){}
	BinNode(int t): key(t), parent(NULL), lChild(NULL), rChild(NULL){}
};

class BTree{
private:
	BinNode *m_root;
public:
	BTree(): m_root(NULL){}
	BTree(BinNode *t): m_root(t){}

	BinNode* getRoot() const{return m_root;}
	
	BinNode* balanceTree(BinNode* pnode, BinNode *parent, int *arr, int low, int high){
		if(low>high || arr == NULL)
			return NULL;
		int mid = (low+high)>>1;
		pnode = new BinNode(arr[mid]);
		pnode->parent = parent;
		pnode->lChild = balanceTree(pnode->lChild, pnode, arr, low, mid-1);
		pnode->rChild = balanceTree(pnode->rChild, pnode, arr, mid+1, high);

		return pnode;
	}

	void PrintPre(BinNode *pnode)
	{
		if(pnode == NULL)
			return;
 
		cout << pnode->key << " ";
		PrintPre(pnode->lChild);
		PrintPre(pnode->rChild);		
	}

    BinNode* minimal(BinNode* pnode){
        if(pnode==NULL) return NULL;
        while(pnode->lChild){
            pnode = pnode->lChild;
        }
        return pnode;
    }

    BinNode* successor(BinNode* qnode){
        if(qnode==NULL) return NULL;
        if(qnode->rChild) return minimal(qnode->rChild);
        BinNode *r = qnode->parent;
        while(r && r->rChild==qnode){
            qnode = r;
            r = r->parent;
        }
        return r;
    }
};

int main(){
	int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    }; //{1, 2, 5, 7, 10, 13, 15, 21, 22};
	int low = 0, high = sizeof(arr)/sizeof(int)-1;
    cout <<"Balanced tree in array format\n";
	BTree *bt = new BTree();
	BinNode *proot = bt->balanceTree(bt->getRoot(), NULL, arr, low, high);
    bt = new BTree(proot);
    bt->PrintPre(bt->getRoot());
    cout << "pnode is:  "<< proot->key<
      
      
       
       successor(proot))->key<
       
       
      
      
     
     
    
    
   
   

In order to verify, need to be careful, it is based on BST. So every simulation should have a BST structure. Random Tree will fail in this method.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值