Chapt 5 Tree I
Chapt 5 Tree I
Chapter 11
Binary Trees and B-Trees
Objectives
• Learn about binary trees
• Explore various binary tree traversal algorithms
• Learn how to organize data in a binary search tree
• Discover how to insert and delete items in a binary
search tree
3
Trees Definition • A tree T is a set of nodes storing
elements such that the nodes have a parent-child
relationship that satisfies the following
• if T is not empty, T has a special tree called the root
that has no parent
• each node v of T different than the root has a unique
parent node w; each node with parent w is a child of w
4
5
Terminology
• Root
• Parent
• Child
• Sibling
• External node
• Internal node
• Subtree
• Ancestor
• Descendant
7
8
9
Binary Tree Data Structure
1.Data
2.Pointer to left child
3.Pointer to right child
10
Binary Trees
• Definition: a binary tree, T, is either empty or such
that
– T has a special node called the root node
– T has two sets of nodes, LT and RT, called the left
subtree and right subtree of T, respectively
– LT and RT are binary trees
• Can be shown pictorially
– Parent, left child, right child
• Node represented as a circle
– Circle labeled by the node
Data Structures Using C++ 2E 11
Binary Trees (cont’d.)
• Root node drawn at the top
– Left child of the root node (if any)
• Drawn below and to the left of the root node
– Right child of the root node (if any)
• Drawn below and to the right of the root node
• Directed edge (directed branch): arrow
17
• It is anchored at the top by a tree pointer, which
is like the head pointer in a linked list.
• The first node in the list is called the root node.
• The root node has pointers to two other nodes,
which are called children, or child nodes.
18
• A node that has no children is called a leaf node.
• All pointers that do not point to a node are set to
NULL.
19
• Binary trees can be divided into subtrees. A
subtree is an entire branch of the tree, from one
particular node down.
20
• Binary trees are excellent data structures for
searching large amounts of information. They are
commonly used in database applications to
organize key values that index database records.
• When used to facilitate searches, a binary tree is
called a binary search tree.
21
• Information is stored in binary search trees in
a way that makes a binary search simple. For
example, look at Figure 3.
22
• It is also true that all the nodes to the left of a
node hold values less than the node's value.
Likewise, all the nodes to the right of a node
hold values that are greater than the node's
data.
• When an application is searching a binary tree,
it starts at the root node. If the root node does
not hold the search value, the application
branches either to the left or right child,
depending on whether the search value is less
than or grater than the value at the root node.
23
• This process continues until the value is
found. Figure-4 illustrates the search pattern
for finding the value P in the binary tree.
24