2016-09-07 04:57:50 +08:00
|
|
|
"""
|
2011-06-03 07:23:47 +08:00
|
|
|
# ===-- tree_utils.py ---------------------------------------*- Python -*-===//
|
|
|
|
#
|
|
|
|
# The LLVM Compiler Infrastructure
|
|
|
|
#
|
|
|
|
# This file is distributed under the University of Illinois Open Source
|
|
|
|
# License. See LICENSE.TXT for details.
|
|
|
|
#
|
|
|
|
# ===---------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
tree_utils.py - A set of functions for examining binary
|
2016-09-07 04:57:50 +08:00
|
|
|
search trees, based on the example search tree defined in
|
2011-06-03 07:23:47 +08:00
|
|
|
dictionary.c. These functions contain calls to LLDB API
|
|
|
|
functions, and assume that the LLDB Python module has been
|
|
|
|
imported.
|
|
|
|
|
|
|
|
For a thorough explanation of how the DFS function works, and
|
|
|
|
for more information about dictionary.c go to
|
|
|
|
http://lldb.llvm.org/scripting.html
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def DFS(root, word, cur_path):
|
2011-06-03 07:23:47 +08:00
|
|
|
"""
|
|
|
|
Recursively traverse a binary search tree containing
|
|
|
|
words sorted alphabetically, searching for a particular
|
|
|
|
word in the tree. Also maintains a string representing
|
|
|
|
the path from the root of the tree to the current node.
|
|
|
|
If the word is found in the tree, return the path string.
|
|
|
|
Otherwise return an empty string.
|
|
|
|
|
|
|
|
This function assumes the binary search tree is
|
|
|
|
the one defined in dictionary.c It uses LLDB API
|
|
|
|
functions to examine and traverse the tree nodes.
|
|
|
|
"""
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2011-06-03 07:23:47 +08:00
|
|
|
# Get pointer field values out of node 'root'
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
root_word_ptr = root.GetChildMemberWithName("word")
|
|
|
|
left_child_ptr = root.GetChildMemberWithName("left")
|
|
|
|
right_child_ptr = root.GetChildMemberWithName("right")
|
2011-06-03 07:23:47 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
# Get the word out of the word pointer and strip off
|
2011-06-03 07:23:47 +08:00
|
|
|
# surrounding quotes (added by call to GetSummary).
|
|
|
|
|
|
|
|
root_word = root_word_ptr.GetSummary()
|
2016-09-07 04:57:50 +08:00
|
|
|
end = len(root_word) - 1
|
2011-06-03 07:23:47 +08:00
|
|
|
if root_word[0] == '"' and root_word[end] == '"':
|
|
|
|
root_word = root_word[1:end]
|
2016-09-07 04:57:50 +08:00
|
|
|
end = len(root_word) - 1
|
2011-06-03 07:23:47 +08:00
|
|
|
if root_word[0] == '\'' and root_word[end] == '\'':
|
|
|
|
root_word = root_word[1:end]
|
|
|
|
|
|
|
|
# Main depth first search
|
|
|
|
|
|
|
|
if root_word == word:
|
|
|
|
return cur_path
|
|
|
|
elif word < root_word:
|
|
|
|
|
|
|
|
# Check to see if left child is NULL
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if left_child_ptr.GetValue() is None:
|
2011-06-03 07:23:47 +08:00
|
|
|
return ""
|
|
|
|
else:
|
|
|
|
cur_path = cur_path + "L"
|
2016-09-07 04:57:50 +08:00
|
|
|
return DFS(left_child_ptr, word, cur_path)
|
2011-06-03 07:23:47 +08:00
|
|
|
else:
|
|
|
|
|
|
|
|
# Check to see if right child is NULL
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if right_child_ptr.GetValue() is None:
|
2011-06-03 07:23:47 +08:00
|
|
|
return ""
|
|
|
|
else:
|
|
|
|
cur_path = cur_path + "R"
|
2016-09-07 04:57:50 +08:00
|
|
|
return DFS(right_child_ptr, word, cur_path)
|
|
|
|
|
2011-06-03 07:23:47 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def tree_size(root):
|
2011-06-03 07:23:47 +08:00
|
|
|
"""
|
|
|
|
Recursively traverse a binary search tree, counting
|
|
|
|
the nodes in the tree. Returns the final count.
|
|
|
|
|
|
|
|
This function assumes the binary search tree is
|
|
|
|
the one defined in dictionary.c It uses LLDB API
|
|
|
|
functions to examine and traverse the tree nodes.
|
|
|
|
"""
|
2016-09-07 04:57:50 +08:00
|
|
|
if (root.GetValue is None):
|
2011-06-03 07:23:47 +08:00
|
|
|
return 0
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (int(root.GetValue(), 16) == 0):
|
2011-06-03 07:23:47 +08:00
|
|
|
return 0
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
left_size = tree_size(root.GetChildAtIndex(1))
|
|
|
|
right_size = tree_size(root.GetChildAtIndex(2))
|
2011-06-03 07:23:47 +08:00
|
|
|
|
|
|
|
total_size = left_size + right_size + 1
|
|
|
|
return total_size
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
def print_tree(root):
|
2011-06-03 07:23:47 +08:00
|
|
|
"""
|
|
|
|
Recursively traverse a binary search tree, printing out
|
|
|
|
the words at the nodes in alphabetical order (the
|
|
|
|
search order for the binary tree).
|
|
|
|
|
|
|
|
This function assumes the binary search tree is
|
|
|
|
the one defined in dictionary.c It uses LLDB API
|
|
|
|
functions to examine and traverse the tree nodes.
|
|
|
|
"""
|
2016-09-07 04:57:50 +08:00
|
|
|
if (root.GetChildAtIndex(1).GetValue() is not None) and (
|
|
|
|
int(root.GetChildAtIndex(1).GetValue(), 16) != 0):
|
|
|
|
print_tree(root.GetChildAtIndex(1))
|
2011-06-03 07:23:47 +08:00
|
|
|
|
|
|
|
print root.GetChildAtIndex(0).GetSummary()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (root.GetChildAtIndex(2).GetValue() is not None) and (
|
|
|
|
int(root.GetChildAtIndex(2).GetValue(), 16) != 0):
|
|
|
|
print_tree(root.GetChildAtIndex(2))
|