binary-trees - Meeting 5 September 8, 2004 Binary Search...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Meeting 5 September 8, 2004 Binary Search Trees (read Section 12 on Binary Search Trees in C ORMEN , L EISERSON , R IVEST , S TEIN ) As mentioned earlier, one of the main purposes of sort- ing is to facilitate fast searching. However, while a sorted sequence stored in a linear array is good for searching, it is expensive to add items to the sequence and to delete items from it. Binary search trees give you the best of both worlds: fast search and fast update. We Frst introduce bi- nary trees and then explain how to sort them and thus turn them into binary search trees. Binary trees. We have used binary trees repeatedly and now return to a more formal and systematic introduction. There are many ways to deFne a binary tree, and the most compact one is recursive: a binary tree is either empty or a node with a binary tree as left subtree and binary tree as right subtree. As an example consider an arith- metic expression used in the solution of quadratic equa- tions: . This expression can be represented by a binary tree as in ±igure 14. When we write pseudo-code / 2 1 ^ a c 4 2 b * * ^ Figure 14: The internal nodes are operators and the external nodes are operands. for trees we assume each node is a record storing an item and pointers to two children. struct Node item ; Node ; typedef Node Tree . Sometimes it is convenient to also store a pointer to the parent, but for now we will do without. Terminology and Properties. Terms for relations be- tween family members such as child , parent , sibling are also used for nodes in a tree, as illustrated in ±igure 15. In a tree, every node has one parent, except the root, which has no parent. A leaf is a node without children, and all other nodes are internal . A node is a descendent of if μ parent root ancestors descendents leaf sibling right child left child Figure 15: Every node has a parent and two children (some of which may be
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

binary-trees - Meeting 5 September 8, 2004 Binary Search...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online