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

Binary Trees Notes

The document discusses binary trees, a hierarchical data structure where each parent node can have up to two child nodes, and outlines their applications, such as in syntax analysis and 3D video games. It provides pseudocode for searching and inserting items into a binary tree, along with identifier tables and trace tables for better understanding. Additionally, it emphasizes the importance of using objects, constructors, and recursion in implementing binary trees in programming.
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)
6 views

Binary Trees Notes

The document discusses binary trees, a hierarchical data structure where each parent node can have up to two child nodes, and outlines their applications, such as in syntax analysis and 3D video games. It provides pseudocode for searching and inserting items into a binary tree, along with identifier tables and trace tables for better understanding. Additionally, it emphasizes the importance of using objects, constructors, and recursion in implementing binary trees in programming.
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/ 10

DANIEL-TONY HOUSARD

Chapter 19/20: Binary Trees

1. Binary Tree
• A binary tree is another frequently used ADT.
• It is a hierarchical data structure in which each parent node can have a maximum of two child
nodes.
• There are many uses for binary trees; for example, they are used in
o syntax analysis
o compression algorithms
o 3D video games
• The binary tree for the data stored is sorted in ascending order.
• Each item is stored as a node.
• Each node can have up to two branches.
• If the value to be added is less than the current node branch left.
• If the value to be added is greater than or equal to the current node branch right.

• A binary tree can also be used to represent an arithmetic expression.


• Consider (a + b) * (c – a)

Copyright © 2016 - All Rights Reserved - ONLINE Computer Training Center 1


DANIEL-TONY HOUSARD

ACTIVITY 19J
Draw the binary tree for the expression (x – y) / ( x * y + z)

• The data structure for an ordered binary tree can be created in pseudocode as follows:

ACTIVITY 19K
Create the data structure in pseudocode for a binary tree to store a list of names. Your list must be
able to store at least 50 names.

• The populated contents of the data structure myTree is shown below:

• The root pointer points to the first node in a binary tree.


• A null pointer is a value stored in the left or right pointer in a binary tree to indicate that there are
no nodes below this node on the left or right.

Copyright © 2016 - All Rights Reserved - ONLINE Computer Training Center 2


DANIEL-TONY HOUSARD

2. Finding an Item in a Binary Tree


• The algorithm to find if an item is in the binary tree myTree and return the pointer to its node if
found or a null pointer if not found, could be written as a function in pseudocode:

• Here is the identifier table for the binary tree search algorithm shown above.

Identifier Description
myTree Tree to be searched
node ADT for tree
rootPointer Pointer to the start of the tree
leftPointer Pointer to the left branch
rightPointer Pointer to the right branch
nullPointer Null pointer set to −1
itemPointer Pointer to current item
itemSearch Item being searched for

• The trace table below shows the algorithm being used to search for 42 in myTree.

rootPointer itemPointer itemSearch


0 0 42
2
3

Copyright © 2016 - All Rights Reserved - ONLINE Computer Training Center 3


DANIEL-TONY HOUSARD

ACTIVITY 19L
Use the algorithm to search for 55 and 75 in myTree. Show the results of each search in a trace table.

3. Inserting items into a Binary Tree


• The binary tree. needs free nodes to add new items.
• For example, myTree below, now has room for 12 items.
• The last three nodes have not been filled yet.
• There is a pointer to the next free node and the free nodes are set up like a heap in a linked list,
using the left pointer.

• The algorithm to insert an item at a new node in the binary tree myTree could be written as a
procedure in pseudocode as shown below:

Copyright © 2016 - All Rights Reserved - ONLINE Computer Training Center 4


DANIEL-TONY HOUSARD

Copyright © 2016 - All Rights Reserved - ONLINE Computer Training Center 5


DANIEL-TONY HOUSARD
• Here is the identifier table:

Identifier Description
myTree Tree to be searched
node ADT for tree
rootPointer Pointer to the start of the tree
leftPointer Pointer to the left branch
rightPointer Pointer to the right branch
nullPointer Null pointer set to −1
itemPointer Pointer to current item
itemAdd Item to add to tree
nextFreePointer Pointer to next free node
itemAddPointer Pointer to position in tree to store item to be added
oldPointer Pointer to leaf node that is going to point to item added
leftBranch Flag to identify whether to go down the left branch or the right branch

• The trace table below shows the algorithm being used to add 18 to myTree.

leftBranch nextFreePointer itemAddPointer rootPointer itemAdd itemPointer oldPointer


Already set to 9 9 Already set to 0 18
10 0 0
TRUE 1 1
TRUE 4 4
FALSE 7 7
-1

• The tree, myTree will now be as shown below:

Copyright © 2016 - All Rights Reserved - ONLINE Computer Training Center 6


DANIEL-TONY HOUSARD

ACTIVITY 19M
Use the algorithm to add 25 to myTree. Show this in a trace table and show myTree once 25 has been
added.

4. Writing a program for a Binary Tree


• Binary trees are best implemented using:
o objects - tree and node
o constructors - adding a new node to the tree
o containment - the tree contains nodes
o functions - search the binary tree for an item
o procedures - insert a new item in the binary tree
o recursion

• Binary tree data structure – Class node


o VB with a recursive definition of node to allow for a tree of any size

• Binary tree data structure – Class tree


o VB uses Nothing for null pointers

Copyright © 2016 - All Rights Reserved - ONLINE Computer Training Center 7


DANIEL-TONY HOUSARD
• Add integer to binary tree
o VB showing a recursive procedure to insert a new node

• Search for integer in binary tree


o VB – the function returns the value searched for if it is found, otherwise it returns Nothing

Copyright © 2016 - All Rights Reserved - ONLINE Computer Training Center 8


DANIEL-TONY HOUSARD
ACTIVITY 19J - ANSWER

ACTIVITY 19K - ANSWER

ACTIVITY 19L - ANSWER

Copyright © 2016 - All Rights Reserved - ONLINE Computer Training Center 9


DANIEL-TONY HOUSARD
ACTIVITY 19M - ANSWER

Copyright © 2016 - All Rights Reserved - ONLINE Computer Training Center 10

You might also like