Ch4.4: array to Balanced tree and print each level

Q: an ordered array is given, need to convert into a balanced binary tree and then BFSly print each level as a D-dimentional linked list

Need to understand list, vector, BFS, recursion, scope of member function, call-by-reference, class, template.

The main method is the linked list form processing.

1: push the given head into the list, then push it into a vector.

2. if the vector is not empty, push all the left/right children of the head into a temporary list, after BFS this level, push the temp list into the previous vector as output.


Here is the source code and output.

// Ch4.4: Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i.e., if you have a tree with depth D, you’ll have D linked lists).

// based on Hawstein's solution
// and zyli's github

#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);		
	}

    vector
      
      
       
       
         > BFSlist(BinNode *head){ vector 
         
         
           > res; int level = 0; list 
          
            li; li.push_back(head); res.push_back(li); while(!res[level].empty()){ list 
           
             l; list 
            
              ::iterator it; for(it=res[level].begin(); it!=res[level].end(); ++it){ BinNode *n = *it; if(n->lChild) l.push_back(n->lChild); if(n->rChild) l.push_back(n->rChild); } ++level; res.push_back(l); } return res; } void print(vector 
              
              
                > res){ vector 
                
                
                  >::iterator vit; for(vit=res.begin(); vit!=res.end(); ++vit){ list 
                 
                   li = *vit; list 
                  
                    ::iterator lit; for(lit=li.begin(); lit!=li.end(); ++lit){ BinNode *n = *lit; cout< 
                   
                     key<<" "; } cout< 
                    
                      balanceTree(bt->getRoot(), NULL, arr, low, high); bt = new BTree(proot); bt->PrintPre(bt->getRoot()); cout << "\nD-dimesion linked list showoff-------\n"; vector 
                      
                      
                        > jerry = bt->BFSlist(proot); bt->print(jerry); } / /*output: Executing the program.... $demo Balanced tree in array format 10 2 1 5 7 15 13 21 22 D-dimesion linked list showoff------- 10 2 15 1 5 13 21 7 22 */ 
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值