0% found this document useful (0 votes)
15 views3 pages

Demo OrderedTreeWithArrays

The document describes a program that implements an ordered tree using arrays. It defines methods for initializing the tree, checking if a node is empty, inserting nodes, and traversing the tree using preorder, inorder, and postorder traversal. It also includes a method for searching for a key within the tree. The main method demonstrates inserting nodes into the tree, traversing it, searching for keys, and printing out the tree structure stored in the arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Demo OrderedTreeWithArrays

The document describes a program that implements an ordered tree using arrays. It defines methods for initializing the tree, checking if a node is empty, inserting nodes, and traversing the tree using preorder, inorder, and postorder traversal. It also includes a method for searching for a key within the tree. The main method demonstrates inserting nodes into the tree, traversing it, searching for keys, and printing out the tree structure stored in the arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

//Ordered Tree with Arrays

//Alumno: Casimiro Granillo José Luis


//Estructura de Datos
//Docente: HERNANDEZ PEREZ ROBERTO
//07/06/2021
package demo_orderedtreewitharrays;

public class Demo_OrderedTreeWithArrays {


static final int Null = -1;
static final int MAX_TREE = 10;
static char Data[] = new char[MAX_TREE];
static int LeftSon[] = new int[MAX_TREE];
static int RightSon[] = new int[MAX_TREE];
static int Available = 0;

static void showArray(){


int i;
System.out.println("Idx Data LeftSon RightSon");
for(i = 0; i < Available; i++){
System.out.println(i + " \t" + Data[i] + " \t" + LeftSon[i] + " \t" +
RightSon[i]);
}
}

static int init(){


return Null;
}

static boolean isEmpty(int T){


boolean empty = false;

if(T == Null){
empty = true;
}
return empty;
}

static int getNode(char item){


int t;

if(Available < MAX_TREE){


t = Available;
Available = Available + 1;
Data[t] = item;
LeftSon[t] = Null;
RightSon[t] = Null;
}else{
System.out.println("There is not enough memory.");
t = -1;
}
return t;
}

static int insert(int T, char item){

if(isEmpty(T))
T = getNode(item);
else
if(item <= Data[T])
LeftSon[T] = insert(LeftSon[T], item);
else
RightSon[T] = insert(RightSon[T], item);
return T;
}

static void preorder(int T){


if(!isEmpty(T)){
System.out.print(Data[T] + " ");
preorder(LeftSon[T]);
preorder(RightSon[T]);
}
}

static void inorder(int T){


if(!isEmpty(T)){
inorder(LeftSon[T]);
System.out.print(Data[T] + " ");
inorder(RightSon[T]);
}
}

static void postorder(int T){


if(!isEmpty(T)){
postorder(LeftSon[T]);
postorder(RightSon[T]);
System.out.print(Data[T] + " ");
}
}

static boolean searchKey(int T, char key){


boolean found = false;

if(!isEmpty(T))
if((key == Data[T]))
found = true;
else
if(key < Data[T])
found = searchKey(LeftSon[T], key);
else
found = searchKey(RightSon[T], key);
return found;

public static void main(String[] args) {


int T = -1;

T = init();
T = insert(T, 'g');
T = insert(T, 'e');
T = insert(T, 'f');
T = insert(T, 'm');
T = insert(T, 'j');
T = insert(T, 's');
// T = insert(T, 'f');
T = insert(T, 'c');
T = insert(T, 'w');
T = insert(T, 'a');
System.out.print("Preorder: ");
preorder(T);
System.out.println();

System.out.print("Inorder: ");
inorder(T);
System.out.println();

System.out.print("Postorder: ");
postorder(T);
System.out.println();

if(searchKey(T, 'x'))
System.out.println("Key x is found.");
else
System.out.println("Key x is not found.");

if(searchKey(T, 'j'))
System.out.println("Key x is found.");
else
System.out.println("Key x is not found.");

System.out.println("Tree in the array: ");


showArray();
System.out.println("T = " + T + " and Available = " + Available + ".");
}

You might also like