Download presentation
Presentation is loading. Please wait.
1
Binary Search Trees < > = 6 2 9 1 4 8 Binary Search Trees
Dictionaries 4/8/2019 7:45 AM Binary Search Trees < 6 2 > 9 1 4 = 8 Binary Search Trees
2
Binary Search Tree Properties
A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying the following property: Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u) key(v) key(w) External nodes do not store items An inorder traversal of a binary search trees visits the keys in increasing order 6 9 2 4 1 8 Binary Search Trees
3
Dictionaries 4/8/2019 7:45 AM Searching An Element To search for a key k, we trace a downward path starting at the root The next node visited depends on the outcome of the comparison of k with the key of the current node If we reach a leaf, the key is not found and we return NO_SUCH_KEY Example: findElement(4) Algorithm findElement(k, v) if T.isExternal (v) return NO_SUCH_KEY if k < key(v) return findElement(k, T.leftChild(v)) else if k = key(v) return element(v) else { k > key(v) } return findElement(k, T.rightChild(v)) < 6 2 > 9 1 4 = 8 Binary Search Trees
4
Insertion An Element <
6 To perform operation insertItem(k, o), we search for key k Assume k is not already in the tree, and let let w be the leaf reached by the search We insert k at node w and expand w into an internal node Example: insert 5 2 9 > 1 4 8 > w 6 2 9 1 4 8 w 5 Binary Search Trees
5
Deletion An Element 6 To perform operation removeElement(k), we search for key k Assume key k is in the tree, and let let v be the node storing k If node v has a leaf child w, we remove v and w from the tree with operation removeAboveExternal(w) Example: remove 4 < 2 9 > v 1 4 8 w 5 6 2 9 1 5 8 Binary Search Trees
6
Izmir University of Economics
Implementation Because a binary tree has at most two children, we can keep direct links to them. The declaration of tree nodes is similar in structure to that for doubly linked lists, in that a node is a structure consisting of the element information plus two pointers (left and right) to other nodes. Trees are generally drawn as circles connected by lines. NULL links are not explicitly drawn. Why? A binary tree with N nodes, has N+1 NULL links. Proof: Ni: #nodes with i children N0+N1+N2=N (1) // #nodes N1+2*N2=N-1 (2) // #edges multiply (1) by 2 and subtract (2) 2*N0+N1=2*N-(N-1)=N+1 Izmir University of Economics
7
Deletion (continued) v
1 v We consider the case where the key k to be removed is stored at a node v whose children are both internal we find the internal node w that follows v in an inorder traversal we copy key(w) into node v we remove node w and its left child z (which must be a leaf) by means of operation removeAboveExternal(z) Example: remove 3 3 2 8 6 9 w 5 z 1 v 5 2 8 6 9 Binary Search Trees
8
Performance Consider a dictionary with n items implemented by means of a binary search tree of height h the space used is O(n) methods findElement , insertItem and removeElement take O(h) time The height h is O(n) in the worst case and O(log n) in the best case Binary Search Trees
9
An Example: Expression Trees
The leaves of an expression tree are operands, such as constants or variable names, and the other nodes contain operators. Operator nodes might have 1 (unary minus), 2, or more than two children. We can evaluate an expression tree, T, by applying the operator at the root to the values obtained by recursively evaluating the left and right subtrees. Inorder traversal: produce an infix expression by first recursively processing left, then, output the operator at the root, and finally recursively process the right. the left subtree evaluates to a + (b * c) and the right subtree evaluates to ((d *e) + f )*g. The entire tree therefore represents (a + (b*c)) + (((d * e) + f)* g). Izmir University of Economics
10
Constructing an Expression Tree - I
We now give an algorithm to convert a postfix expression into an expression tree. Since we already have an algorithm to convert infix to postfix, we can generate expression trees from the two common types of input. The method we describe strongly resembles the postfix evaluation algorithm of Section stack=; while (!EndOf(expression)){ read(ch); if (isOperand(ch)) push(MakeNode(ch, NULL, NULL), stack); else if (isOperator(ch)){ T1=topAndPop(stack); T2=topAndPop(stack); push(MakeNode(ch, T2, T1), stack); } MakeNode(data,left,right) creates a struct tree_node and returns a pointer to it Izmir University of Economics
11
Constructing an Expression Tree - II
As an example, suppose the input is: a b + c d e + * * a b + c d e + * * a b + c d e + * * a b + c d e + * * a b + c d e + * * Izmir University of Economics
12
Constructing an Expression Tree - III
a b + c d e + * * a b + c d e + * * Izmir University of Economics
13
Izmir University of Economics
Homework Assignments 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.8, 4.31, 4.33, 4.40, 4.43, 4.45, You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me. Izmir University of Economics
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.