Array and binary tree, Summaries of Computer science

Helps in arrays and binary trees

Typology: Summaries

2025/2026

Uploaded on 03/09/2026

ngqabutho-ngwenya
ngqabutho-ngwenya 🇿🇦

1 document

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithm Design and Data Structures
1. Distinguish Between Dynamic and Static Data Structures
Data structure is a collection of different data items that are stored together as a single unit
and the operations allowable on them.
- It is the way in which data is organised or grouped into a single unit
Dynamic Data Structures
increase and decrease in size while the program is running, e.g. binary trees, linked lists,
etc.
uses space needed at a particular time, no limitations at all, unless computer memory is full
Advantages of Dynamic Data Structures
Only uses the space that is needed at any time
Makes efficient use of the memory, no spaces lie idle at any given point
Storage no-longer required can be returned to the system for other uses.
Does not allow overflow
There is effective use of resources as resources are allocated at run-time, as they are
required.
Disadvantages of Dynamic Data Structures
Complex and more difficult to program as software needs to keep track of its size and data
item locations at all times
Can be slow to implement searches
A linked list only allows serial access
Static data structure
do not change in size while the program is running, e.g arrays and fixed length records.
Most arrays are static, i.e., once you declare them, they cannot change in size.
It main advantage is that amount of storage is known and therefore is easier to program
Advantages of Static Data Structures
Easy to check for overflow
Memory allocation is fixed and therefore there is no problem with adding and removing
data items.
Compiler can allocate space during compilation
locations of each element is fixed and known by the program.
Easy to program as there is no need to check for data structure size at any given time/point
An array allows random access and is faster to access elements than in other data structures
Disadvantages of Static Data Structures
Programmer has to estimate maximum amount of space needed, which may be difficult
Can waste a lot of space if some space is left with no data entered into it.
** In Static data structure the size of the structure is fixed i.e. the data structure can be
modified but without changing the memory space allocated to it. In Dynamic data
structure the size of the structure in not fixed and can be modified during the operations
performed on it.
pf3
pf4
pf5

Partial preview of the text

Download Array and binary tree and more Summaries Computer science in PDF only on Docsity!

Algorithm Design and Data Structures

1. Distinguish Between Dynamic and Static Data Structures

Data structure is a collection of different data items that are stored together as a single unit and the operations allowable on them.

  • It is the way in which data is organised or grouped into a single unit

Dynamic Data Structures

  • increase and decrease in size while the program is running, e.g. binary trees, linked lists, etc.
  • uses space needed at a particular time, no limitations at all, unless computer memory is full Advantages of Dynamic Data Structures
  • Only uses the space that is needed at any time
  • Makes efficient use of the memory, no spaces lie idle at any given point
  • Storage no-longer required can be returned to the system for other uses.
  • Does not allow overflow
  • There is effective use of resources as resources are allocated at run-time, as they are required. Disadvantages of Dynamic Data Structures
  • Complex and more difficult to program as software needs to keep track of its size and data item locations at all times
  • Can be slow to implement searches
  • A linked list only allows serial access

Static data structure

  • do not change in size while the program is running, e.g arrays and fixed length records.
  • Most arrays are static, i.e., once you declare them, they cannot change in size.
  • It main advantage is that amount of storage is known and therefore is easier to program Advantages of Static Data Structures
  • Easy to check for overflow
  • Memory allocation is fixed and therefore there is no problem with adding and removing data items.
  • Compiler can allocate space during compilation
  • locations of each element is fixed and known by the program.
  • Easy to program as there is no need to check for data structure size at any given time/point
  • An array allows random access and is faster to access elements than in other data structures Disadvantages of Static Data Structures
  • Programmer has to estimate maximum amount of space needed, which may be difficult
  • Can waste a lot of space if some space is left with no data entered into it. ** In Static data structure the size of the structure is fixed i.e. the data structure can be modified but without changing the memory space allocated to it. In Dynamic data structure the size of the structure in not fixed and can be modified during the operations performed on it.

2. Perform Operations On Binary Trees and Arrays

Binary Trees

A binary tree is a data structure, consisting of a root node and zero, one or two sub-tree which are organised in a hierarchical way. Each node is a parent of at most two nodes. Constructing a Binary Tree Place first item into root node For subsequent items, start from root node Repeat If (new item > this node item) Then Follow right pointer Else Endif Follow left pointer Until pointer = 0 Place item at this node Binary tree traversal Tree traversal means walking through the trees’ structure such that each node is visited once. Common traversal methods are: Pre-Order, In-order and Post-Order Traversals. Pre-Order traversal The order of traversal is:

  • Visit the Root Node
  • Traverse the Left sub-tree
  • Traverse the Right sub-tree. In-Order Traversal The order of traversal is:
  • Traverse the Left sub-tree
  • Visit the Root node
  • Traverse the Right sub-tree. NB : In-order traversal prints items in ascending order or in alphabetical order In-order traversal

There are two possible cases with a node (successor or predecessor)

  • A node has right children: In this case the leftmost child of the right sub-tree will be successor.
  • A node has no children: Go to the parent node and find a node for which this node is part of left sub-tree. Example solution a) Produce a binary tree for the following array 50,60,30,43, b) Assuming that the element 50 is completely deleted and removed from the tree structure, draw a new binary tree structure after 50 has been removed

a) 50

b) The node to be deleted has two child nodes, therefore

  • Firstly find the successor of this node – to do this use the in-order traversal method to traverse the tree  19, 30, 43, 50, 60 } output obtained using in-order This is the successor of 50
  • Now delete the successor ( 60 ) from the tree and
  • Replace the node to be deleted ( 50 ) with the successor ( 60 )

Searching item from Binary Tree The algorithm is as follows: Enter item to search(this item) Start at root node Repeat If wanted item = this item Then Found = True Display Item Else If wanted Item >this item Then Follow right pointer EndIf Else EndIf Follow left pointer Until (Found=True or Null pointer is encountered) IMPLEMENTATION OF BINARY TREES USING ARRAYS Binary trees can be implemented using left and right pointers for each node. Each node will have the following:

  • Left pointer
  • Right pointer
  • Data item Let’s take the binary tree below for the list of elements: Long, Charlesworth, Illman, Hawthon, Todd, Youngman, Jones, Ravage. These can be illustrated as follows: Left Data Right Tree [1] 2 Long 5 Tree [2] 0 Charlesworth 3 Tree [3] 4 Illman 7 Tree [4] 0 Hawthorne 0 Tree [5] 8 Todd 6 Tree [6] 0 Youngman 0 Tree [7] 0 Jones 0 Tree [8] 0 Ravage 0
  • The pointer value 0 indicates a ‘nil’ pointer, thus a node will be pointing to nothing.
  • Tree[ 1 ].Left = 2
  • Tree[Tree[ 1 ].Left].Right = 3
  • Tree[ 6 ].Data = “Youngman