• Tidak ada hasil yang ditemukan

Tree implementation

N/A
N/A
Protected

Academic year: 2024

Membagikan "Tree implementation"

Copied!
13
0
0

Teks penuh

(1)

Tree implementation

ﻞﺼﻓ ﻢﺘﻔﻫ

: هدﺎﻴﭘ يزﺎﺳ

ﺖﺧرد رد

اوﺎﺟ

سرﺪﻣ :

هﺮﻬﺷ نﺎﻴﻧادﻮﺟآ

By: Shohreh Ajoudanian, Ph.D.

دﺎﺑآ ﻒﺠﻧ ﺪﺣاو ﻲﻣﻼﺳا دازآ هﺎﮕﺸﻧاد

(2)

Node سﻼﻛ

class Node {

int iData; // data used as key value double fData; // other data

node leftChild; // this node’s left child node rightChild; // this node’s right child public void displayNode()

{

// (see Listing 8.1 for method body) }

}

2

(3)

Tree سﻼﻛ

class Tree {

private Node root; // the only data field in Tree public void find(int key) {

… }

public void insert(int id, double dd) {

….

}

public void delete(int id) {

… }

// various other methods } // end class Tree

(4)

TreeApp سﻼﻛ ﺖﺧرد يور تﺎﻴﻠﻤﻋ مﺎﺠﻧا ياﺮﺑ

class TreeApp {

public static void main(String[] args) { Tree theTree = new Tree; // make a tree

theTree.insert(50, 1.5); // insert 3 nodes theTree.insert(25, 1.7);

theTree.insert(75, 1.9);

node found = theTree.find(25); // find node with key 25 if(found != null)

System.out.println(“Found the node with key 25”);

else

System.out.println(“Could not find node with key 25”);

} // end main()

} // end class TreeApp

4
(5)

ﺖﺧرد رد هﺮﮔ ﻚﻳ ندﺮﻛ اﺪﻴﭘ

(6)

ﻪﻣادا ...

public Node find(int key) // find node with given key { // (assumes non‐empty tree)

Node current = root; // start at root

while(current.iData != key) // while no match, { if(key < current.iData) // go left?

current = current.leftChild;

else

current = current.rightChild; // or go right?

if(current == null) // if no child, return null; // didn’t find it }

return current; // found it }

6

(7)

هﺮﮔ ﻚﻳ جرد

(8)

public void insert(int id, double dd){

Node newNode = new Node(); // make new node newNode.iData = id; // insert data

newNode.dData = dd;

if(root==null) // no node in root root = newNode;

else // root occupied

{ Node current = root; // start at root Node parent;

8

while(true) // (exits internally) {

parent = current;

if(id < current.iData) // go left?

{current = current.leftChild;

if(current == null) // if end of the line, { // insert on left

parent.leftChild = newNode;

return;

}

} // end if go left

(9)

else // or go right?

{current = current.rightChild;

if(current == null) // if end of the line { // insert on right

parent.rightChild = newNode;

return;

}

} // end else go right } // end while

} // end else not root

} // end insert()

(10)

ﺶﻳﺎﻤﻴﭘ inorder

private void inOrder(node localRoot) {

if(localRoot != null){

inOrder(localRoot.leftChild);

System.out.print(localRoot.iData + “ “);

inOrder(localRoot.rightChild);

} }

10

(11)

ﺖﺧرد يﺎﻫ هﺮﮔ داﺪﻌﺗ

public int getNumberOfNodes() {

int leftNumber = 0;

int rightNumber = 0;

if (leftChild != null)

leftNumber = leftChild.getNumberOfNodes();

if (rightChild != null)

rightNumber = rightChild.getNumberOfNodes();

return 1 + leftNumber + rightNumber;

} // end getNumberOfNodes

(12)

ﺖﺧرد ﻖﻤﻋ

private int getHeight(node) {

int height = 0;

if (node != null)

height = 1 + Math.max(getHeight(node.leftChild), getHeight(node.rightChild));

return height;

} // end getHeight

12

(13)

By: Shohreh Ajoudanian

Email: [email protected]

Data Structures

Referensi

Dokumen terkait

Abstract: This paper describes the implementation of data tree structure for the application information system of ulam bebantenan.. Ulam bebantenan complementary tool

C4.5 DECISION TREE IMPLEMENTATION IN SISTEM INFORMASI ZAKAT (SIZAKAT) TO AUTOMATICALLY DETERMINING THE AMOUNT OF ZAKAT RECEIVED BY MUSTAHIK.. David Bayu Ananda and