Docsity

Binary Trees: Terminologies, Properties, and Applications, Slides of Data Structures and Algorithms

Partial preview of the text

Download Binary Trees: Terminologies, Properties, and Applications and more Slides Data Structures and Algorithms in PDF only on Docsity!

Trees

Chapter 8

Outline

 Trees Terminologies

 Binary Trees

 Binary Trees Applications

 Properties of Proper Binary Trees

 Representation for Binary Trees

 Linked List Representation for Binary Trees

 Array-Based Representation of Binary Trees

 Tree traversal

 Pre-order

 In-order

 Post-order

 Breadth-first Traversal

 Expression trees (Constructing, printing, evaluation)

Tree Terminology

Root

 node without parent (6)

 Internal node

 node with at least one child (10, 8)

Leaf (a.k.a. external node)

 node without children (14, 16, 12, 32)

Ancestors of a node

 parent, grandparent, grand-grandparent, etc.

Descendants of a node

 child, grandchild, …

Root Parent 1 4 Child siblings leaf

Tree Terminology

 Depth of a node

 Number of ancestors

 Height of a tree

 Maximum depth of any node

Subtree

 Tree consisting of a node and its descendants

Degree of a node

 Number of children

A
B C D
E F G H
I J K

Tree Height

The height of a node is the

length of the longest path under

’s subtree

 All leaves have a height of 0

height of tree = height of root =

depth of tree

Height = 3 Height = 1 5 Height = 1 Height = 2 Height = 0

Degree of a node

Degree = 2

Degree = 1

Degree = 0

Degree = 3

Binary Trees

Binary Trees

An ordered tree with the following

properties:

 Each internal node has at most two children

Each child node is labeled as being either a

left child or a right child.

 A left child precedes a right child in the order

of children of a node.

A binary tree is proper if each node has

either zero or two children.

 Known as full binary trees.

A
B C
D E F G
H I

Binary Tree Applications

Arithmetic Expression Tree

 Binary tree associated with an arithmetic expression

 internal nodes: operators

 external nodes: operands (variables or constants)

 Example: arithmetic expression tree for the expression

a 1

3 b

Properties of Binary Trees

Maximum number of nodes in the levels of a binary tree.

level d has at most nodes

Properties of Proper Binary Trees

if T is proper, then T has the

following properties:

In a nonempty proper binary

tree T, with external nodes and

internal nodes, we have =

Array-Based Representation of Binary Trees

Nodes are stored in an

array based on their

positions in the tree.

If p is the root of the tree

f (p) = 0.

If p is the left child of

position q

f (p) = 2 f (q)+1.

If p is the right child of

position q

f (p) = 2 f (q)+2.

Linked Binary Tree Structure

B

A D

C E

B

A D

C E

Linked Binary Tree Structure (Binary Tree Node)

public class BTNode { private E item; private BTNode left; private BTNode right; public BTNode() { item = null; left = right = null; } public void setItem(E e){ item = e; } public void setLeft(BTNode n){ left = n; } public void setRight(BTNode n){ right = n;} public E getItem(){ return item;} public BTNode getLeft(){ return left;} public BTNode getRight(){ return right;} public boolean isLeaf() { return (left == null && right == null); } }