0% found this document useful (0 votes)
8 views18 pages

Trees by Wilder

The document provides an overview of tree data structures in computer science, focusing on binary trees and their properties, such as full, complete, and balanced trees. It includes definitions, terminology, and typical operations performed on binary trees, along with example code for creating and manipulating tree nodes. The document emphasizes the recursive nature of binary tree definitions and algorithms.

Uploaded by

Niraj Lamsal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views18 pages

Trees by Wilder

The document provides an overview of tree data structures in computer science, focusing on binary trees and their properties, such as full, complete, and balanced trees. It includes definitions, terminology, and typical operations performed on binary trees, along with example code for creating and manipulating tree nodes. The document emphasizes the recursive nature of binary tree definitions and algorithms.

Uploaded by

Niraj Lamsal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

1

1 Trees
Object

Component

Button Checkbox Choice Label List Scrollbar TextComponent

TextField TextArea

Class Hierarchy in Java


2

x = 50 + ((2 × 10) + 10)

x = 80

50 + 30

20 + 10

2 × 10

Evaluation of a Mathematical Expression


3


A

❡ C❡
B

D❡ E❡

Simple Binary Tree


4

1.1 General Tree Terminology


• root node at the top of a tree.

• leaf a node with no children.

• parent the parent of a node is the node linked above it.

• sibling two nodes are siblings if they have the same parent.

• ancestor a node’s parent is its first ancestor.

• subtree any node in a tree that can be viewed as the root


of a new, smaller tree.
• left and right subtrees the nodes beginning with a left
or right child.
• height the maximum depth of any leaf.
5

1.2 Binary Trees


A binary tree is a finite set of nodes. The set might be empty
(no nodes, the empty tree). If the set is not empty, it follows
these rules:

1. There is one special node—the root.


2. Each node may be associated with up to two other different
nodes, called its left child and its right child. If a node c is
the child of another node p, then p is c’s parent.
3. If you start at a node and move to the node’s parent (if there
is one), then move again to that node’s parent, and keep
moving upward to each node’s parent, you will eventually
reach the root.
6

• Full binary Tree a binary tree of height h with no missing


nodes. All leaves have the same depth and every non-leaf
has two children.
• Complete binary Tree a binary tree of height h that is
full to level h − 1 and has level h filled in from left to right.
• Balanced Binary Tree a binary tree in which the left
and right subtrees of any node have heights that differ by
at most 1.
7

17 11

9 20 8 14
Full binary tree

Full binary Tree a binary tree of height h with no missing


nodes. All leaves have the same depth and every non-leaf has
two children.
8

17 11

9 20 8 14

13 16 15 NULL
Complete but not Full binary tree

Complete binary Tree a binary tree of height h that is full


to level h − 1 and has level h filled in from left to right.
9

17 11

9 20 8 14

13 NULL 16 15
Not Complete or Full binary tree
10

NULL 11

NULL 14

NULL 23
Unbalanced binary tree
11

NULL 11

14 NULL

NULL 23
Unbalanced binary tree (zipper tree)
12

1.3 Binary Tree Definition


A binary tree is either

1. An empty tree
2. Or it has a root and the remaining nodes are divided into
two disjoint sets named the left subtree and the right
subtree.

Since the definition of binary trees is recursive, most of the


algorithms used to manipulate binary trees are defined recur-
sively.
13

1.4 Typical Binary Tree Representation


A node in a binary tree can be defined using the following:

struct BinaryTreeNode
{
int data;
BinaryTreeNode *left;
BinaryTreeNode *right;
};

As we saw with nodes in linked lists, it is convenient to declare


a new data type for pointers to the nodes.

typedef struct BinaryTreeNode *BinaryTreeNodePtr;


14

1.5 Tree Operations


• Print a tree (traversal)

• Create a node

• Delete a node

• Test if a node is a leaf

• Search a tree

• Copy a tree
15

1.5.1 Creating a binary tree node

BinaryTreeNode *CreateNode( int newVal )


{
BinaryTreeNode *newNode = new BinaryTreeNode;

newNode->data = newVal;
newNode->left = NULL;
newNode->right = NULL;

return newNode;
}
16

BinaryTreeNode *CreateNode(
int newVal,
BinaryTreeNode *leftPtr = NULL,
BinaryTreeNode *rightPtr = NULL )
{
BinaryTreeNode *newNode = new BinaryTreeNode;

newNode->data = newVal;
newNode->left = leftPtr;
newNode->right = rightPtr;

return newNode;
}
17

1.5.2 Testing if a node is a leaf

bool IsLeaf( BinaryTreeNode *t )


{
return ((t->left == NULL) && (t->right == NULL));
}
18

1.6 Tree Size


int TreeSize( BinaryTreeNode *t )
{
if( t == NULL )
return 0;
else
return 1 +
TreeSize( t->left ) +
TreeSize( t->right );
}

You might also like