Print A Tree In Vertical Order

本文介绍了一种二叉树的垂直遍历算法,并通过示例详细解释了该算法的工作原理。文章提供了完整的C++代码实现,展示了如何确定每个节点与根节点的相对位置,进而按距离进行遍历。

参见  http://www.geeksforgeeks.org/print-binary-tree-vertical-order/

1 首先定义 Vertical Order,

           1
        /      \
       2        3
      /  \       /  \
     4   5    6    7
                 \      \
                  8     9 
               
 
The output of print this tree vertically will be:
4 :
2
1 5 6
3 8
7

因为

4 :         与root的距离为-2
2:          与root的距离为-1
1 5 6:    与root的距离为0
3 8:       与root的距离为1
7:          与root的距离为2
9 :         与root的距离为3

所以按照和root的距离从远到近的顺序排列下来,就是上面所示的打印结果。


2 代码如下:

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
    
    void get_min_max(TreeNode* root, int & min, int & max, int current_distance ) {
        if (root == NULL) return;
        if (current_distance < min) min = current_distance;
        if (current_distance > max) max = current_distance;
        get_min_max(root->left, min, max, current_distance - 1);
        get_min_max(root->right, min, max, current_distance + 1);
    }
    
    void helper(TreeNode* root, int line_index, int current_distance) {
        if (root == NULL) return;
        if(line_index == current_distance) {
            std::cout<<root->val<<", ";
        }
        helper(root->left, line_index, current_distance - 1);
        helper(root->right, line_index, current_distance + 1);
    }
    void vertical_tra(TreeNode* root) {
        int min;
        int max;
        int current_distance = 0;
        get_min_max(root, min, max, current_distance);
        std::cout<<"min = "<<min<<", max = "<<max<<std::endl;
        for (int i = min; i < max; ++i ) {
            helper(root, i, current_distance);
            std::cout<<std::endl;
        }
        
    }
};





 

def extract_complete_pdf_table(self, file_path, person_name): """全新方案:基于单元格定位提取PDF表格,确保与原Excel结构一致""" try: import pdfplumber # 更精确的表格提取库 all_records = [] standard_headers = None header_order = [] with pdfplumber.open(file_path) as pdf: # 遍历所有页面 for page_num, page in enumerate(pdf.pages, 1): # 提取页面中的所有表格(基于线条和单元格定位) tables = page.extract_tables({ "vertical_strategy": "lines", # 基于垂直线检测列 "horizontal_strategy": "lines", # 基于水平线检测行 "snap_tolerance": 3, # 允许的误差范围 "join_tolerance": 3 }) if not tables: continue # 处理每个表格 for table in tables: # 表格数据清洗 cleaned_table = [] for row in table: cleaned_row = [cell.strip() if cell and isinstance(cell, str) else "" for cell in row] if any(cleaned_row): # 跳过空行 cleaned_table.append(cleaned_row) if len(cleaned_table) < 2: # 至少需要表头+1行数据 continue # 确定表头(首行为表头) current_headers = cleaned_table[0] data_rows = cleaned_table[1:] # 首次识别表头时设置为标准表头 if not standard_headers: standard_headers = current_headers header_order = [h for h in current_headers if h] # 过滤空表头 # 提取数据行 for row in data_rows: # 跳过空行 if not any(row): continue # 检查是否包含目标人员(完整表格也需要过滤时使用) if person_name and not any(person_name in str(cell) for cell in row): continue record = {} # 严格按表头顺序映射数据 for i, header in enumerate(standard_headers): if i < len(row) and header: record[header] = row[i] elif header: record[header] = "" # 缺失数据补空 # 处理额外列(表头未定义的列) for i in range(len(standard_headers), len(row)): col_name = f"额外列_{i - len(standard_headers) + 1}" record[col_name] if col_name not in header_order: header_order.append(col_name) # 添加页码信息 record["页码"] = page_num if "页码" not in header_order: header_order.append("页码") record["_header_order"] = header_order.copy() all_records.append(record) # 确保所有记录列顺序完全一致 if all_records and header_order: for record in all_records: # 按标准顺序排列并补充缺失列 ordered_record = {col: record.get(col, "") for col in header_order} record.clear() record.update(ordered_record) record["_header_order"] = header_order.copy() return all_records if all_records else None except Exception as e: self.logger.error(f"PDF表格提取错误 {file_path}: {str(e)}") # 出错时回退到原方法 return self._original_extract_complete_pdf_table(file_path, person_name) def extract_employee_data_from_pdf(self, file_path, employee_name): """员工信息提取也使用新方案,确保与完整表格提取逻辑一致""" # 复用完整表格提取方法,只需传入人员姓名进行过滤 full_table = self.extract_complete_pdf_table(file_path, employee_name) return full_table 如何彻底解决提取信息后表头与原表格文件表头不一致的问题,特别是表头比较复杂,有多行、有合并单元格的情况下,还能保证提取的信息与原表头一致,更智能的表头解析能力。我的问题主要是针对PDF格式文件提取时,提高表头解析能力,避免出现表头不一致问题,excel格式文件目前没有问题。请给出修改后的完整代码。谢谢
09-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值