0% found this document useful (0 votes)
17 views

Chapt 5 Tree I

This chapter discusses binary trees, which are nonlinear data structures where each node has at most two children. It defines binary trees and their terminology, and describes how to represent binary trees using structs with left and right child pointers. The chapter also covers binary search trees, where data is organized to enable efficient searching by comparing values to the current node.

Uploaded by

adelino
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Chapt 5 Tree I

This chapter discusses binary trees, which are nonlinear data structures where each node has at most two children. It defines binary trees and their terminology, and describes how to represent binary trees using structs with left and right child pointers. The chapter also covers binary search trees, where data is organized to enable efficient searching by comparing values to the current node.

Uploaded by

adelino
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

Data Structures Using C++ 2E

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

Data Structures Using C++ 2E 2


A tree is a nonlinear data structure, compared to
arrays, linked lists, stacks and queues which are
linear data structures. A tree can be empty with no
nodes or a tree is a structure consisting of one node
called the root and zero or one or more subtrees.

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

A tree whose elements have at most 2 children is called a binary tree.


Since each element in a binary tree can have only 2 children, we
typically name them the left and right child.

A Binary Tree node contains following parts.

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

FIGURE 11-1 Binary tree


Data Structures Using C++ 2E 12
Binary Trees (cont’d.)

FIGURE 11-2 Binary tree with one, two, or three nodes

FIGURE 11-3 Various binary trees with three nodes


Data Structures Using C++ 2E 13
Binary Trees (cont’d.)
• Every node in a binary tree
– Has at most two children
• struct defining node of a binary tree
– For each node
• The data stored in info
• A pointer to the left child stored in llink
• A pointer to the right child stored in rlink

Data Structures Using C++ 2E 14


Binary Trees (cont’d.)
• Pointer to root node is stored outside the binary tree
– In pointer variable called the root
• Of type binaryTreeNode

FIGURE 11-4 Binary tree


Data Structures Using C++ 2E 15
16
• A binary tree is a non-linear linked list where
each node may point to two other nodes.

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.

Values are stored in a binary tree so


that a node's left child holds data
whose value is less than the node's
data, and the node's right child holds
data whose value is greater tan the
node's data.

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

You might also like