12e9dd93eSCaroline Tice""" 22e9dd93eSCaroline Tice# ===-- tree_utils.py ---------------------------------------*- Python -*-===// 32e9dd93eSCaroline Tice# 42946cd70SChandler Carruth# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 52946cd70SChandler Carruth# See https://llvm.org/LICENSE.txt for license information. 62946cd70SChandler Carruth# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 72e9dd93eSCaroline Tice# 82e9dd93eSCaroline Tice# ===---------------------------------------------------------------------===// 92e9dd93eSCaroline Tice 102e9dd93eSCaroline Ticetree_utils.py - A set of functions for examining binary 112e9dd93eSCaroline Ticesearch trees, based on the example search tree defined in 122e9dd93eSCaroline Ticedictionary.c. These functions contain calls to LLDB API 132e9dd93eSCaroline Ticefunctions, and assume that the LLDB Python module has been 142e9dd93eSCaroline Ticeimported. 152e9dd93eSCaroline Tice 162e9dd93eSCaroline TiceFor a thorough explanation of how the DFS function works, and 172e9dd93eSCaroline Ticefor more information about dictionary.c go to 182e9dd93eSCaroline Ticehttp://lldb.llvm.org/scripting.html 192e9dd93eSCaroline Tice""" 202e9dd93eSCaroline Tice 21*525cd59fSSerge Gueltonfrom __future__ import print_function 22*525cd59fSSerge Guelton 232e9dd93eSCaroline Tice 242e9dd93eSCaroline Ticedef DFS(root, word, cur_path): 252e9dd93eSCaroline Tice """ 262e9dd93eSCaroline Tice Recursively traverse a binary search tree containing 272e9dd93eSCaroline Tice words sorted alphabetically, searching for a particular 282e9dd93eSCaroline Tice word in the tree. Also maintains a string representing 292e9dd93eSCaroline Tice the path from the root of the tree to the current node. 302e9dd93eSCaroline Tice If the word is found in the tree, return the path string. 312e9dd93eSCaroline Tice Otherwise return an empty string. 322e9dd93eSCaroline Tice 332e9dd93eSCaroline Tice This function assumes the binary search tree is 342e9dd93eSCaroline Tice the one defined in dictionary.c It uses LLDB API 352e9dd93eSCaroline Tice functions to examine and traverse the tree nodes. 362e9dd93eSCaroline Tice """ 372e9dd93eSCaroline Tice 382e9dd93eSCaroline Tice # Get pointer field values out of node 'root' 392e9dd93eSCaroline Tice 402e9dd93eSCaroline Tice root_word_ptr = root.GetChildMemberWithName("word") 412e9dd93eSCaroline Tice left_child_ptr = root.GetChildMemberWithName("left") 422e9dd93eSCaroline Tice right_child_ptr = root.GetChildMemberWithName("right") 432e9dd93eSCaroline Tice 442e9dd93eSCaroline Tice # Get the word out of the word pointer and strip off 452e9dd93eSCaroline Tice # surrounding quotes (added by call to GetSummary). 462e9dd93eSCaroline Tice 472e9dd93eSCaroline Tice root_word = root_word_ptr.GetSummary() 482e9dd93eSCaroline Tice end = len(root_word) - 1 492e9dd93eSCaroline Tice if root_word[0] == '"' and root_word[end] == '"': 502e9dd93eSCaroline Tice root_word = root_word[1:end] 512e9dd93eSCaroline Tice end = len(root_word) - 1 522e9dd93eSCaroline Tice if root_word[0] == '\'' and root_word[end] == '\'': 532e9dd93eSCaroline Tice root_word = root_word[1:end] 542e9dd93eSCaroline Tice 552e9dd93eSCaroline Tice # Main depth first search 562e9dd93eSCaroline Tice 572e9dd93eSCaroline Tice if root_word == word: 582e9dd93eSCaroline Tice return cur_path 592e9dd93eSCaroline Tice elif word < root_word: 602e9dd93eSCaroline Tice 612e9dd93eSCaroline Tice # Check to see if left child is NULL 622e9dd93eSCaroline Tice 63b9c1b51eSKate Stone if left_child_ptr.GetValue() is None: 642e9dd93eSCaroline Tice return "" 652e9dd93eSCaroline Tice else: 662e9dd93eSCaroline Tice cur_path = cur_path + "L" 672e9dd93eSCaroline Tice return DFS(left_child_ptr, word, cur_path) 682e9dd93eSCaroline Tice else: 692e9dd93eSCaroline Tice 702e9dd93eSCaroline Tice # Check to see if right child is NULL 712e9dd93eSCaroline Tice 72b9c1b51eSKate Stone if right_child_ptr.GetValue() is None: 732e9dd93eSCaroline Tice return "" 742e9dd93eSCaroline Tice else: 752e9dd93eSCaroline Tice cur_path = cur_path + "R" 762e9dd93eSCaroline Tice return DFS(right_child_ptr, word, cur_path) 772e9dd93eSCaroline Tice 782e9dd93eSCaroline Tice 792e9dd93eSCaroline Ticedef tree_size(root): 802e9dd93eSCaroline Tice """ 812e9dd93eSCaroline Tice Recursively traverse a binary search tree, counting 822e9dd93eSCaroline Tice the nodes in the tree. Returns the final count. 832e9dd93eSCaroline Tice 842e9dd93eSCaroline Tice This function assumes the binary search tree is 852e9dd93eSCaroline Tice the one defined in dictionary.c It uses LLDB API 862e9dd93eSCaroline Tice functions to examine and traverse the tree nodes. 872e9dd93eSCaroline Tice """ 88b9c1b51eSKate Stone if (root.GetValue is None): 892e9dd93eSCaroline Tice return 0 902e9dd93eSCaroline Tice 912e9dd93eSCaroline Tice if (int(root.GetValue(), 16) == 0): 922e9dd93eSCaroline Tice return 0 932e9dd93eSCaroline Tice 94b9c1b51eSKate Stone left_size = tree_size(root.GetChildAtIndex(1)) 95b9c1b51eSKate Stone right_size = tree_size(root.GetChildAtIndex(2)) 962e9dd93eSCaroline Tice 972e9dd93eSCaroline Tice total_size = left_size + right_size + 1 982e9dd93eSCaroline Tice return total_size 992e9dd93eSCaroline Tice 1002e9dd93eSCaroline Tice 1012e9dd93eSCaroline Ticedef print_tree(root): 1022e9dd93eSCaroline Tice """ 1032e9dd93eSCaroline Tice Recursively traverse a binary search tree, printing out 1042e9dd93eSCaroline Tice the words at the nodes in alphabetical order (the 1052e9dd93eSCaroline Tice search order for the binary tree). 1062e9dd93eSCaroline Tice 1072e9dd93eSCaroline Tice This function assumes the binary search tree is 1082e9dd93eSCaroline Tice the one defined in dictionary.c It uses LLDB API 1092e9dd93eSCaroline Tice functions to examine and traverse the tree nodes. 1102e9dd93eSCaroline Tice """ 111b9c1b51eSKate Stone if (root.GetChildAtIndex(1).GetValue() is not None) and ( 112b9c1b51eSKate Stone int(root.GetChildAtIndex(1).GetValue(), 16) != 0): 1132e9dd93eSCaroline Tice print_tree(root.GetChildAtIndex(1)) 1142e9dd93eSCaroline Tice 115*525cd59fSSerge Guelton print(root.GetChildAtIndex(0).GetSummary()) 1162e9dd93eSCaroline Tice 117b9c1b51eSKate Stone if (root.GetChildAtIndex(2).GetValue() is not None) and ( 118b9c1b51eSKate Stone int(root.GetChildAtIndex(2).GetValue(), 16) != 0): 1192e9dd93eSCaroline Tice print_tree(root.GetChildAtIndex(2)) 120