A binary search tree looks exactly like a binary tree except that it is in a specific order where the data in the left subtree are less than node.data and the data in right subtree are greater than node.data. A main method associated with the binary search tree is the contain method. This method takes a binary search tree and a number as inputs and return a bool that indicates whether the value is contained in the tree. The special order of the BST makes this method much more efficient than before when we can only use a binary tree because it avoids checking unnecessary nodes. With BST we can check whether an element is contained in about lgn nodes where we have to check n nodes with an ordinary tree.
A binary expression tree is a binary tree with node.data equals to an arithmetic expression. When evaluating a binary expression tree, we use recursions as we did in the last few weeks. If the expression is a leaf it returns its value otherwise it evaluates the left tree and the right tree and then combines them with the binary operator. We use the python built-in function eval here.
No comments:
Post a Comment