2 # ===-- tree_utils.py ---------------------------------------*- Python -*-===//
4 # The LLVM Compiler Infrastructure
6 # This file is distributed under the University of Illinois Open Source
7 # License. See LICENSE.TXT for details.
9 # ===---------------------------------------------------------------------===//
11 tree_utils.py - A set of functions for examining binary
12 search trees, based on the example search tree defined in
13 dictionary.c. These functions contain calls to LLDB API
14 functions, and assume that the LLDB Python module has been
17 For a thorough explanation of how the DFS function works, and
18 for more information about dictionary.c go to
19 http://lldb.llvm.org/scripting.html
23 def DFS (root, word, cur_path):
25 Recursively traverse a binary search tree containing
26 words sorted alphabetically, searching for a particular
27 word in the tree. Also maintains a string representing
28 the path from the root of the tree to the current node.
29 If the word is found in the tree, return the path string.
30 Otherwise return an empty string.
32 This function assumes the binary search tree is
33 the one defined in dictionary.c It uses LLDB API
34 functions to examine and traverse the tree nodes.
37 # Get pointer field values out of node 'root'
39 root_word_ptr = root.GetChildMemberWithName ("word")
40 left_child_ptr = root.GetChildMemberWithName ("left")
41 right_child_ptr = root.GetChildMemberWithName ("right")
43 # Get the word out of the word pointer and strip off
44 # surrounding quotes (added by call to GetSummary).
46 root_word = root_word_ptr.GetSummary()
47 end = len (root_word) - 1
48 if root_word[0] == '"' and root_word[end] == '"':
49 root_word = root_word[1:end]
50 end = len (root_word) - 1
51 if root_word[0] == '\'' and root_word[end] == '\'':
52 root_word = root_word[1:end]
54 # Main depth first search
58 elif word < root_word:
60 # Check to see if left child is NULL
62 if left_child_ptr.GetValue() == None:
65 cur_path = cur_path + "L"
66 return DFS (left_child_ptr, word, cur_path)
69 # Check to see if right child is NULL
71 if right_child_ptr.GetValue() == None:
74 cur_path = cur_path + "R"
75 return DFS (right_child_ptr, word, cur_path)
80 Recursively traverse a binary search tree, counting
81 the nodes in the tree. Returns the final count.
83 This function assumes the binary search tree is
84 the one defined in dictionary.c It uses LLDB API
85 functions to examine and traverse the tree nodes.
87 if (root.GetValue == None):
90 if (int (root.GetValue(), 16) == 0):
93 left_size = tree_size (root.GetChildAtIndex(1));
94 right_size = tree_size (root.GetChildAtIndex(2));
96 total_size = left_size + right_size + 1
100 def print_tree (root):
102 Recursively traverse a binary search tree, printing out
103 the words at the nodes in alphabetical order (the
104 search order for the binary tree).
106 This function assumes the binary search tree is
107 the one defined in dictionary.c It uses LLDB API
108 functions to examine and traverse the tree nodes.
110 if (root.GetChildAtIndex(1).GetValue() != None) and (int (root.GetChildAtIndex(1).GetValue(), 16) != 0):
111 print_tree (root.GetChildAtIndex(1))
113 print root.GetChildAtIndex(0).GetSummary()
115 if (root.GetChildAtIndex(2).GetValue() != None) and (int (root.GetChildAtIndex(2).GetValue(), 16) != 0):
116 print_tree (root.GetChildAtIndex(2))