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

23

The document contains a C# implementation of a binary search tree (ABB) with methods for inserting nodes and calculating the sum of the digits of each node's data. It defines a NodeABB class for tree nodes and an ABB class for managing the tree structure. The main program demonstrates adding nodes and printing their values.

Uploaded by

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

23

The document contains a C# implementation of a binary search tree (ABB) with methods for inserting nodes and calculating the sum of the digits of each node's data. It defines a NodeABB class for tree nodes and an ABB class for managing the tree structure. The main program demonstrates adding nodes and printing their values.

Uploaded by

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

TITLE

using System;

public class NodeABB{

public NodeABB left;

public NodeABB right;

public int data;

// Constructor

public NodeABB(int data){

this.data = data;

this.left = null;

this.right = null;

// Método para imprimir el valor del nodo

public void PrintNode(){

Console.WriteLine(data);

class ABB{

public NodeABB? head;

public ABB(){

head = null;

private void Insertar(int dato, NodeABB? p){

if(p==null){

head = new NodeABB(dato);

}else{

//ahora, si tenemos la cabecera

if(dato < p.data ){

if(p.right == null){

p.right = new NodeABB(dato);

}
else{

//tiene un hijo

Insertar(dato,p.right);

else if(dato > p.data ){

if(p.left == null){

p.left = new NodeABB(dato);

else{

//tiene un hijo

Insertar(dato,p.left);

public void Add(int dato){

Insertar(dato, head);

# implementacion

private int sumcif(int x) {

if (x < 10)

return x;

return x%10 + sumcif(x/10);

private int CifNod(NodeABB p) {

if (p == null) {

return 0;

}
return sumcif(p.data)+CifNod(p.left)+CifNod(p.right);

public int CifraNod()

CifNod(head);

class Program{

static void Main(string[] args){

ABB tree = new ABB();

tree.Add(10);

Console.WriteLine(tree.head.data);

tree.Add(8);

Console.WriteLine(tree.head.right.data);

tree.Add(12);

Console.WriteLine(tree.head.left.data);

tree.Add(15);

Console.WriteLine(tree.head.left.left.data);

tree.CifraNod();

You might also like