Java版:
二叉树:
myBtree类:
public class myBtree {
private Node root;
class Node{
private int data;
private Node left;
private Node right;
public void zhongxu() {
if(this.left!=null){
this.left.zhongxu();
}
System.out.println(this.data) ;
if(this.right!=null){
this.right.zhongxu();
}
}
public void add(Node node) {
if(this.data>node.data){
if (this.left==null){
this.left=node;
}else {
left.add(node);
}
}
if(this.data<node.data){
if (this.right==null){
this.right=node;
}else {
right.add(node);
}
}
}
}
public void add(int data) {
Node node = new Node();
node.data=data;
if (root==null){
root=node;
}else{
root.add(node);
}
}
public void zhongxu() {
root.zhongxu();
}
}
myBtreeTest类:
public class myBtreeTest {
public static void main(String[] args) {
myBtree myB = new myBtree();
myB.add(12);
myB.add(16);
myB.add(15);
myB.add(18);
myB.add(9);
myB.add(10);
myB.add(8);
myB.add(7);
//二叉树终须排列 主要利用递归思想 通过递归判断左右分支并添加节点,通过递归遍历输出
myB.zhongxu();
}
}
python 版:
二叉树:
class mybtree:
def __init__(self):
self.root=None
def zhong(self):
self.root.zhong()
def add(self,data):
n=self.node()
n.data=data
if self.root is None:
self.root=n
else:
self.root.add(n)
class node:
def __init__(self):
self.data=None
self.left=None
self.right=None
def add(self,n):
if self.data > n.data :
if self.left is None:
self.left = n
else:
self.left.add(n)
if self.data < n.data:
if self.right is None:
self.right = n
else:
self.right.add(n)
def zhong(self):
if self.left is not None :
self.left.zhong()
print(self.data)
if self.right is not None :
self.right.zhong()
tree = mybtree()
tree.add(11)
tree.add(15)
tree.add(8)
tree.add(9)
tree.add(6)
tree.zhong()
-----华丽的分割线
Java版:
单向链表lLinkedList:
mylist类:
package Demo;
public class mylist<T> {
private Node head;
private Node tail;
private int size;
class Node{
private T data;
private Node next;
}
public void add(T data) {
size++;
Node node = new Node();
node.data=data;
if(head == null){
head=node;
}else{
tail.next=node;
}
tail=node;
}
public int size() {
return size;
}
public T get(int i) {
Node p = head;
for (int j = 0;j<i; j++) {
p=p.next;
}
return p.data;
}
}
mylistTest类:
package Demo;
public class mylistTest {
public static void main(String[] args) {
mylist my = new mylist();
my.add("zs");
my.add(123);
my.add("ls");
for (int i = 0; i < my.size(); i++) {
System.out.println(my.get(i));
}
}
}
python版本:
单向链表LinkedList:
'''
定义迭代:
两种:
1.yeild 生成器 函数编程方式
2.类中提供__iter__ __next__面向对象编程方式
__iter__ 的方式__要返回一个具有__next__方法的对象的引用
拓展:
程序员不能得到对象,得到的是对象的引用。引用是个整数,占4个字节
引用的值是引用顺序,而不是引用数值。
'''
class MyList:
class Node:
def __init__(self):
self.data=None
self.next=None
def __init__(self):
self.head=None
self.tail=None
self.size=0
def add(self,data):
self.size +=1
n=self.Node()
n.data=data
if self.head == None:
self.head=n
else:
self.tail.next=n
self.tail=n
def getSize(self):
return self.size
def get(self,i):
p=self.head
for j in range(0,i):
p=p.next
return p.data
my=MyList()
my.add("qsg")
my.add("asd")
my.add(112)
for i in range(0,my.getSize()):
print(my.get(i))
#生成器
print("生成器")
def show():
for i in range(10,0,-1):
yield i
for i in show():
print(i)
#迭代器
class MyList:
class Node:
def __init__(self):
self.data=None
self.next=None
def __init__(self):
self.head=None
self.tail=None
self.size=0
def add(self,data):
self.size +=1
n=self.Node()
n.data=data
if self.head == None:
self.head=n
else:
self.tail.next=n
self.tail=n
def getSize(self):
return self.size
def get(self,i):
p=self.head
for j in range(0,i):
p=p.next
return p.data
def __iter__(self):
self.p=self.head
return self
def __next__(self):
p1 = self.p
self.p = self.p.next
return p1.data
# if self.p is None: 加上异常解决控制台输出错误
#
my1=MyList()
my.add("qsg")
my.add("asd")
my.add(112)
for i in my1:
print(i)